How to add up rows (and columns) of a 2D array
How would I write a function to add up the content of each row in a 2D array? To add the contents of each column? my code (so far):
#include <iostream>
using namespace std;
const int QUARTER = 4;
void getdata(float [][QUARTER], int);
void displaydata (float [][QUARTER], int);
void quartertotal(float [][QUARTER], int);
int main()
{
const int DIVISION = 6;
float data[DIVISION][QUARTER] = {0};
float getarray[DIVISION][QUARTER];
for (int a=0; a < DIVISION; a++)
{
for (int b = 0; b< QUART开发者_如何学编程ER; b++)
{
cout << "Enter sales for Division ";
cout<< a+1;
cout<< " Quarter ";
cout<< b+1;
cout<< ": ";
cin >> getarray[a][b];
}
}
displaydata(getarray, DIVISION);
cout << endl;
quartertotal(getarray,DIVISION);
cout << endl;
cout << endl << endl;
return 0;
}
(float getarray[][QUARTER], int divisions)
{
cout<<"\t\t\t\tQ1\tQ2\tQ3\tQ4\n";
for (int i = 0; i < divisions; i++)
{
cout << "Sales for Division " << (i+1) << " are: \t";
for (int j=0; j < QUARTER; j++)
cout << getarray[i][j] << "\t";
cout << endl;
}
}
I hope that u r comfortable with the concept of 1 dimensional arrays. Now first an overview of multi (2D, 3D etc.) dimensional arrays - The 2 dimensional arrays can be thought of as a group(or technically array) of multiple 1 dimensional arrays. Similarly the idea can be extended further i.e. a 3 dimensional array is array of multiple 2 dimensional arrays and so on.
Coming back to your question, for that a good enough code has already been posted by jacob.
You already have a way of going to each cell in the array for getting your sales for division/quarter, while your doing that you could sum up the data.
a\b 1 2 3 4
1
2
3
4
5
6
If you want to add up a row (Division) add up all the b for the same a If you want to add up a col (Quarter) add up all the a for the same b.
to get division 1
a = 1
for each b
row += getarray[a][b];
int row_total[DIVISION] = {0};
for (int a=0; a < DIVISION; a++)
{
for (int b = 0; b< QUARTER; b++)
{
row_total[a] += getarray[a][b];
}
}
I don't know what you're trying to achieve with this code. What's data
supposed to do? Also, since this is homework, you're probably limited to using arrays as opposed to std::vector
. I suppose, dynamic allocations are not possible as well --- or is that not the case?
From your comment about not understanding arrays:
Here's a way of picturing an array: Imagine a row of mail boxes. Each mail box contains a piece of mail with a number on it. To add up each of those numbers, you need to open each mailbox, read the number on the paper within, and add it to a running tally, lets say on a clipboard you carry with you. Once you have visited each mailbox and tallied its total on your clipboard, the clipboard contains the final sum of the mailboxes. The rows of mailboxes represent a 1 dimensional array. The clipboard represents a variable for tallying the values.
For a 2 dimensional array, let's imagine that you have a number of streets to visit, each street with the same number of mailboxes. To tally each street, let's use a different piece of paper per street on your clipboard for tallying. The cliboard just became an array of papers with tallies instead of just 1 for tallying each street's mailbox total.
The funny thing about programming is quite often, every day examples fit perfectly within a programming idea. That bunch of papers on a clipboard could also be considered a "Stack" of papers where you can only get to the paper on top easily. If whenever you added a piece of paper to your pile, you placed it on the bottom instead of on top, it becomes a "queue". And so on.
精彩评论