-2

I am a new CS student and am having a hard time understanding 2 dimensional arrays. Right now I am just trying to print it out 1 row at a time.

This is my code

#include <stdio.h>

#define N 3

int main(void)

{
    int array[N][N], i, j, rows[N], cols[N];


    for(i=0;i<N;i++)
    {
        printf("Enter row %d: ", i+1);
        for(j=0;j<N;j++)
        scanf("%d", &array[i][j]);
    }
    printf("The 5 rows you entered are: \n");
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            printf("%d", array[i][j]);
            printf("\n");
        }
    }
    printf("\n");
    return 0;
}

I would like to have the program print out:

1 2 3
1 2 3
1 2 3

If I add the \n it prints out:

1\n
2\n
3\n
1\n
2\n
3\n
1\n
2\n
3\n

Imagine the \n's as what the screen is printing out, I am trying to represent what the code prints out. I am having trouble getting this box to print out exactly what I am typing in. It won't let me hit enter over and over to represent what is actually on the command screen.

without the /n it prints out

123123123

I am also trying to add rows and columns. I managed to find some code, and understand most of it, except for one line. This is the code.

#include <stdio.h>

int main(void)
{
    int i, j, array[5][5], rows[5],cols[5];

    for(i=0;i<5;i++)
    {
        printf("Enter row %d: ", i+1);
        for(j=0;j<5;j++)
            scanf("%d", &array[i][j]);
    }
    **for(i=0;i<5;rows[i]=cols[i]=0,i++);**
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            rows[i]=rows[i]+array[i][j];
            cols[j]=cols[j]+array[i][j];
        }
    }
    printf("\nRow Totals:    ");
        for(i=0;i<5;i++)
    printf("%d\t", rows[i]);
    printf("\nColumn Totals: ");
    for(i=0;i<5;i++)
        printf("%d\t", cols[i]);
    printf("\n");

    return 0;
}

for(i=0;i<5;rows[i]=cols[i]=0,i++);
is the line I do not understand. First off, I don't really understand the rows[i]=cols[i]=0,i++. And second off, I don't understand why there is a semicolon after it. I thought for statements did not use semicolons, but the program does not work correctly without this line, or without the semicolon. When I try to use it without this line of code, it gives me crazy answers, I am assuming because the elements in the array are not set to 0 and this line of code sets the elements of the arrays to 0. Can someone give me another line of code that might be more understandable for someone relatively new to C? And maybe explain to me why it uses a semicolon?

mort
  • 12,988
  • 14
  • 52
  • 97
Mike
  • 318
  • 2
  • 4
  • 13

3 Answers3

4

I would like to have the program print out: 1 2 3\n 1 2 3\n 1 2 3\n

Do:

for(i=0;i<N;i++)
{
    for(j=0;j<N;j++)
    {
        printf("%d", array[i][j]);

    }
    printf("\n");
}


**for(i=0;i<5;rows[i]=cols[i]=0,i++);** 

is for sure syntax error. I guess you added ** for marking the statement. However,

for(i=0;i<5;rows[i]=cols[i]=0,i++);

is a valid C statement. A for-loop can be split into the form

for(initilization;condition;increment)

The semi-colon separates the three parts of the for-loop. In your case the for-loop increment part is further divided to multiple statements using commas like below.

for(initialization;condition;do something useful here, update condition variable);

The purpose of the code is to initialize the row and column sums to zero before actually finding the sum. Note the comma in the increment part. It allows you to add multiple statements. Similarly you could use the comma to have multiple initializations or multiple conditions.

The semi-colon after the for-loop is a sequence point. Here, note that you don't have statement/compound-statement for the for-loop. In plain and simple language the semi-colon tells the for-loop to do nothing. But you initialize the row and column sums to zero indirectly inside the for-loop which itself is smart.

It is clear that the usage is not very straight-forward. If you don't understand these kind of statements, I suggest you to go through a good C programming book.

sjsam
  • 21,411
  • 5
  • 55
  • 102
2

for loops have four parts. You're probably used to loops like:

for (int i = 0; i < 10; i++)
{
    printf("Hello!");
}

or

for (int i = 0; i < 10; i++)
    printf("Hello!");

The first part (setup), "int i = 0", runs at the start of the loop. The second part (test expression), "i < 10", is the condition, is checked before each iteration, and ends the loop if it evaluates to false. The third part (increment), "i++", runs at the end of each iteration, and is not evaluated. The fourth part (body), runs after the second part and before the third.

In the setup and increment sections of the for loop, you can use commas to separate multiple statements. It wouldn't make sense to use commas in the test expression, as it needs to evaluate to either true or false. It also wouldn't make sense to use commas in the body, as you can just use semicolons there.

There's no reason why you can't use the loop itself to do your work for you. In the example you provided:

 for(i=0;i<5;rows[i]=cols[i]=0,i++);

You already understand what the loop is doing (setting all values in the rows and cols arrays to 0.) It's just doing it in the increment part of the loop, rather than in the loop's body. It's also taking advantage of the fact that the assignment operation evaluates to the value being assigned. So, "cols[i] = 0" evaluates to "0", which rows[i] is then assigned to.

However, for loops always treat the next statement as their body. If you want to tell the loop not to do anything at all, then you need to place an empty statement (a semicolon with nothing in front of it) after the loop so that it knows not to do anything else.

If you remove the semicolon, then it will assume that the next line is the body of loop, which gives this behavior:

for(i=0;i<5;rows[i]=cols[i]=0,i++)
{
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            rows[i]=rows[i]+array[i][j];
            cols[j]=cols[j]+array[i][j];
        }
    }
}

For the actual solution to your problem, use sjsam's answer.

Luke Pillar
  • 164
  • 1
  • 4
1

Minor adjustment to the output logic

printf("The %d rows you entered are: \n", N);
for(i=0;i<N;i++)
{
    for(j=0;j<N;j++)
    {
        if (j != 0)
        {
            putc(' '); /* add space if we are not the first column */
        }
        printf("%d", array[i][j]);
    }
    putc ('\n'); /* new line after each row is complete */
}
printf("\n");
Stian Skjelstad
  • 2,277
  • 1
  • 9
  • 19