Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views8 pages

Array

The document contains a series of C++ programming tasks related to arrays and matrices. It includes code snippets for summing arrays, identifying even and odd numbers, finding maximum and minimum values, handling 2D arrays, adding and transposing matrices, calculating determinants, and multiplying matrices. Each task is accompanied by example console outputs demonstrating the results.

Uploaded by

sakibsikder1919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Array

The document contains a series of C++ programming tasks related to arrays and matrices. It includes code snippets for summing arrays, identifying even and odd numbers, finding maximum and minimum values, handling 2D arrays, adding and transposing matrices, calculating determinants, and multiplying matrices. Each task is accompanied by example console outputs demonstrating the results.

Uploaded by

sakibsikder1919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Q1.Write 2 array and sum them and then put them in another array.

A1. #include <iostream>


using namespace std;
int main() {
double a1[5]={1,3,5,6,7};
double a2[5]={2,3,4,5,6};
double a3[5];
for(int i=0;i<5;i++){a3[i]=a1[i]+a2[i];

cout<<"a3["<<i<<"]="<<a3[i]<<endl;}
return 0;
}
CONSOLE:
a3[0]=3

a3[1]=6

a3[2]=9

a3[3]=11

a3[4]=13

Q2.Write a program that can found even and odd numbers from an
array.
A2. #include <iostream>
using namespace std;
int main() {
int a1[5]={1,3,5,6,7};
for(int i=0;i<5;i++){
if(a1[i]%2==0){cout<<"a["<<i<<"] is even"<<endl;}
else cout<<"a["<<i<<"] is odd"<<endl;}

return 0;
}
CONSOLE:
a[0] is odd

a[1] is odd

a[2] is odd

a[3] is even

a[4] is odd

Q3.Write an array and find its maximum and minimum value.


A3. #include<iostream>
#include<cmath>
using namespace std;
int main(){
int A[10]={1,2,3,4,5,6,7,8,9,10};
int max=A[0];
int min=A[1];
for(int i=0;i<10;i++){
if(max<A[i]) max=A[i];
}
cout<<"Maximum value: "<<max<<endl;
for(int i=0;i<10;i++){
if(min>A[i]) min=A[i];
}
cout<<"Minimum value: "<<min<<endl;
return 0;}
CONSOLE:
Maximum value: 10

Minimum value: 1

Q4.Write a code segment that can take the values of element of a 2


dimentional array of size 3*3 by cin command and also print the values
in the form of a matrix.
A4. #include<iostream>
#include<cmath>
using namespace std;
int main(){
int A[3][3];

for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout<<"Enter element a"<<i<<j<<":";
cin>>A[i][j];
}

for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout<<A[i][j]<<" ";
if(j==3-1)cout<<endl;
}}
return 0;}
CONSOLE:
Enter element a00:1

Enter element a01:2

Enter element a02:3

Enter element a10:4

Enter element a11:5

Enter element a12:6

Enter element a20:7

Enter element a21:8

Enter element a22:9

123

456

789

Q5.Write a matrix. i) Add 2 dimensional matrix ii) Transpose the matrix.


A5. I) #include<iostream>
#include<cmath>

using namespace std;

int main(){

int A[2][3]={1,2,3,4,5,6};

int B[2][3]={10,20,30,40,50,60};

int C[2][3];

for(int i=0;i<2;i++){

for(int j=0;j<3;j++){

C[i][j]=A[i][j]+B[i][j];

cout<<C[i][j]<<" ";}

cout<<endl;}
return 0;}

CONSOLE:
11 22 33

44 55 66

II) #include<iostream>
#include<cmath>
using namespace std;
int main(){
int A[2][3]={1,2,3,4,5,6};
int B[2][3]={10,20,30,40,50,60};
int C[2][3];
int transC[3][2];
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
C[i][j]=A[i][j]+B[i][j];
transC[j][i]=C[i][j];}}
for(int i=0;i<3;i++){
for(int j=0;j<2;j++){
cout<<transC[i][j]<<" ";
if(j==2-1)cout<<endl;
}}

return 0;}
CONSOLE:
11 44

22 55
33 66

Q6.Calculate determinant of a matrix.


A6. #include<iostream>
#include<cmath>
using namespace std;
float epsilon(int i,int j,int k){

return 0.5*(i-j)*(j-k)*(k-i);
}
int main(){ int sum=0;
int A[3][3]={2,4,5,6,7,8,9,10,12};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
sum=sum+epsilon(i,j,k)*A[0][i]*A[1][j]*A[2][k];
}
}
}

cout<<"Determinant: "<<sum<<endl;

return 0;}
CONSOLE:
Determinant: -7

Q7. Multiply two matrix.


A7. #include<iostream>
#include<cmath>
using namespace std;
int main(){
int A[3][3]={2,4,5,6,7,8,9,10,12};
int B[3][3]={5,6,3,7,9,3,2,2,4};
int C[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
C[i][j]=0;
for(int k=0;k<3;k++){
C[i][j]+=(A[i][k]*B[k][j]);
}
}
}

for(int i=0;i<3;i++){
for(int j=0;j<3;j++){cout<<C[i][j]<<" ";}
cout<<endl;
}

return 0;}
CONSOLE:
48 58 38

95 115 71

139 168 105

You might also like