0

I started to code a really simple task but I keep getting the wrong output. Help me, I'm desperate! The problem is: I have to print a table 4*10. The numbers in the table go from 1 to 40 and they are ascending- the table looks like this:

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40

My task is to replace the every other multiple of 3 ( 3, 9, 15) is by the number 2. Every multiple of 5 that hasn't already been used is replaced by the number 3. Everything else is replaced by 1.

 #include<cstdio>
#include<cstring>


using namespace std;

    int table[10][4];
    int i,j;
    int br;

int main()
{
    br=1;


    for(i=0;i<4;i++)
    for(j=0;j<10;j++)
    {

    if(br%6==3) table[i][j]=2;
    else if(br%5==0 && br%6!=3) table[i][j]=3;
    else table[i][j]=1;


    br++;
    }


    for(i=0;i<4;i++)
    {for(j=0;j<10;j++)
    {
    printf("%d",table[i][j]);
    }
    printf("\n");
    }



return 0;
}

The output I keep getting :
1121111121
1111211111
2111112131
1121311123

The output I should get:
1121311123
1111211113
2111312113
1121311123

4
  • 1
    Enable all warnings and debugging options (e.g. compile with g++ -Wall -g) and use the debugger (e.g. gdb) Commented Dec 14, 2013 at 20:16
  • "that hasn't already been used"? Commented Dec 14, 2013 at 20:18
  • anyway, note that the table-ness is a property of the presentation only, and that your logic only needs to deal with the sequence of consecutive integers starting with 1 and ending with 40. Commented Dec 14, 2013 at 20:19
  • For example, 15 is replaced by 2 (it is a multiple of 3), and therefore should not be replaced by 3, although it is the multiple of 5. Commented Dec 14, 2013 at 20:20

2 Answers 2

1

One problem is that you are using your indices backwards with respect to how you declared your array:

int table[10][4];

for(i=0;i<4;i++)
for(j=0;j<10;j++)
{
   if(br%6==3) table[i][j]=2;
   else if(br%5==0 && br%6!=3) table[i][j]=3;
   else table[i][j]=1;

See how your (i) variable in the for loop is set to go 0,1,2,3 and your (j) variable in the inner loop is set to go (0,1,2,3,...,9) ? Then you set items in your array using table[i][j], which means that you'll be setting table[0][0] through table[3][9], but your table is declared as table[10][4].

The solution would be either to change your table declaration to be table[4][10], or alternatively change your instances of table[i][j] to be table[j][i] instead. Either way will work, as long as your for-loop ranges match your table's dimensions.

Sign up to request clarification or add additional context in comments.

Comments

1

You access the array in the wrong order. Instead of

table[i][j];

you should write

table[j][i];

whenever accessing your table as i denotes your row and j your column (as long as you name them consistently).

Alternatively swap the indexing in the table declaration, i.e.

int table[4][10];

instead of

int table[10][4];

The output is then correct.

1 Comment

Or simply define the array as int table[4][10]; instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.