Вы находитесь на странице: 1из 18

SUSCET

CSE-3rd SEM

OOP

Program-40 Aim:To write a program to create and display an array. Program:


#include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int n,i,a[50]; cout<<" \n enter value of n:"; cin>>n; cout<<" \n enter the different array elements are:"; for (i=0;i<n;i++) { cin>>a[i]; } cout<<"entered elements are:"; for(i=0;i<n;i++) { cout<<" "<<a[i]; } getch(); }

Priya Gupta

69

100690309898

SUSCET

CSE-3rd SEM

OOP

Output:

Priya Gupta

70

100690309898

SUSCET

CSE-3rd SEM

OOP

Program-41 Aim:To write a program to find the average of elements in the array. Program:
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,i,a[10],sum=0,j; float av; cout<<" \n enter value of n:"; cin>>n; cout<<" \n enter the different array elements are:"; for (i=0;i<n;i++) { cin>>a[i]; } cout<<"entered elements are:"; for(i=0;i<n;i++) { cout<<" "<<a[i]; } for(i=0;i<n;i++) { sum=sum+a[i]; Priya Gupta 71 100690309898

SUSCET } cout<<" \n sum of matrix is:"<<sum; av=(float)sum/n;

CSE-3rd SEM

OOP

cout<<"\n averge of array elements:"<<av; getch(); }

Output:

Priya Gupta

72

100690309898

SUSCET

CSE-3rd SEM

OOP

Program-42 Aim:To write a program to find a largest element in an array. Program:


#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,i,a[10],large; cout<<" \n enter value of n:"; cin>>n; cout<<" \n enter the different array elements are:"; for (i=0;i<n;i++) { cin>>a[i]; } cout<<"entered elements are:"; for(i=0;i<n;i++) { cout<<" "<<a[i]; } large=a[0]; for (i=1;i<n;i++) { if(large<a[i]) Priya Gupta 73 100690309898

SUSCET { large=a[i]; } }

CSE-3rd SEM

OOP

cout<<"\n largest element is:"<<large<<endl; getch(); }

Output:

Priya Gupta

74

100690309898

SUSCET

CSE-3rd SEM

OOP

Program-43 Aim:To write a program to addition,transpose,trace of elements by using switch statement. Program:
#include<iostream.h> #include<conio.h> void main() { clrscr(); int sum=0, m,n,i,a[10][20],p,q,b[20][10],c[10][10],j,k,ch; cout<<" \n enter number of rows and colums of first matrix:"; cin>>m>>n; cout<<"\n enter the number of rows and columns of second matrix:"; cin>>p>>q; cout<<" \n enter the different array elements are:"; for (i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>a[i][j]; } } cout<<"entered first matrix is:"; for (i=0;i<m;i++) { Priya Gupta 75 100690309898

SUSCET cout<<"\n"; for(j=0;j<n;j++) { cout<<"\t"<<a[i][j]; } } cout<<"\n enter the second matrix:"; for(i=0;i<p;i++) { for(j=0;j<q;j++) { cin>>b[i][j]; } } cout<<"\n entered second matrix is:"; for(i=0;i<p;i++) { cout<<"\n"; for(j=0;j<q;j++) { cout<<"\t"<<b[i][j]; } } cout<<"\n 1.addition"; cout<<"\n 2.sum of digonal elements"; Priya Gupta

CSE-3rd SEM

OOP

76

100690309898

SUSCET cout<<"\n 3.transponse of array"; cout<<"enter the choice:"; cin>>ch; switch(ch) { case 1: if (n==p) cout<<"addition is possible"<<endl; else

CSE-3rd SEM

OOP

cout<<"addition is not possible"<<endl; for(i=0;i<m;i++) { c[i][j]=0; for(j=0;j<n;j++) { c[i][j]=a[i][j]+b[i][j]; } } cout<<"result of addition is:"<<endl; for (i=0;i<m;i++) { cout<<"\n"; for (j=0;j<n;j++) { cout<<"\t"<<c[i][j]; Priya Gupta 77 100690309898

SUSCET } } break; case 2: for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(i==j) { sum=sum+a[i][j]; } } } cout<<"sum is"<<sum; break; case 3: cout<<"transponse of elements:"; for (i=0;i<n;i++) { cout<<"\n"; for (j=0;j<m;j++) { cout<<"\t"<<a[j][i]; } Priya Gupta

CSE-3rd SEM

OOP

78

100690309898

SUSCET } getch(); } }

CSE-3rd SEM

OOP

Output:

Priya Gupta

79

100690309898

SUSCET

CSE-3rd SEM

OOP

Program-44 Aim:To write a program to multiply of two arrays. Program:


#include<iostream.h> #include<conio.h> void main() { clrscr(); int m,n,i,a[10][20],p,q,b[20][10],c[10][10],j,k; cout<<" \n enter number of rows and colums of first matrix:"; cin>>m>>n; cout<<"\n enter the number of rows and columns of second matrix:"; cin>>p>>q; cout<<" \n enter the different array elements are:"; for (i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>a[i][j]; } } cout<<"entered first matrix is:"<<endl; for (i=0;i<m;i++) { Priya Gupta 80 100690309898

SUSCET cout<<"\n"; for(j=0;j<n;j++) { cout<<"\t"<<a[i][j]; } }

CSE-3rd SEM

OOP

cout<<"\n enter the second matrix:"<<endl; for(i=0;i<p;i++) { for(j=0;j<q;j++) { cin>>b[i][j]; } } cout<<"entered second matrix is:"<<endl; for(i=0;i<p;i++) { cout<<"\n"; for(j=0;j<q;j++) { cout<<"\t"<<b[i][j]; } } if (n==p) cout<<"\n multpication is possible"<<endl; Priya Gupta 81 100690309898

SUSCET else

CSE-3rd SEM

OOP

cout<<"\n multipication is not possible"<<endl; for(i=0;i<m;i++) { c[i][j]=0; for(j=0;j<n;j++) { for (k=0;k<n;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } cout<<"result of multipication is:"<<endl; for (i=0;i<m;i++) { cout<<"\n"; for (j=0;j<n;j++) { cout<<"\t"<<c[i][j]; } } getch(); }

Priya Gupta

82

100690309898

SUSCET

CSE-3rd SEM

OOP

Output:

Priya Gupta

83

100690309898

SUSCET

CSE-3rd SEM

OOP

Program-45 Aim:To write a program to find the sum of row sum,column sum,grand sum of the array. Program:
#include<iostream.h> #include<conio.h> void main() { int a[10][10],m,n,i,j,rs,cs,gs; clrscr(); cout<<"\n enter the number of row and columns:"; cin>>m>>n; cout<<"entered the elements:"; for (i=1;i<=m;i++) { for (j=1;j<=n;j++) { cin>>a[i][j]; } } cout<<"entered the matrix:"; for (i=1;i<=m;i++) { cout<<"\n"; Priya Gupta 84 100690309898

SUSCET for(j=1;j<=n;j++) { cout<<" "<<a[i][j]; } } gs=0; for (i=1;i<=m;i++) { rs=0; for (j=1;j<=n;j++) { rs=rs+a[i][j]; } cout<<"\n row sum is:"<<rs; gs=gs+rs; } cout<<"\n Grand sum is:"<<gs; for (j=1;j<=n;j++) { cs=0; for (i=1;i<=m;i++) { cs=cs+a[i][j]; } cout<<"\n column sum:"<<cs; Priya Gupta

CSE-3rd SEM

OOP

85

100690309898

SUSCET } getch(); }

CSE-3rd SEM

OOP

Output:

Priya Gupta

86

100690309898

Вам также может понравиться