Lab Work W15
Task I
Create a C++ program that sums the diagonal elements of a 2D array(Matrix):
#includeiostream
usingnamespacestd;
int main()
{
inti, j, A[3][3], sum=0;
cout"Enter the elements of the A matrix "endl;
for (i=0;i<3;i++){
for (j=0;j<3;j++){
cin>A[i][j];}}
for (i=0;i<3;i++){
for (j=0;j<3;j++){
if(i==j){
sum+=A[i][j];
cout<A[i][j]<endl;}
}}
cout"the sum of the A diagonal elements is "<sum;
system("pause");
return 0;
}
Task II
Create a C++ program that sums two 2D arrays(Matrix) that you initialize dinamiccaly:
#includeiostream
usingnamespace::std;
int main()
{
inti, j, A[3][3], B[3][3], C[3][3], sum=0;
cout"Enter the elements of the A and B matrices "endl;
for (i=0;i<3;i++)
for (i=0;i<3;i++){
cin>A[i][j];
cinB[i][j];
}
for (i=0;i<3;i++)
for (i=0;i<3;i++)
C[i][j]=A[i][j]+B[i][j];
for (i=0;i<3;i++){
for (i=0;i<3;i++){
cout<”the sum of A And B is “<C[i][j];
}
}
system("pause");
return 0;
}
Task III
Create a C++ program that uses a function to sum three digits:
#includeiostream
usingnamespace::std;
void sumDigits();
int main()
{
sumDigits();
return 0;
}
void sumDigits(){
int num1, num2, num3, sum;
cout<”Enter the three numbers to sum”<endl;
cin>num1>num2>num3;
sum = num1 + num2 + num3;
}
Task IV
Create a C++ program that uses a function to calculate simple interest:
#includeiostream
usingnamespace::std;
void si();
int main()
{
si();
return 0;
}
void si(){
//Implementation
}
Task V
Write a C++ program to perform the 4 arithmetic operations using a function for each operation
#includeiostream
usingnamespace::std;
void addition();
voidsubstraction();
void multiplication();
void division();
int main()
{
addition();
substraction();
multiplication();
division();
system("pause");
return 0;
}
void addition(){
int num1, num2, add;
cout"Enter the two numbers to sum"endl;
cin>num1>num2;
add = num1 + num2;
cout"the sum of the 2 numbers is "<add<endl;
}
voidsubstraction(){
int num1, num2, sub;
cout"Enter the two numbers to substract"endl;
cin>num1>num2;
sub = num1 - num2;
cout"the difference between the 2 numbers is "<sub<endl;
}
void multiplication(){
int num1, num2, mult;
cout"Enter the two numbers to multiply"endl;
cin>num1>num2;
mult = num1 * num2;
cout"the multiplication of the 2 numbers is "multendl;
}
void division(){
double num1, num2, div;
cout"Enter the two numbers to divide"endl;
cin>num1>num2;
div = num1 / num2;
cout"the division of the 2 numbers is "<div<endl;
}