### Multidimensional arrays program not working

Hi, I am new to multidimensional arrays, and I am struggling to understand why this program of mine isn't working as it should. Karen and Murphy receive each day a bonus, for a total of 5 days. In the end I'd like to display the total sum of Karen's bonuses and of Murphy's. However the program abruptly stops after I've inserted the first 4 figures.
 ``1234567891011121314151617`` `````` # include using namespace std; int main() { int bonus[1][4]; int k=0,m=0, i=0; while(i<4){ cin>>bonus[0][i]; k = k+bonus[0][i]; cin>>bonus[1][i]; m = m+bonus[1][i]; i++; } cout<<"Karen's total tally is: "<

> int bonus[1][4];
You have two people, so this should be
int bonus[2][4];
You have 2 people and 5 days so it should be int bonus[2][5];
Thanks!
Last edited on
Hello Theloneinteger,

After seeing what salem c and Thomas1965 have said I came up with this:
 ``12345678910111213141516171819202122232425262728`` ``````# include using namespace std; int main() { constexpr int MAXROW{ 1 }; // <--- Should be 2. constexpr int MAXCOL{ 4 }; // <--- Should be 6. int bonus[MAXROW][MAXCOL]; int totalKaren{}, totalMurphy{}, col{}; while (col < 4) { // <--- Needs a prompt. cin >> bonus[0][col]; totalKaren += bonus[0][col]; // <--- Needs a prompt. cin >> bonus[1][col]; // <--- Row out of bounds. totalMurphy += bonus[1][col]; // <--- Row out of bounds. col++; } cout << "Karen's total tally is: " << totalKaren << endl; cout << "Murphy's total tally is: " << totalMurphy << endl; }``````

The blank lines help the readability of the code and make it much easier to follow. A good variable name also helps, I am not saying what I did is a good name, but it is just to give you an idea, this will be helpful in the future.

Lines 17 and 21 are a shorter way of writing what you did.

C++ is one of several (0) zero based languages, so when you write `int bonus[1][4];` you are saying that you have 1 row (0) zero and 4 columns 0 - 3. When you get to `cin>>bonus[1][i];` the row is outside the boundaries of the array and you do not know what memory location you are writing to. That is where your program crashes.

When it comes to {}s do not mix them. Pick a style and be consistent with its use. https://en.wikipedia.org/wiki/Indentation_style#Brace_placement_in_compound_statements for some ideas. When it comes to the code the first goal is to make it as easy to read and follow as you can, The first benefit it to you.

I hope this gives you some ideas that you can use.

Andy
Andy thanks for your tremendous answer. I've only got one question, why have the variables totalKaren, totalMurphy and col got {} after them when declared? Is it some sort of way to avoid writing totalKaren= 0 when declaring?
Hello Theloneinteger,

Yes. That is available from C++11 on called the uniform initializer. Empty {}s set the variable to (0) zero based on the type of the variable.

In a way it is a shorthand method like +=, -= etc. and ++ or --.

You can also use it with C style arrays `int arr{MAXSIZE]{}` will set the first element to (0) and all the rest follow.

I have also seen and used it in `for (int i{};... ` and seen it used in the body to reset a variable to (0).

std::string, std::vector and other container classes are empty when defined and do not need initialized unless you want to give them a starting value then just put it between the {}. `std::string str{ "initial value" };`

Andy
 why have the variables totalKaren, totalMurphy and col got {} after them when declared?

C++11 introduced two concepts known as "uniform initialization" and "initializer lists."

https://mbevin.wordpress.com/2012/11/16/uniform-initialization/
Thanks, it is much more clear now