Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to be able to make an array with the output format displayed like the image below. I am stuck on how to do this.
j=    1  2  3  4  5
----+----------------
I=1 |-1 -2 -3 -4 -5
2   |-6
3   |-11
4   |-16


What I have tried:

This is the code I have so far but I don't know where to go from here:
C++
#include <stdio.h>
int main()
{
    int A[4][5] =
    {
        { -1, -2, -3, -4, -5 },
        { -6 },
        { -11 },
        { -16 }
    };
    int I,j;
    for( I=0; I<4; I++ )
    {
        for( j=0; j<5; j++ )
        {
             /* this is where I am stuck*/
        }
    }
    return 0;
}
Posted
Updated 27-Sep-19 4:06am
v2

1 solution

I am not going to do all the work for you but I will give you a hint. I think your for loops are a bit off.
C++
#include <stdio.h>

#define Rows  4
#define Cols  5

int main()
{
    int A[Rows][Cols] =
    {
        { -1, -2, -3, -4, -5 },
        { -6 },
        { -11 },
        { -16 }
    };
    int i,j;

    // display top row - the column indexes

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

    // display second row - the dashes

    printf( "----+----------------\n" );

    // display third row, the first row of data

    // display subsequent rows

    for( i = 1; i < Rows; i++ )
    {
    }
    return 0;
}
That code will display the first two rows. I will leave the rest for you. Read the documentation for the printf function and its format specifiers and you should be able to figure out the rest of this.
 
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