In C++ Write a program that creates a 5 by 5, two-dimensional array that store 2
ID: 3803824 • 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<fstream>
using namespace std;
int array[5][5]; //creates a 5*5 matrix or a 2d array.
int input(istream& in=cin)
{
int x;
in >> x;
return x;
}
int main()
{
for(int i=0; i<5; i++) //This loops on the rows.
{
for(int j=0; j<5; j++) //This loops on the columns
{
array[i][j] = input(); //you can also connect to the file
}
}
cout<<"Array Display" << display()<<endl;
cout<<"calculateTotal: "<<calculateTotal() <<endl;
cout<<"totalRow " <<totalRow()<<endl;
cout<<"totaltotalColumn " <<totalColumn()<<endl;
cout<<"maximum: " <<main()<<endl;
return 0;
}
void display() {
for(int i=0; i<5; i++) //This loops on the rows.
{
for(int j=0; j<5; j++) //This loops on the columns
{
cout << array[i][j] << " ";
}
cout << endl;
}
}
int calculateTotal() {
int total=0;
for(int i=0; i<5; i++) //This loops on the rows.
{
for(int j=0; j<5; j++) //This loops on the columns
{
total +=array[i][j]; //you can also connect to the file
}
}
return total;
}
void totalColumn() {
for(int j=0; j<5; j++) //This loops on the rows.
{
int sumRow = 0;
for(int i=0; i<5; i++) //This loops on the columns
{
sumRow += array[i][j]; //you can also connect to the file
}
cout << sumRow;
cout << endl;
}
}
int maximum() {
int max=0;
for (int i = 0; i < 5; i++)
{
max = array[i][0];
for (int j = 1; j < 5; j++)
{
if (array[i][j] > max)
{
max = array[i][j];
}
}
}
cout<< max;
cout << endl;
return max;
}
void totalRow() {
for(int i=0; i<5; i++) //This loops on the rows.
{
int sumRow = 0;
for(int j=0; j<5; j++) //This loops on the columns
{
sumRow += array[i][j]; //you can also connect to the file
}
cout << sumRow;
cout << endl;
}
}
I made program let me know if any chnage requirment.