Arrays Programs in C++
#include< iostream> using namespace std; int main() { int i, a[5],sum = 0; cout<<"Enter 5 Elements : "; for(i =0; i< 5; i++) { cin>>a[i]; } for(i =0; i< 5; i++) { sum = sum +a[i]; } cout<<"Sum = "<< sum<< endl; return 0; }OUTPUT
Enter 5 Elements : 12 13 14 15 14 Sum = 68
#include< iostream> using namespace std; int main() { int i, a[5] ; cout<<"Enter 5 Elements : "; for(i =0; i< 5; i++) { cin>>a[i]; } int max = a[0]; for(i =1; i< 5; i++) { if(max < a[i]) { max = a[i]; } } cout<<"Max = "<< max<< endl; return 0; }OUTPUT
Enter 5 Elements : 12 34 56 43 32 Max = 56
#include< iostream> using namespace std; int main() { int i, a[5] ; cout<<"Enter 5 Elements : "; for(i =0; i< 5; i++) { cin>>a[i]; } int min = a[0]; for(i =1; i 5; i++) { if(min>a[i]) { min = a[i]; } } cout<<"Min = "<< min<< endl; return 0; }OUTPUT
Enter 5 Elements : 12 34 11 36 342 Min = 11
#include< iostream> using namespace std; int main() { int m = 0, a[5],min,smin ; cout<<"Enter 5 Elements : "; for(int i =0; i< 5; i++) { cin>>a[i]; } min = a[0]; for(int i =1; i< 5; i++) { if(a[i] < min) { min = a[i]; m = i; } } smin = a[5-m-1]; for(int i =0 ; i< 5; i++) { if(smin> a[i] && m!= i) { smin = a[i]; } } cout<<"Second Min = "<< smin; return 0; }OUTPUT
Enter 5 Elements : 1 3 45 32 54 Second Min = 3
#include< iostream> using namespace std; int main() { int m = 0, a[5],max,smax ; cout<<"Enter 5 Elements : "; for(int i =0; i< 5; i++) { cin>>a[i]; } max = a[0]; for(int i =1; i< 5; i++) { if(a[i]>max) { max = a[i]; m = i; } } smax = a[5-m-1]; for(int i =0 ; i< 5; i++) { if(smax< a[i] && m!= i) { smax = a[i]; } } cout<<"Second Max = "<< smax; return 0; }OUTPUT
Enter 5 Elements : 1 3 45 32 54 Second Max = 45
#include< iostream> using namespace std; int main() { int a[5]={1,10,23,45,21}; int i,n,flag = 0; cout<<"Enter a Element : "; cin>>n; for(i=0; i< 5; i++){ if(a[i]==n){ flag = 1; break; } } if(flag==0) { cout<<"Element not Found\n"; } else { cout<<"Found at "<< i+1<< endl; } return 0; }OUTPUT
Enter a Element : 23 Found at 3
#include< iostream> using namespace std; int main() { int m1[3][3],m2[3][3],m3[3][3]; cout<<"Enter Elements of Mat1: \n"; for(int i = 0; i< 3; i++) { for(int j = 0; j< 3; j++) { cin>>m1[i][j]; } } cout<<"Enter Elements of Mat2: \n"; for(int i = 0; i< 3; i++) { for(int j = 0; j< 3; j++) { cin>>m2[i][j]; } } cout<<"Sum of the matrix: \n"; for(int i = 0; i< 3; i++) { for(int j = 0; j< 3; j++) { m3[i][j] = m1[i][j]+m2[i][j]; cout<< m3[i][j]<<" "; } cout<< endl; } return 0; }OUTPUT
Enter Elements of Mat1: 1 2 3 2 3 4 6 8 2 Enter Elements of Mat2: 3 4 2 1 2 1 3 1 2 Sum of the matrix: 4 6 5 3 5 5 9 9 4
#include< iostream> using namespace std; int main() { int m1[3][3],m2[3][3],m3[3][3]; cout<<"Enter Elements of Mat1:\n"; for(int i = 0; i< 3; i++) { for(int j = 0; j< 3; j++) { cin>>m1[i][j]; } } cout<<"Enter Elements of Mat2:\n"; for(int i = 0; i< 3; i++) { for(int j = 0; j< 3; j++) { cin>>m2[i][j]; } } cout<<"Difference of matrix:\n"; for(int i = 0; i< 3; i++) { for(int j = 0; j< 3; j++) { m3[i][j] = m1[i][j]-m2[i][j]; cout<< m3[i][j]<<" "; } cout<< endl; } return 0; }OUTPUT
Enter Elements of Mat1: 3 4 3 2 3 4 6 8 2 Enter Elements of Mat2: 1 2 2 1 2 1 3 1 2 Difference of matrix: 2 2 1 1 1 3 3 7 0
#include< iostream> using namespace std; int main() { int r1 = 3, c1= 2, r2 = 2; int c2 = 4,sum =0; int m1[3][2],m2[2][4],m3[3][4]; cout<<"Enter Elements of Mat1: \n"; for(int i = 0; i< r1; i++) { for(int j = 0; j< c1; j++) { cin>>m1[i][j]; } } cout<<"Enter Elements of Mat2: \n"; for(int i = 0; i< r2; i++) { for(int j = 0; j< c2; j++) { cin>>m2[i][j]; } } for(int i = 0; i< r1; i++) { for(int j = 0; j< c2; j++) { for(int k = 0; k< r2;k++) { sum = sum+m1[i][k]*m2[k][j]; } m3[i][j]= sum; sum = 0; } } cout<<"Product of matrix: \n"; for(int i = 0; i< r2; i++) { for(int j = 0; j< c2; j++) { cout<< m3[i][j]<<" "; } cout<< endl; } return 0; }OUTPUT
Enter Elements of Mat1: 1 3 3 4 2 3 Enter Elements of Mat2: 1 2 3 4 3 4 5 6 Product of matrix: 10 14 18 22 15 22 29 36
#include< iostream> using namespace std; int main() { int mat[3][2],tran[2][3]; cout<<"Enter Elements of Mat: \n"; for(int i = 0; i< 3; i++) { for(int j = 0; j< 2; j++) { cin>>mat[i][j]; tran[j][i] = mat[i][j]; } } cout<<"Transpose of Matrix: \n"; for(int i = 0; i< 2; i++) { for(int j = 0; j< 3; j++) { cout<< tran[i][j]<<" "; } cout<< endl; } return 0; }OUTPUT
Enter Elements of Mat 3 4 2 3 6 8 Transpose of Matrix: 3 2 6 4 3 8