-4

Screenshot

Here's the code. It's supposed to generate and output a 2D array but, for some reason, messes up the value for 9x10 (90) and causes the program to crash. I've attached a screenshot.

Any advice? Also, how can I make the numbers line up nicely into a neat grid?

#include <iostream>

using namespace std;

int main ()
{
    int values[10][10];
    for(int i=1;i<=10;i++)
    {
        for(int j=1;j<=10;j++)
        {
            values[i][j]=i*j;
            cout<<\t<<values[i][j]<<flush;
        }
    cout<<endl;
    }

return 0;
}
ildjarn
  • 62,044
  • 9
  • 127
  • 211
Shehryar
  • 23
  • 4
  • 6
    You do know that indexes in arrays in C++ are zero based? So for an array of ten elements the indexes go from `0` to `9` (inclusive). If you *don't* know that then you need to [find a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start over. – Some programmer dude Jul 18 '16 at 23:08
  • I'm aware, but since I wanted to use the variables i and j to actually calculate the values (which go from 1x1 to 10x10) I went with i=1 and j=1. This doesn't seem to be the cause of the error, though. – Shehryar Jul 18 '16 at 23:13
  • 1
    @Shehryar it is, in fact, the cause of the error. The values you show to the user (1...10) differ from the values you need to store data in the array (0..9). Fortunately one can be derived from the other, but given your current requirements they cannot be the same. – fbrereto Jul 18 '16 at 23:18
  • @Shehryar Why do you need an array, actually? – LogicStuff Jul 18 '16 at 23:18
  • 2
    That *is* the cause of your error. You can't just ignore the bounds of your array because they don't suit your other purposes... – JBentley Jul 18 '16 at 23:18
  • @fbrereto I changed the int i and j to start from 0 to accurately represent the index bounds--and changed the calculation to (i+1) and (j+1). Same issue, still. – Shehryar Jul 18 '16 at 23:21
  • @JBentley didn't work unfortunately. – Shehryar Jul 18 '16 at 23:21
  • @LogicStuff it's just an exercise from the tutorial I'm using to learn C++ – Shehryar Jul 18 '16 at 23:22
  • @Shehryar Since you indicated that Wanderer's answer worked, my guess is that you forgot to change the upper bound of your for loops from 10 to 9 when you made your modifications. – JBentley Jul 18 '16 at 23:28
  • @JBentley strangely enough despite editing the bounds of the loop and array and modifying the calculation to (i+1)*(j+1), I had the same issue. A fresh cpp file saved the day somehow. thanks. – Shehryar Jul 18 '16 at 23:32

3 Answers3

3

Change it to

for( int i=0;i<10;i++)
{
    for(int j=0;j<10;j++)
    {
        values[i][j] = (i + 1)*(j + 1);
    }
}

edited to include the body too.

Wanderer
  • 307
  • 1
  • 12
2

Zero based indexes, equally spaced values in console, values starting at 1:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int values[10][10];
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            values[i][j] = (i + 1)*(j + 1);
            cout << setw(5) << values[i][j];
        }
        cout << endl;
    }
    return 0;
}
  • @Shehryar Never say never. For one thing you'll upset Justin Beiber, but more importantly, there are origin 1 programming languages. – user4581301 Jul 18 '16 at 23:39
1

arrays start at 0 not 1

using namespace std;

int main ()
{
    int values[10][10];
    for(int i=1;i<=10;i++)
    {
        for(int j=1;j<=10;j++)
        {
            values[i-1][j-1]=i*j; // <<<<<<<<<<
            cout<<'\t'<<values[i-1][j-1]<<flush;
        }
    cout<<endl;
    }

return 0;
}

edit: output

Success time: 0 memory: 3412 signal:0
    1   2   3   4   5   6   7   8   9   10
    2   4   6   8   10  12  14  16  18  20
    3   6   9   12  15  18  21  24  27  30
    4   8   12  16  20  24  28  32  36  40
    5   10  15  20  25  30  35  40  45  50
    6   12  18  24  30  36  42  48  54  60
    7   14  21  28  35  42  49  56  63  70
    8   16  24  32  40  48  56  64  72  80
    9   18  27  36  45  54  63  72  81  90
    10  20  30  40  50  60  70  80  90  100
pm100
  • 48,078
  • 23
  • 82
  • 145