Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My assignment is to write up 6 functions that will run in main.Currently, I\'m a

ID: 3650264 • Letter: M

Question

My assignment is to write up 6 functions that will run in main.Currently, I'm at the first part, where I need to get total of all the values in each double array.

Write a program that creates a 2D array initialized with test data. Use any data type you wish. The program should have the following functions:

1. getTotal Accepts a 2d array as its argument and returns the total of all the values in the array


This is what I got, but in just the function, there has been some errors in it. Please help


#include <iostream>

using namespace std;
const int SIZE = 10;

int getTotal(int x,int w)
{
int total = 0;
int input, number[SIZE][SIZE];

for(int i=0; i < x; i++)
{
for(int j=0; j<w; j++)
{
cin >> input;
number[i][j] = input;
}
}

for(int i=0; i<x; i++)
{
for(int j=0; j<w; j++)
{
total = total + number[i][j];
}
}
cout << "The total of all the numbers in the 2D array is " << total << "."<< endl;
return total;
}

int main()
{
int number[SIZE][SIZE];
int x,w,input;

cout << "Please input the number of rows you would like to have:" << endl;
cin >> x;
cout << "Please input the number of columns you would like to have:" << endl;
cin >> w;

cout << "Please input the values you would like to have in each section of the row by column." << endl;

for(int i=0; i < x; i++)
{
for(int j=0; j<w; j++)
{
cin >> input;
number[i][j] = input;
}
}
for(int i=0; i<x; i++)
{
for(int j=0; j<w; j++)
{
cout << number[i][j] << " ";
}
cout << endl;
}
getTotal();
return 0;
}

Explanation / Answer

Please find my comments below:

#include <iostream>

using namespace std;

const int SIZE = 10; / you have made the size as 10 what if the user wants a 20*10 matrix?


int getTotal(int x,int w)
{
int total = 0;
int input, number[SIZE][SIZE];

for(int i=0; i < x; i++)

{
for(int j=0; j{
cin >> input;
number[i][j] = input;

}

}

/this piece of code need not be added because input has already been accepted.. you will overwrite the values

for(int i=0; i<x; i++)
{
for(int j=0; j<w; j++)
{
total = total + number[i][j];
}
}
cout << "The total of all the numbers in the 2D array is " << total << "."<< endl;
return total;
}

int main()
{
int number[SIZE][SIZE];
int x,w,input;

cout << "Please input the number of rows you would like to have:" << endl;
cin >> x;
cout << "Please input the number of columns you would like to have:" << endl;
cin >> w;

cout << "Please input the values you would like to have in each section of the row by column." << endl;

for(int i=0; i < x; i++)
{
for(int j=0; j{
cin >> input;
number[i][j] = input;
}
}

for(int i=0; i<x; i++)
{
for(int j=0; j<w; j++) /dont declare i and j again the statement can be for(j=0; j<w; j++)
{
cout << number[i][j] << " ";
}
cout << endl;
}
getTotal();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote