Hello, I'm having trouble with my array assignment. The instructions are
Create a integer type two-dimensional array of 4 rows and 3 columns
Populate the two-dimensional array with values entered by the user, use a function for this.
Print the array in a spreadsheet format, also print the sum of the values of each row. The out put must be:
Row Totals
value01 value02 value03 Sum01
value04 value05 value06 Sum02
value07 value08 value09 Sum03
value10 value11 value12 Sum04

The column's values and Sums must be right justified. Use the setw() function.

Use a function to print the values. Call this function printValues().
Must use loops to enter values, traverse the array, calculate sum, and print the values in the array.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` `````` #include #include using namespace std; int const ROWS = 3; int const COLUMNS = 4; void userInputValue(); void printValues(); int main() { userInputValue(); printValues(); return 0; } void userInputValue() { int index = 0; int myArray[ROWS][COLUMNS]; cout << "Enter 12 numbers" << endl; for(int row = 0; row < ROWS; row++) { for(int column = 0; column < COLUMNS; column++) { cout << ++index << ". Enter a number: "; cin >> myArray[row][column]; } } } // End of method userInputValue void printValues() { int myArray[ROWS][COLUMNS]; cout << "The numbers you entered were:" << endl; for(int row = 0; row < ROWS; row++) { for(int column = 0; column < COLUMNS; column++) { cout << myArray[row][column] << ' '; cout << endl; } } } // End of method printValues ``````

SO far this is what I have and whenever it prints I get
"Row Totals
0
1
2 "

Edit; The "row totals" must be on top of the sums section not on top of the values.
Last edited on
Hello av16352,

Right the first problem I see is in the "userInputValue" function. Here you define your array, but when the function is finished and looses scope the variables defined in the function are destroyed. So when you get back to "main" you have no array.

Give me a few minutes and I will show you what you could do.

Andy
Hello av16352,

This should give a better idea of what the program should do.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include #include using namespace std; int const ROWS{ 4 }; // <--- You have the size numbers backwards. int const COLUMNS{ 3 }; void userInputValue(int myArray[][COLUMNS]); void printValues(const int myArray[][COLUMNS]); int main() { //int myArray[ROWS][COLUMNS]; // <--- Original used for normal runs. int myArray[ROWS][COLUMNS] // <--- Used for testing. { { 10, 12, 81 }, { 53, 1, 34 }, { 1, 27, 98 }, { 33, 98, 26} }; //userInputValue(myArray); // <--- Commented for testing. printValues(myArray); return 0; // <--- Not required, but makes a good break point. } void userInputValue(int myArray[][COLUMNS]) { int index = 0; cout << "Enter 12 numbers" << '\n'; for (int row = 0; row < ROWS; row++) { for (int column = 0; column < COLUMNS; column++) { cout << ++index << ". Enter a number: "; cin >> myArray[row][column]; } } } // End of method userInputValue void printValues(const int myArray[][COLUMNS]) { cout << "\nThe numbers you entered were:" << "\n\n"; for (int row = 0; row < ROWS; row++) { for (int column = 0; column < COLUMNS; column++) { //cout << myArray[row][column] << ' '; std::cout << std::setw(3) << myArray[row][column]; // <--- Add to this for the sum. } cout << '\n'; } } // End of method printValues ``````

Now all I need to know is what the other functions look like.

Andy
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````#include #include using namespace std; int const ROWS = 4; int const COLUMNS = 3; void userInputValue(); void printValues(); int main() { userInputValue(); printValues(); return 0; } void userInputValue() { int index = 0; int myArray[ROWS][COLUMNS]; cout << "Enter 12 numbers" << endl; for(int row = 0; row < ROWS; ++row) { for(int column = 0; column < COLUMNS; ++column) { cout << ++index << ". Enter a number: "; cin >> myArray[row][column]; } } } // End of method userInputValue void printValues() { int myArray[ROWS][COLUMNS]; int sum = 0; cout << right << setw(30) << "Row Totals" << endl; for(int row = 0; row < ROWS; row++) { for(int column = 0; column < COLUMNS; ++column) { sum = sum + myArray[row][column]; } cout << row << ' '; cout << endl; sum = 0; } } // End of method printValues ``````

Thank you and I'm sorry I didn't realize I copied my old attempt. This was my latest attempt which is what I needed help on.
Last edited on
Hello av16352,

I still refer you to my first post. Much of what is there still applies. Like the blank lines, the indenting and overall formatting. It makes it easier to read this way for both of us.

Actually I was think the the "sum" would be a function. It does make sense, but this can be made to work.

I will load it up and give it a test.

Andy
Hello av16352,

Your "print" function is mostly usable, but needs some work.

Running you version I get this output:
 ``` Row Totals Total -858993460-858993460-858993460 1717986916 -858993460-858993460-858993460 1717986916 -858993460-858993460-858993460 1717986916 -858993460-858993460-858993460 1717986916 ```

Using the test numbers I set up I get this:
 ``` Row Totals 10 12 81 103 53 1 34 88 1 27 98 126 33 98 26 157 ```

Although I would put "Row Totals" on 2 lines.

In the "print function the "sum" works, but you forgot to print the numbers in the array. Also all you need is:
`sum += myArray[row][column];`.

By defining "sum" in the outer for loop it is destroyed each time that loop ends and the remade next time you enter. A little less work this way.

What i ended up with is:
 ``12345678910111213141516171819202122`` ``````void printValues(const int myArray[][COLUMNS]) { //int myArray[ROWS][COLUMNS]; // <--- Array is empty. It only has garbage values. cout << right << setw(20) << "Row Totals" << endl; // <--- setw() is default right to start with. for (int row = 0; row < ROWS; row++) { int sum{}; // <--- The (0)zero is not needed. Moved to here. for (int column = 0; column < COLUMNS; ++column) { std::cout << std::setw(3) << myArray[row][column]; // <--- Added. sum += myArray[row][column]; // <--- Changed. } cout << std::setw(6) << sum<<'\n'; // <--- Changed. Made the setw larger than needed. Used this for spacing. //sum = 0; // <--- No longer needed. } } // End of method printValues ``````

The only change I would make to my first code is on line 14. You need to put {}s before the semi-colon.

Andy
Andy, what exactly do the braces {} do next to sum? We have not learned that in class and if we were to use it, I'd have to explain it.

However; I tried to edit my code a bit and see if I can fix where I went wrong but I'm still running into some issues. This is what I have so far;

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````#include #include using namespace std; int const ROWS = 4; int const COLUMNS = 3; void userInputValue(int myArray[ROWS][COLUMNS]); void printValues(const int myArray[ROWS][COLUMNS]); int main() { int myArray[ROWS][COLUMNS]; userInputValue(myArray); printValues(myArray); return 0; } void userInputValue(int myArray[ROWS][COLUMNS]) { int index = 0; cout << "Enter 12 numbers" << endl; for(int row = 0; row < ROWS; ++row) { for(int column = 0; column < COLUMNS; ++column) { cout << ++index << ". Enter a number: "; cin >> myArray[row][column]; } } } // End of method userInputValue void printValues(const int myArray[ROWS][COLUMNS]) { cout << right << setw(20) << "Row Totals" << endl; for (int row = 0; row < ROWS; row++) { int sum; for (int column = 0; column < COLUMNS; ++column) { cout << setw(3) << myArray[row][column]; sum = sum + myArray[row][column]; } cout << setw(6) << sum<<'\n'; } } // End of printValues ``````

Output I get;

 ``` Row Totals 10 2 3-434904305 4 5 6-434904290 7 8 9-434904266 90 80 70-434904026 ```
I didnâ€™t add a value to int sum after looking at it.

After putting int sum = 0; The programs works!
Hello av16352,

The {}s, known as the uniform initializer, but may have a different name these days, became available in the 2011 standards and after. Saying `int sum = 0;` you can now say `int sum{};` and it does the same thing. The empty {}s allows the compiler to choose the best form of (0)zero for the variable based on the variable's type.

A "char" would get (\0), "int" types would get (0) and floating point types would get (0.0). The "std::string" and other container types are empty when defined and do not need initialized.

You can also put a value between the {} as I did with the constant variables at the beginning of the file.

Since it is available I find it much easier to type than all the " = 0" or something.

Just because it has not been covered in class does not mean that you can not learn it somewhere else.

By not initializing "sun" the storage space is reserved for the variable, but whatever happens to be in the storage space is trying to be used as an "int", but the value of that is considered garbage. Hence that really small negative number you get for the totals.

Variables like "sum" or "total" should ALWAYS be initialized or set to (0)zero before they are used. Also it is ALWAYS a good idea to initialize your variables when defined.

The code looks fine as I read it. Not tested yet.

In the "print" function you have: `sum = sum + myArray[row][column];` The shorter way to write that , and it does the same thing, is: `sum += myArray[row][column];`. This is similar to `++index` with the difference being that you are taking the value of "sum" and adding "myArray[row][column]" instead of just adding 1.

If yo do not understand the "setw" function let me know.

Andy
Hello av16352,

With a little adjustment to the "input" and "print" functions I have the output looking like this:
 ``` Enter 12 numbers: 1. Enter a number: 10 2. Enter a number: 12 3. Enter a number: 81 4. Enter a number: 53 5. Enter a number: 100 6. Enter a number: 42 7. Enter a number: 100 8. Enter a number: 1234 9. Enter a number: 98 10. Enter a number: 33 11. Enter a number: 95 12. Enter a number: 26 Row Totals 10 12 81 103 53 100 42 195 100 1234 98 1432 33 95 26 154 ```

I find it nicer than when all lines run together.

Andy
Sorry for the late reply but thank you for teaching me about uniform initializers! Yes, I overlooked the part where I didn't assign sum a value so that was my mistake.