Saturday, July 15, 2017

Robert Lafore 4th Edition Solution Manual Chapter 5 - Functions

CODE:

1. Refer to the CIRCAREA program in Chapter 2, “C++ Programming Basics.” Write a function

called circarea() that finds the area of a circle in a similar way. It should take an argument of type float and return an argument of the same type. Write a main() function that gets a radius value from the user, calls circarea(), and displays the result. #include<iostream> #include<conio.h> using namespace std; float circarea(float radius); int main() { float rad; cout<<"Enter radius of circle: "; cin>> rad; cout <<"Area is "<<circarea(rad); } float circarea(float radius) { return 3.14159F*radius*radius;}

CODE:

2. Raising a number n to a power p is the same as multiplying n by itself p times. Write a function called power() that takes a double value for n and an int value for p, and returns the result as a double value. Use a default argument of 2 for p, so that if this argument is omitted, the number n will be squared. Write a main() function that gets values from the user to test this function. #include<iostream> #include<conio.h> using namespace std; double power(double n, int p=2); int main(void) { double n; int p=2; cout<<"Enter n: "; cin>> n; cout<<"Enter p: "; cin>> p; cout <<"The power is "<<power(n, p); } double power(double n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;}

called power() that takes a double value for n and an int value for p, and returns the result as a double value. Use a default argument of 2 for p, so that if this argument is omitted, the number n will be squared. Write a main() function that gets values from the user to test this function. #include<iostream> #include<conio.h> using namespace std; double power(double n, int p=2); int main(void) { double n; int p=2; cout<<"Enter n: "; cin>> n; cout<<"Enter p: "; cin>> p; cout <<"The power is "<<power(n, p); } double power(double n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;}

CODE:

3. Write a function called zeroSmaller() that is passed two int arguments by reference and then sets the smaller of the two numbers to 0. Write a main() program to exercise this function. #include<iostream> #include<conio.h> using namespace std; int main() { int n1, n2; cout<<"Enter n1: "; cin>> n1; cout<<"Enter n2: "; cin>> n2; cout <<"The number assigned to zero is "; if(zeroSmaller(n1, n2)) cout<<"n1"; else cout<<"n2"; } bool zeroSmaller(int& n1, int& n2) { if(n1<n2) {n1=0; return true;} else {n2=0;return false;} }

the smaller of the two numbers to 0. Write a main() program to exercise this function. #include<iostream> #include<conio.h> using namespace std; int main() { int n1, n2; cout<<"Enter n1: "; cin>> n1; cout<<"Enter n2: "; cin>> n2; cout <<"The number assigned to zero is "; if(zeroSmaller(n1, n2)) cout<<"n1"; else cout<<"n2"; } bool zeroSmaller(int& n1, int& n2) { if(n1<n2) {n1=0; return true;} else {n2=0;return false;} }

CODE:

4.Write a function that takes two Distance values as arguments and returns the larger one. Include

a main() program that accepts two Distance values from the user, compares them, and displays the larger. #include<iostream> #include<conio.h> using namespace std; int distWatch(int d1, int d2); int main() { int d1, d2; cout<<"Enter d1: "; cin>> d1; cout<<"Enter d2: "; cin>> d2; cout <<"The largest distance is "<<distWatch(d1, d2); } int distWatch(int d1, int d2) { if(d1<d2) return d2; else return d1;}

CODE:

5.Write a function called hms_to_secs() that takes three int values—for hours, minutes, and

seconds—as arguments, and returns the equivalent time in seconds (type long). Create a program that exercises this function by repeatedly obtaining a time value in hours, minutes, and seconds from the user (format 12:59:59), calling the function, and displaying the value of seconds it returns. #include<iostream> #include<conio.h> using namespace std; long hms_to_secs(int hours, int minutes, int seconds); int main() { int h, m, s; char sep; cout<<"Enter the time in format hh:mm:ss : "; cin>>h>>sep>>m>>sep>>s; cout <<"The equivalent time in seconds is : "<<hms_to_secs(h, m, s); } long hms_to_secs(int hours, int minutes, int seconds) { return seconds+minutes*60+hours*3600;}

CODE:

6.Start with the program from Exercise 11, Chapter 4, “Structures,” which adds two struct time

values. Keep the same functionality, but modify the program so that it uses two functions. The first, time_to_secs(), takes as its only argument a structure of type time, and returns the equivalent in seconds (type long). The second function, secs_to_time(), takes as its only argument a time in seconds (type long), and returns a structure of type time. #include<iostream> #include<conio.h> using namespace std; struct time{int hours; int minutes; int seconds;}; long time_to_secs(time t); time secs_to_time(long s); int main() { time t1, t2, t3; char c; cout<<"In [hh:mm:ss] format;\n"; cout<<"Enter first time value : "; cin >>t1.hours>>c>>t1.minutes>>c>>t1.seconds; cout<<"Enter second time value: "; cin >>t2.hours>>c>>t2.minutes>>c>>t2.seconds; t3=secs_to_time(time_to_secs(t1)+time_to_secs(t2)); cout<<"The result is: "<<t3.hours<<":"<<t3.minutes<<":"<<t3.seconds; } long time_to_secs(time t){ return t.hours*3600+t.minutes*60+t.seconds;} time secs_to_time(long s){ time t; t.seconds=s%60; t.minutes=((s-t.seconds)%3600)/60; t.hours=s/3600; if(t.seconds>59) {t.seconds-=59; t.minutes++;} //Check seconds over. if(t.minutes>59) {t.minutes-=59; t.hours++;} //Check minutes over. return t; }

CODE:

7.Start with the power () function of Exercise 2, which works only with type double. Create a series of overloaded functions with the same name that, in addition to double, also work with types char, int, long, and float. Write a main() program that exercises these overloaded functions with all argument types. #include<iostream> #include<conio.h> using namespace std; double power(double n, int p=2); char power(char n, int p=2); int power(int n, int p=2); long power(long n, int p=2); float power(float n, int p=2); int main() { char sep; int p=2; double d_n; char c_n; int i_n; long l_n; float f_n; cout<<"In [n^p] format;\n"; cout<<"Enter a double type n : "; cin >>d_n>>sep>>p; cout<<"The power is "<<power(d_n, p)<<endl; cout<<"Enter a char type n : "; cin >>c_n>>sep>>p; cout<<"The power is "<<power(c_n, p)<<endl; cout<<"Enter a int type n : "; cin >>i_n>>sep>>p; cout<<"The power is "<<power(i_n, p)<<endl; cout<<"Enter a long type n : "; cin >>l_n>>sep>>p; cout<<"The power is "<<power(l_n, p)<<endl; cout<<"Enter a float type n : "; cin >>f_n>>sep>>p; cout<<"The power is "<<power(f_n, p)<<endl; } double power(double n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} char power(char n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} int power(int n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} long power(long n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} float power(float n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;}

of overloaded functions with the same name that, in addition to double, also work with types char, int, long, and float. Write a main() program that exercises these overloaded functions with all argument types. #include<iostream> #include<conio.h> using namespace std; double power(double n, int p=2); char power(char n, int p=2); int power(int n, int p=2); long power(long n, int p=2); float power(float n, int p=2); int main() { char sep; int p=2; double d_n; char c_n; int i_n; long l_n; float f_n; cout<<"In [n^p] format;\n"; cout<<"Enter a double type n : "; cin >>d_n>>sep>>p; cout<<"The power is "<<power(d_n, p)<<endl; cout<<"Enter a char type n : "; cin >>c_n>>sep>>p; cout<<"The power is "<<power(c_n, p)<<endl; cout<<"Enter a int type n : "; cin >>i_n>>sep>>p; cout<<"The power is "<<power(i_n, p)<<endl; cout<<"Enter a long type n : "; cin >>l_n>>sep>>p; cout<<"The power is "<<power(l_n, p)<<endl; cout<<"Enter a float type n : "; cin >>f_n>>sep>>p; cout<<"The power is "<<power(f_n, p)<<endl; } double power(double n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} char power(char n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} int power(int n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} long power(long n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;} float power(float n, int p) { for(int ret=1; p>0; p--) ret*=n; return ret;}

CODE:

8. Write a function called swap() that interchanges two int values passed to it by the calling program.

(Note that this function swaps the values of the variables in the calling program, not those in the function.) You’ll need to decide how to pass the arguments. Create a main() program to exercise the function. #include<iostream> #include<conio.h> using namespace std; void swap(int& a, int& b); int main() { int a, b; cout<<"Enter a : "; cin >>a; cout<<"Enter b : "; cin >>b; swap(a, b); cout<<"Now a value is : "<<a<<" and b value is : "<<b; } void swap(int& a, int& b){int c=a; a=b; b=c;}

CODE:

9. This exercise is similar to Exercise 8, except that instead of two int variables, have the swap()

function interchange two struct time values (see Exercise 6). #include<iostream> #include<conio.h> using namespace std; struct time{int hours; int minutes; int seconds;}; void swap(time& t1, time& t2); int main() { time t1, t2; char c; cout<<"In [hh:mm:ss] format;\n"; cout<<"Enter first time value : "; cin >>t1.hours>>c>>t1.minutes>>c>>t1.seconds; cout<<"Enter second time value: "; cin >>t2.hours>>c>>t2.minutes>>c>>t2.seconds; swap(t1, t2); cout<<"Now first time is : " <<t1.hours<<c<<t1.minutes<<c<<t1.seconds <<" and second time is : "<<t2.hours<<c<<t2.minutes<<c<<t2.seconds; } void swap(time& a, time& b){time c=a; a=b; b=c;}

CODE:

10. Write a function that, when you call it, displays a message telling how many times it has been

called: “I have been called 3 times”, or whatever. Write a main() program that calls this function at least 10 times. Try implementing this function in two different ways. First, use an external variable to store the count. Second, use a local static variable. Which is more appropriate? Why can’t you use an automatic variable? #include<iostream> #include<conio.h> using namespace std; void caller_counter(void); int main() { int outer_counter=0; outer_counter++; caller_counter(); cout<<"\nThe main programme counter value is: "<<outer_counter; } void caller_counter(void) { static int inner_counter=0; inner_counter++; cout<<"I have been called "<<inner_counter<<" times"; }

CODE:

11. Write a program, based on the sterling structure of Exercise 10 in Chapter 4, “Structures,” that obtains from the user two money amounts in old-style British format (£9:19:11), adds them, and displays the result, again in old-style format. Use three functions. The first should obtain a pounds- shillings-pence value from the user and return the value as a structure of type sterling. The second should take two arguments of type sterling and return a value of the same type, which is the sum of the arguments. The third should take a sterling structure as its argument and display its value.*/ #include<iostream> #include<conio.h> using namespace std; struct sterling{int pounds; int shillings; int pence;}; sterling psp_to_sterling(int pounds, int shillings, int pence); sterling sterling_add(sterling s1, sterling s2); void sterling_disp(sterling s); char c; int main() { int x, y, z; sterling x1, x2; cout<<"In [9:19:11] format;\n"; cout<<"Enter first money amount in old-style British : \x9c"; cin>>x>>c>>y>>c>>z; x1=psp_to_sterling(x, y, z); cout<<"Enter second money amount in old-style British : \x9c"; cin>>x>>c>>y>>c>>z; x2=psp_to_sterling(x, y, z); sterling_disp(sterling_add(x1, x2)); } sterling psp_to_sterling(int pounds, int shillings, int pence) { sterling x; x.pounds=pounds; x.shillings=shillings; x.pence=pence; return x; } sterling sterling_add(sterling s1, sterling s2) { s1.pounds += s2.pounds; s1.shillings += s2.shillings; s1.pence += s2.pence; if(s1.pence>11){s1.shillings += static_cast<int>(s1.pence/12); s1.pence %= 12;} if(s1.shillings>19){s1.pounds += static_cast<int>(s1.shillings/20); s1.shillings %= 20;} return s1;} void sterling_disp(sterling s){ cout<<"Total is : \x9c" <<s.pounds<<c<<s.shillings<<c<<s.pence;}

obtains from the user two money amounts in old-style British format (£9:19:11), adds them, and displays the result, again in old-style format. Use three functions. The first should obtain a pounds- shillings-pence value from the user and return the value as a structure of type sterling. The second should take two arguments of type sterling and return a value of the same type, which is the sum of the arguments. The third should take a sterling structure as its argument and display its value.*/ #include<iostream> #include<conio.h> using namespace std; struct sterling{int pounds; int shillings; int pence;}; sterling psp_to_sterling(int pounds, int shillings, int pence); sterling sterling_add(sterling s1, sterling s2); void sterling_disp(sterling s); char c; int main() { int x, y, z; sterling x1, x2; cout<<"In [9:19:11] format;\n"; cout<<"Enter first money amount in old-style British : \x9c"; cin>>x>>c>>y>>c>>z; x1=psp_to_sterling(x, y, z); cout<<"Enter second money amount in old-style British : \x9c"; cin>>x>>c>>y>>c>>z; x2=psp_to_sterling(x, y, z); sterling_disp(sterling_add(x1, x2)); } sterling psp_to_sterling(int pounds, int shillings, int pence) { sterling x; x.pounds=pounds; x.shillings=shillings; x.pence=pence; return x; } sterling sterling_add(sterling s1, sterling s2) { s1.pounds += s2.pounds; s1.shillings += s2.shillings; s1.pence += s2.pence; if(s1.pence>11){s1.shillings += static_cast<int>(s1.pence/12); s1.pence %= 12;} if(s1.shillings>19){s1.pounds += static_cast<int>(s1.shillings/20); s1.shillings %= 20;} return s1;} void sterling_disp(sterling s){ cout<<"Total is : \x9c" <<s.pounds<<c<<s.shillings<<c<<s.pence;}

CODE:

12. Revise the four-function fraction calculator from Exercise 12, Chapter 4, so that it uses

functions for each of the four arithmetic operations. They can be called fadd(), fsub(), fmul(), and fdiv (). Each of these functions should take two arguments of type struct fraction, and return an argument of the same type. #include<iostream> #include<conio.h> using namespace std; struct fraction{int numerator; int denominator;}; fraction fadd(fraction a, fraction b); fraction fsub(fraction a, fraction b); fraction fmul(fraction a, fraction b); fraction fdiv(fraction a, fraction b); int main() { fraction f[3]; char c, op; cout<<"Enter your task : "; cin >>f[0].numerator>>c>>f[0].denominator>>op>>f[1].numerator>>c>>f[1].denominator; if(!f[0].denominator || !f[1].denominator) {cout<<"Illeagle fraction !"<<endl; op=false;} switch(op) { case '+': f[2]=fadd(f[0], f[1]); break; case '-': f[2]=fsub(f[0], f[1]); break; case '*': f[2]=fmul(f[0], f[1]); break; case '/': f[2]=fdiv(f[0], f[1]); break; default: cout<<"Unknow operator please try again !"<<endl;} cout<<"Answer = "<<f[2].numerator<<c<<f[2].denominator; } fraction fadd(fraction a, fraction b) { fraction f; f.numerator =a.numerator*b.denominator+a.denominator*b.numerator; f.denominator=a.denominator*b.denominator; return f; } fraction fsub(fraction a, fraction b) { fraction f; f.numerator =a.numerator*b.denominator-a.denominator*b.numerator; f.denominator=a.denominator*b.denominator; return f; } fraction fmul(fraction a, fraction b) { fraction f; f.numerator =a.numerator*b.numerator; f.denominator=a.denominator*b.denominator; return f; } fraction fdiv(fraction a, fraction b) { fraction f; if(b.numerator != 0) { f.numerator =a.numerator*b.denominator; f.denominator=b.numerator*a.denominator; } else cout<<"Math error !"<<endl; return f; }
Share:

1 comment: