0

I'm having problems with a little beginners Programm in C, specifically with the Array output.

#include <stdio.h>
#include <stdlib.h>

#define X 10
#define Y 10

void init_world(char (*)[Y][X]);
void next_gen(char (*)[Y][X], char (*)[Y][X]);
void put_world(char (*matrix)[Y][X]);

int main(void)
{
    int x=0;
    char welt1[X][Y];
    char welt2[X][Y];
    init_world(welt1);
    put_world(welt1);
    do
    {
        next_gen(welt1,welt2);
        put_world(welt1);
        x++;

    }while((welt2!=welt1)and (x<10));
    getchar();
    return 0;
}

void init_world(char (*welt)[Y][X])
{
    ...
}


void next_gen(char (*zelle)[Y][X], char (*neu)[Y][X])
{
    ...
}

void put_world(char (*matrix)[Y][X])
{
    int y, x;
    for(y=0; y<X; y++)
        for(x=0; x<Y; x++)
            if(matrix[y][x] != 0)
                printf("%c",'*');
            else printf("%c",' ');
        printf("\n");
    printf("\n--------------\n");
}

The last function should print the array elements. This should happen in a maxtrix of '*' or ' ' but it keeps printing these just in lines

******* *****   *************** ***     *** *** *** ***  
*********************** *** *** *** *   *

--------------
5
  • 4
    You should use blocks of curly braces. C ignores indenting. Commented Dec 20, 2016 at 12:57
  • 1
    (welt2!=welt1) always true. Also char welt1[X][Y]; char welt2[X][Y]; --> char welt1[Y][X]; char welt2[Y][X]; Commented Dec 20, 2016 at 13:00
  • If you want your program to print the array, you have to code it to do so. Right now all the code says is print an '*' or a space. Commented Dec 20, 2016 at 13:02
  • init_world, next_gen and put_world are arguments type mismatch. E.g char (*matrix)[Y][X] --> char (*matrix)[X] Commented Dec 20, 2016 at 13:05
  • why it's bad to omit curly braces Commented Dec 20, 2016 at 13:16

2 Answers 2

1
void put_world(char (*matrix)[Y][X])
{
    int y, x;
    for(y=0; y<X; y++)
    {
        for(x=0; x<Y; x++)
            if(matrix[y][x] != 0)
                printf("%c",'*');
            else printf("%c",' ');
        printf("\n");    //Out of scope of first for loop previously (keep it in).
    }
    printf("\n--------------\n");
}
Sign up to request clarification or add additional context in comments.

1 Comment

The line else printf("%c",' '); is very poorly indented.
1

Your printf("\n") is not part of your for loop.

    void put_world(char (*matrix)[Y][X])
    {
        int y, x;
        for(y=0; y<X; y++)
        {
            for(x=0; x<Y; x++)
                if(matrix[y][x] != 0)
                    printf("%c",'*');
                else printf("%c",' ');
            printf("\n");
        }
        printf("\n--------------\n");
    }

Should do it.

4 Comments

The line else printf("%c",' '); is very poorly indented.
@MichaelWalz The indentation is correct. As the else is part of the if-statement, it is part of the inner for loop and thus correctly indented as it stands (on level with the if).
@Zinki what I meant is that printf("%c",' '); should be on the line following the else at the same indentation level as printf("%c",'*');. Somewhat like this: if(matrix[y][x] != 0)\n printf("%c",'*');\nelse\n printf("%c",' ');\n.
@MichaelWalz Ah I see, yes, that would add to the code readability, but considering the question I wanted to make the minimum number of changes to OPs code to make it work. If we're talking aesthetics and readability, there are a number of further changes I'd make.

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.