Problem Objective You are required to write an interactive object-oriented progr
ID: 3712286 • Letter: P
Question
Problem Objective
You are required to write an interactive object-oriented program which performs the following tasks:
Reads 16 integers into a two-dimensional array.
Prints the content of the array.
Notice (Requirements):
For each of the above operation, you must write a function to perform the required task. Each function must receive the array as its parameter and finds (returns) an appropriate answer.
1-Finds the sum of the first row.
2-Finds the sum of the first column.
3-Finds sum of the main diagonal (right main diagonal).
4-Prints the results using an appropriate format
Explanation / Answer
ANS:-
Given that,
Example: If elements of matrix are:
1 2 3
4 5 6
7 8 9
Output:
Sum of row 1 = 6
Sum of row 2 = 15
...
...
Sum of column 3 = 18
For rows , average =sum/no. of rows
for column, average = sum/no. of column
program:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const int numRows = 10;
const int numCols = 10;
int val[numRows][numCols];
int i, j;
double sum = 0, avgR, avgC;
void randNum() // Creates 10x10 array of random nums 0-9
{
srand(2622); // random seed
for (i = 0; i < numRows; i++) // Creates random rows
{
cout << endl << endl;
for (j = 0; j < numCols; j++) // Creates random columns
{
val[i][j] = rand() % 10;
cout << setw(6) << val[i][j];
}
}
cout << endl << endl;
}
void randAvg() // finds average of each row and column
{
for (i = 0; i < numCols; i++)
{
sum += val[i][j];
}
avgR = sum / numRows;
cout << " " << avgR << endl;
}
int main() // calls each function
{
randNum();
randAvg();
For Finding & Printing Sum Of Diagonal Elements Of Matrix(right main diagonal):
/* Declaration Of Header Files */
# include <iostream.h>
# include <conio.h>
/* Start Of Main Program */
void main()
{
/* Declaration Of Variables */
int i, j, r = 0, c = 0;
int a [ 10 ][ 10 ];
clrscr();
/* Asking For The Input From User */
cout << " Enter Number Of Rows & Columns Of 2D Array [ Matrix ] : ";
cin >> r >> c ;
// Accepting Values Of 2D Array [ Matrix ]
cout << " Enter " << r * c << " Values for 2D Array : ";
for ( i = 0; i < r; i++ )
{
for ( j = 0; j < c; j++ )
{
cin >> a [ i ][ j ];
}
}
// Printing Values Of 2D Array [ Matrix ]
cout << " Values Of 2D Array [ Matrix ] Are : ";
for ( i = 0; i < r; i++ )
{
cout << " ";
for ( j = 0; j < c; j++ )
{
cout << a [ i ][ j ];
}
}
/* Source Code For Computing Sum Of Diagonal Elements If & Only If Rows & Columns Are Equal */
sum=0;
if(r==c)
{
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
if(i+j==0 || i+j==2 || i+j==4)
{
sum=sum+a[i][j];
}
}
}
/* Printing The Output Onto The Screen/Console */
cout<<" Sum Of Diagonal Elements Of Array Is : "<<sum;
}
else
{
cout<<" Addition Is Not Possible";
}
getch();
}
/* End Of Main Program */
Output :
Enter Order For Array A : 3 3
Enter 9 Values For Array :
1 2 3 4 5 6 7 8 9
Array A Is :
1 2 3
4 5 6
7 8 9
Sum Of Diagonal Elements Of Array Is : 15
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.