Write a full C++program to do the following processes on a two dimension array o
ID: 3910977 • Letter: W
Question
Write a full C++program to do the following processes on a two dimension array of integers. Assume the size is 4x4. 1- Fill the array with the numbers like this pattern: Note: Do not accept the values as input, or hard code it Find the math to generate these numbers and fill the array with them. 2- Reset the main diagonal elements of the array. This means all the elements which fall in the main diagonal will be 0. See this example: The result will be like this: 0111 2022 3303 4440 Note: Again find the relationship among these items in the main diagonal and reset them. 3-Create a method named Sum to find the summation of each row of the array. So pass the array and the row number that you want to calculate and return the sum of it.Explanation / Answer
#include <iostream>
using namespace std;
int sum(int a[4][4], int row)
{
int total = 0;
for(int j=0; j<4; j++)
{
total += a[row-1][j];
}
return total;
}
void print(int a[4][4])
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
int main() {
int a[4][4];
// task 1: to put row number in each row
for(int i=1; i<=4; i++)
{
for(int j=0; j<4; j++)
{
a[i-1][j] = i;
}
}
cout << "After task 1, array is as given below." << endl;
print(a);
// to reset the array
for(int i=0; i<4; i++)
{
a[i][i] = 0;
}
cout << endl << "After task 2, array is as given below." << endl;
print(a);
// CALLING sum to print the sum of each row
cout << endl << "Part 3" << endl;
for(int j=1; j<=4; j++)
{
cout << "Sum of row" << j << " is " << sum(a, j) << endl;
}
}
/*SAMPLE OUTPUT
After task 1, array is as given below.
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
After task 2, array is as given below.
0 1 1 1
2 0 2 2
3 3 0 3
4 4 4 0
Part 3
Sum of row1 is 3
Sum of row2 is 6
Sum of row3 is 9
Sum of row4 is 12
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.