In C++ Write a program that creates a 5 by 5, two-dimensional array that store 2
ID: 3803336 • Letter: I
Question
In C++
Write a program that creates a 5 by 5, two-dimensional array that store 25 integers and the program will call five functions.
• Function display(): the program will display all the integers (in the 5 by 5 format)
• Function calculateTotal(): the program will return the total of the 25 integers
• Function totalRow(): returns and displays a 5-element array with each of the element showing the total of all the elements of the same row in the 5 by 5 array.
• Function totalColumn(): returns and displays a 5-element array with each of the element showing the total of all the elements of the same column in the 5 by 5 array
• Function maximum(): returns the largest value in the 5 by 5 array
Explanation / Answer
#include <iostream>
#include <cstdlib>
using namespace std;
void display(int n,int **a)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j]<<' ';
}
cout<<endl;
}
}
int calculateTotal(int n,int **a)
{
int sum=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
sum+=a[i][j];
}
}
return sum;
}
int *totalRow(int n,int **a)
{
int *b = (int *)malloc(sizeof(int)*5);
for(int i=0;i<n;i++)
{
b[i]=0;
for(int j=0;j<n;j++)
{
b[i]+=a[i][j];
}
}
return b;
}
int *totalColumn(int n,int **a)
{
int *b = (int *)malloc(sizeof(int)*5);
for(int i=0;i<n;i++)
{
b[i]=0;
for(int j=0;j<n;j++)
{
b[i]+=a[j][i];
}
}
return b;
}
int maximum(int n,int **a)
{
int max = a[0][0];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
}
}
}
return max;
}
int main()
{
return 0;
}