Write a function to read a matrix of size m*n from the keyboard.
#include<iostream> #include<iomanip> using namespace std; void matrix(int m,int n) { float **p; p=new float*[m]; for(int i=0;i<m;i++) { p[i]=new float[n]; } cout<<" Enter "<<m<<"by"<<n<<" matrix elements one by one "<<endl; for(i=0;i<m;i++) { for(int j=0;j<n;j++) { float value; cin>>value; p[i][j]=value; } } cout<<" The given matrix is :"<<endl; for(i=0;i<m;i++) { for(int j=0;j<n;j++) { cout<<p[i][j]<<" "; } cout<<"\n"; } } int main() { int r,c; cout<<" Enter size of matrix : "; cin>>r>>c; matrix(r,c); return 0; }
OUTPUT
Enter size of matrix : 3 4
Enter 3 by 4 matrix elements one by one
1 2 3 4
2 3 4 5
3 4 5 6
The given matrix is :
1 2 3 4
2 3 4 5
3 4 5 6
Write a program to read a matrix of size m*n from the keyboard and display the same on the screen using function.
#include<iostream> #include<iomanip> using namespace std; void matrix(int m,int n) { float **p; p=new float*[m]; for(int i=0;i<m;i++) { p[i]=new float[n]; } cout<<" Enter "<<m<<" by "<<n<<" matrix elements one by one "<<endl; for(i=0;i<m;i++) { for(int j=0;j<n;j++) { float value; cin>>value; p[i][j]=value; } } cout<<" The given matrix is :"<<endl; for(i=0;i<m;i++) { for(int j=0;j<n;j++) { cout<<p[i][j]<<" "; } cout<<"\n"; } } int main() { int r,c; cout<<" Enter size of matrix : "; cin>>r>>c; matrix(r,c); return 0; }
OUTPUT
Enter size of matrix : 4 4
Enter 4 by 4 matrix elements one by one
1 2 3 4 7
2 3 4 5 8
3 4 5 6 9
The given matrix is :
1 2 3 4 7
2 3 4 5 8
3 4 5 6 9
Rewrite the program of Exercise 4.2 to make the row parameter of the matrix as a default argument.
#include<iostream> #include<iomanip> using namespace std; void matrix(int n,int m=3) { float **p; p=new float*[m]; for(int i=0;i<m;i++) { p[i]=new float[n]; } cout<<" Enter "<<m<<" by "<<n<<" matrix elements one by one "<<endl; for(i=0;i<m;i++) { for(int j=0;j<n;j++) { float value; cin>>value; p[i][j]=value; } } cout<<" The given matrix is :"<<endl; for(i=0;i<m;i++) { for(int j=0;j<n;j++) { cout<<p[i][j]<<" "; } cout<<"\n"; } } int main() { int c; cout<<" Enter column of matrix : "; cin>>c; matrix(c); return 0; }
OUTPUT
Enter column of matrix : 3
Enter 3 by 3 matrix elements one by one
1 2 3
2 3 4
3 4 5
The given matrix is :
1 2 3
2 3 4
3 4 5
The effect of a default argument can be alternatively achieved by overloading. Discuss with examples.
#include<iostream> #include<iomanip> using namespace std; void matrix(int m,int n) { float **p; p=new float*[m]; for(int i=0;i<m;i++) { p[i]=new float[n]; } cout<<" Enter "<<m<<"by"<<n<<" matrix elements one by one "<<endl; for(i=0;i<m;i++) { for(int j=0;j<n;j++) { float value; cin>>value; p[i][j]=value; } } cout<<" The given matrix is :"<<endl; for(i=0;i<m;i++) { for(int j=0;j<n;j++) { cout<<p[i][j]<<" "; } cout<<"\n"; } } void matrix(int m,long int n=3) { float **p; p=new float*[m]; for(int i=0;i<m;i++) { p[i]=new float[n]; } cout<<" Enter "<<m<<" by "<<n<<" matrix elements one by one "<<endl; for(i=0;i<m;i++) { for(int j=0;j<n;j++) { float value; cin>>value; p[i][j]=value; } } cout<<" The given matrix is :"<<endl; for(i=0;i<m;i++) { for(int j=0;j<n;j++) { cout<<p[i][j]<<" "; } cout<<"\n"; } } int main() { int r; cout<<" Enter row of matrix : "; cin>>r; matrix(r); return 0; }
OUTPUT
Enter column of matrix : 2
Enter 2 by 3 matrix elements one by one
1 0 1
0 2 1
The given matrix is :
1 0 1
0 2 1
Write a macro that obtains the largest of the three numbers.
#include<iostream> #include<iomanip> using namespace std; float large(float a,float b,float c) { float largest; if(a>b) { if(a>c) largest=a; else largest=c; } else { if(b>c) largest=b; else largest=c; } return largest; } int main() { float x,y,z; cout<<" Enter three values : "; cin>>x>>y>>z; float largest=large(x,y,z); cout<<" large = "<<largest<<endl; return 0; }
OUTPUT
Enter three values : 4 5 8
large = 8
Redo Exercise 4.16 using inline function. Test the function using a main function.
Missing
Write a function power() to raise a number m to power n. The function takes a double value for m and int value for n and returns the result correctly. Use a default value of 2 for n to make the function to calculate the squares when this argument is omitted. Write a main that gets the values of m and n from the user to test the function.
#include<iostream> #include<iomanip> #include<math> using namespace std; long double power(double m,int n) { long double mn=pow(m,n); return mn; } long double power(double m,long int n=2) { long double mn=pow(m,n); return mn; } int main() { long double mn; double m; int n; cout<<" Enter the value of m & n"<<endl; cin>>m>>n; mn=power(m,n); cout<<" m to power n : "<<mn<<endl; mn=power(m); cout<<" m to power n : "<<mn<<endl; return 0; }
OUTPUT
Enter the value of m & n
12 6
m to power n : 2985984
m to power n: 144
Write a function that performs the same operation as that of Exercise 4.18 but takes an int value for m. Both the functions should have the same name. Write a main that calls both the functions. Use the concept of function overloading.
#include<iostream> #include<iomanip> #include<math> using name space std; long double power(int m,int n) { long double mn= (long double)pow(m,n); return mn; } long double power(int m,long int n=2) { long double mn=(long double)pow(m,n); return mn; } int main() { long double mn; int m; int n; cout<<" Enter the value of m & n"<<endl; cin>>m>>n; mn=power(m,n); cout<<" m to power n : "<<mn<<endl; mn=power(m); cout<<" m to power n : "<<mn<<endl; return 0; }
OUTPUT
Enter the value of m & n
15 16
m to power n : 6.568408e+18
m to power n: 225
Next Previous