/*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);
void main(void)
{
cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"-------------------------------------------------------------------------------\n"<<endl;
time t1, t2, t3; char c;
do{
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;
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
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);
void main(void)
{
cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"-------------------------------------------------------------------------------\n"<<endl;
char sep; int p=2; double d_n; char c_n; int i_n; long l_n; float f_n;
do{
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;
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
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);
void main(void)
{
cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"-------------------------------------------------------------------------------\n"<<endl;
int a, b;
do{
cout<<"Enter a : "; cin >>a;
cout<<"Enter b : "; cin >>b; swap(a, b);
cout<<"Now a value is : "<<a<<" and b value is : "<<b;
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
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);
void main(void)
{
cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"-------------------------------------------------------------------------------\n"<<endl;
time t1, t2; char c;
do{
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;
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
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);
void main(void)
{
cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"-------------------------------------------------------------------------------\n"<<endl;
int outer_counter=0;
do{
outer_counter++; caller_counter();
cout<<"\nThe main programme counter value is: "<<outer_counter;
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
void caller_counter(void){
//I can't use an automatic variable coz it's created once I call the function only.
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;
void main(void)
{
cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"-------------------------------------------------------------------------------\n"<<endl;
int x, y, z; sterling x1, x2;
do{
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));
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
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);
void main(void)
{
cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"-------------------------------------------------------------------------------\n"<<endl;
fraction f[3]; char c, op;
do{
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;
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
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;}
Thank you so much for solving these questions. Really helpful :)
ReplyDeleteThank you so much for solving these questions. Really helpful :)
ReplyDeleteYar apke pas aage wale chapters ke honge swal solved?
DeleteThanks
ReplyDeleteThanks
ReplyDeleteThanks
ReplyDeleteSor kindly upload chapers' exercises following the 5 th chapter
ReplyDelete