I have a program for my C++ class. I'm pretty sure i've done it correctly but i get a Tun-Time Check Failure #2 - Stack around the variable 'totalarray' was corrupted. Here is my project and program.

Create a flowchart and write a complete C++ program that declares a 2 dimensional integer array of size 10 rows and 10 columns. The program should use two nested for loops to fill the array as follows. If the row number is divisible by the column number then put the row number in the cell, otherwise put the column number in the cell. The program should then use nested for loops to print the array in table form to the screen.

I don't get an error message if I change array size to totalarray[11][11]

any help would be appreciated. Thanks

#include <iostream>

using namespace std;

int main ()

{

int sum;

int totalarray [10][10];

for (int r=1; r<=10; r++){

for (int c=1; c<=10; c++){

sum=r/c;

if (sum<1)

totalarray[r][c]=c;

else

totalarray[r][c]=r;

cout<<totalarray[r][c]<<" ";

}

cout<<endl;

}

return 0;

}

Create a flowchart and write a complete C++ program that declares a 2 dimensional integer array of size 10 rows and 10 columns. The program should use two nested for loops to fill the array as follows. If the row number is divisible by the column number then put the row number in the cell, otherwise put the column number in the cell. The program should then use nested for loops to print the array in table form to the screen.

I don't get an error message if I change array size to totalarray[11][11]

any help would be appreciated. Thanks

#include <iostream>

using namespace std;

int main ()

{

int sum;

int totalarray [10][10];

for (int r=1; r<=10; r++){

for (int c=1; c<=10; c++){

sum=r/c;

if (sum<1)

totalarray[r][c]=c;

else

totalarray[r][c]=r;

cout<<totalarray[r][c]<<" ";

}

cout<<endl;

}

return 0;

}

Arrays are zero-based, meaning that the first object in totalarray starts at totalarray[0] and the last is at totalarray[9].

You are trying to write/read totalarray[10], which is not part of the array.

Because of this,`for` loops often start at 0 and end when it reaches the size of the array.

You are trying to write/read totalarray[10], which is not part of the array.

Because of this,

Last edited on

I figured it out. I knew arrays started at 0 but I assumed that the first number you put in would go to that position. It would if my count started at 0 but it doesn't, it starts at one. So buy changing the the array to totalarray[r-1][c-1] it works. I also didn't have another nested loop to cout my array.

This is the fixed version.

#include <iostream>

using namespace std;

int main ()

{

int sum;

int totalarray [10][10];

for (int r=1; r<=10; r++){

for (int c=1; c<=10; c++){

sum=r/c;

if (sum<1)

totalarray[r-1][c-1]=c;

else

totalarray[r-1][c-1]=r;

}

}

for (int r=0; r<10; r++){

for (int c=0; c<10; c++){

cout<<totalarray[r][c]<<" ";

}

cout<<endl;

}

return 0;

}

This is the fixed version.

#include <iostream>

using namespace std;

int main ()

{

int sum;

int totalarray [10][10];

for (int r=1; r<=10; r++){

for (int c=1; c<=10; c++){

sum=r/c;

if (sum<1)

totalarray[r-1][c-1]=c;

else

totalarray[r-1][c-1]=r;

}

}

for (int r=0; r<10; r++){

for (int c=0; c<10; c++){

cout<<totalarray[r][c]<<" ";

}

cout<<endl;

}

return 0;

}

your final print out will be

12345678910

22345678910

33345678910

44445678910

55555678910

66666678910

77777778910

88888888910

99999999910

10101010101010101010

I don't understand " if the row number is divisible by the column number then put the row number in the cell" please explain

12345678910

22345678910

33345678910

44445678910

55555678910

66666678910

77777778910

88888888910

99999999910

10101010101010101010

I don't understand " if the row number is divisible by the column number then put the row number in the cell" please explain

All the numbers entering the array are row and column numbers. The row being divisible by the column is what determines what number goes into the array.

Starting with the 1 going diagonally down to 10, these numbers and all those below are all row numbers. The numbers above this diagonal line are all column numbers.

This was class homework to input variables and take them out of an array.

Starting with the 1 going diagonally down to 10, these numbers and all those below are all row numbers. The numbers above this diagonal line are all column numbers.

This was class homework to input variables and take them out of an array.

All the numbers entering the array are row and column numbers. The row being divisible by the column is what determines what number goes into the array. |

No, that isn't what determines what number goes into the array. For your implementation, the number is determined by whether the column will go into the row at least one time.

All row numbers are divisible by column numbers if you begin the numbering with 1.

I would've interpreted the instructions to mean "evenly divisible", which would result in different output.

Topic archived. No new replies allowed.