Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a code that needs to have a format of the table below. I have most of the code written but when I run it, the first row of the array doesn't print. Please help me fix this!

Needed format:
j=   1  2  3  4  5
---+---------------
i=1|-1 -2 -3 -4 -5
2  |-6
3  |-11
4  |-16

What I am getting as an output:

j=  1  2  3  4  5
---+----------------
i=1|-6
2  |-11
3  |-16
4  |1008754248


What I have tried:

This is the code I have so far:

C++
#include <stdio.h>

int main()  {

   int A[4][5] = {
        {-1, -2, -3, -4, -5},
        {-6, 0, 0, 0, 0},
        {-11, 0, 0, 0, 0},
        {-16, 0, 0, 0, 0}
   };
   int i, j;
   printf("j=    "); {
   for (j=0; j<5; j++)  
       printf( " %d  ", j + 1);
       printf( "\n" );
       printf("----+----------------------\n");
     
       for (i=0; i<4; i++)  {
            printf("%d  |%d ", i+1, A[i][j]);
       printf("\n");     
       };
    };
    return 0;
}
Posted
Updated 27-Sep-19 21:22pm
v4

You should have edited your first question and continued with that.

Regardless, the second to last printf statement is the problem. You are trying to print A[i][j] but what is the value of the j then? It was last set by the previous for loop so it is 5 which exceeds the range of the A matrix. Set j back to 0 before that for loop and it should work better.

-edit-

Disregard my previous solution as it is not correct according to the logic as it is.

At this point, I think you should step back and re-think some things. Go back to what I gave you before. Start with that and then write code to generate the output for the remaining lines without any loops. Then study that code and see if you can see repeated logic that would lend itself to a loop.
 
Share this answer
 
v2
Comments
W Balboos, GHB 27-Sep-19 12:05pm    
At least as posted when I saw it, the loops are nested. I re-did the code wrapping and it's easier to see. Maybe this is an updated version and you're referring to the previous one ?

The formatting is really weird - printing by columns instead of rows. Somewhat of a mess - the displayed output doesn't jive with the code.
Rick York 27-Sep-19 13:40pm    
The logic is all hosed now. The loop braces are not where they should be at all. This solution just does not apply any more.
W Balboos, GHB 27-Sep-19 14:05pm    
I think we need a new icon for Q&A: something symbolizing "pull the handle to flush".
First of all, use consistent coding a&nd indentation style
Indentation style - Wikipedia[^]
The real indentation of your code is:
C++
#include <stdio.h>

int main()  {

    int A[4][5] = {
        {-1, -2, -3, -4, -5},
        {-6, 0, 0, 0, 0},
        {-11, 0, 0, 0, 0},
        {-16, 0, 0, 0, 0}
    };
    int i, j;
    printf("j=    ");
    { // this line have nothing to do here
        for (j=0; j<5; j++) // a { here would be nice
            printf( " %d  ", j + 1);
        // a } here too
        printf( "\n" );
        printf("----+----------------------\n");

        for (i=0; i<4; i++)  {
            printf("%d  |%d ", i+1, A[i][j]);
            printf("\n");
        };
    }; // this line have nothing to do here
    return 0;
}

Quote:
I have most of the code written but when I run it, the first row of the array doesn't print.

Pay attention to the value of j when you print the array.
Use the debugger to see exatly what your code is doing.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Pay attention to correct array indexing.
The following program
C
#include <stdio.h>

int main()
{

  int a[4][5] =
  {
    {-1, -2, -3, -4, -5},
    {-6, 0, 0, 0, 0},
    {-11, 0, 0, 0, 0},
    {-16, 0, 0, 0, 0}
  };

  int i, j;

  printf(" \\ j ");
  for (j=0; j<5; j++)
    printf( " %d  ", j + 1);
  printf( "\n" );
  printf("i \\ --------------------\n");

  // first line is the special one
  printf("1  | ");
  for (j=0; j<5; j++)
    printf("%d  ", a[0][j]);
  printf("\n");

  for (i=1; i<4; i++)
    printf("%d  | %d\n", i+1, a[i][0]);
  return 0;
}
outputs
 \ j  1   2   3   4   5  
i \ --------------------
1  | -1  -2  -3  -4  -5  
2  | -6
3  | -11
4  | -16
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900