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

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

--------------
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 4
    You should use blocks of curly braces. C ignores indenting. – Matthias Dec 20 '16 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];` – BLUEPIXY Dec 20 '16 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. – Herb Dec 20 '16 at 13:02
  • `init_world`, `next_gen` and `put_world` are arguments type mismatch. E.g `char (*matrix)[Y][X]` --> `char (*matrix)[X]` – BLUEPIXY Dec 20 '16 at 13:05
  • [why it's bad to omit curly braces](http://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces?lq=1) – Barmar Dec 20 '16 at 13:16

2 Answers2

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");
}
instance
  • 1,366
  • 15
  • 23
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.

Seb33300
  • 7,464
  • 2
  • 40
  • 57
Zinki
  • 446
  • 4
  • 10
  • The line `else printf("%c",' ');` is very poorly indented. – Jabberwocky Dec 20 '16 at 13:14
  • @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 Dec 20 '16 at 13:44
  • @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`. – Jabberwocky Dec 20 '16 at 14:11
  • @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. – Zinki Dec 20 '16 at 14:15