This is what is asked of me.

"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 with the even numbers beginning with 2 in the [0][0] element and filling the first row with 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 and then continuing on the second row with 22, 24, etc… The program should then use nested for loops to print the array in table form to the screen."

This is the code I have to fill the array (i think it works I have not ran it since I can't get it to print out).

//Michelle Stokes CSI130 Handout 10
//Arrays

#include <iostream>
using namespace std;

int main()
{
int ray[10][10], r, c, n;
	for(r=0; r<=10; r++)
	{
		for(c=0; c<=10; c++)
		{
n=2;			
ray[r][c]=n;
			n=n+2;
		}
	}

	return 0;
}

my problem is, I can't figure out how to print the array in table form to the screen. I thought maybe something along these lines to get it to print...

for(r=0; r<=10; r++)
	{
		for(c=0; c<=10; c++)
		{
			cout << "  " << ray[r][c];
                                 }
	}

But I don't think it looks right.

I ran it and got a screen full of twos and finally a debug error of "Run-Time Check Failure #2 - Stack around the variable 'ray' was corrupted.

Edited 7 Years Ago by mitchstokes225: added content

Just take away the <=10 and substitute in <10, going less than or equal to will overstep your array.

Also, you can accomplish the first part with just the loop variables (no need for n) ;)

Just take away the <=10 and substitute in <10, going less than or equal to will overstep your array.

Also, you can accomplish the first part with just the loop variables (no need for n) ;)

I tried this, which fixed the all 2's thing.

If i do this;

#include <iostream>
using namespace std;

int main()
{
int ray[10][10], r, c, n=2;
	for(r=0; r<=10; r++)
	{
		for(c=0; c<=10; c++)
		{
			ray[r][c]=n;
			n=n+2;
		}
	}
	for(r=0; r<10; r++)
	{
		for(c=0; c<10; c++)
		{
			cout << ray[r][c] << "  ";
		}
	}
	return 0;
}

I get 2 - 218 printed all right after each other till I get the same error. And my table isn't right.

Should be
2 4 6 8 10 12 14 16 18 20
22 24 26 28 30 32 34 36 38 40
42 44 46 48 50 52 54 56 58 60
etc..... to 200.

This question has already been answered. Start a new discussion instead.