Benjamin
Benjamin

Reputation: 345

Why isn't my output changing with each loop iteration?

I am creating a simple C application using GCC in Ubuntu based on Conway's Game of Life. I have basically all the code that I need, but I'm having trouble with one tiny aspect of the code.

My C source file:

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

#define HEIGHT 32
#define WIDTH 32
#define COMPASS 8
#define SPACE '.'

unsigned long mask = 0x80000000;
unsigned long row[WIDTH] = { 0 };
unsigned long copy[WIDTH] = { 0 };

int northWest(int rowNum) {
    copy[rowNum - 1] = row[rowNum - 1] >>= 1;
    return copy[rowNum - 1] & row[rowNum];
}

int north(int rowNum) {
    copy[rowNum - 1] = row[rowNum - 1];
    return copy[rowNum - 1] & row[rowNum];
}

int northEast(int rowNum) {
    copy[rowNum - 1] = row[rowNum - 1] <<= 1;
    return copy[rowNum - 1] & row[rowNum];
}

int west(int rowNum) {
    copy[rowNum] = row[rowNum] >>= 1;
    return copy[rowNum] & row[rowNum];
}

int east(int rowNum) {
    copy[rowNum] = row[rowNum] <<= 1;
    return copy[rowNum] & row[rowNum];
}

int southWest(int rowNum) {
    copy[rowNum + 1] = row[rowNum + 1] >>= 1;
    return copy[rowNum + 1] & row[rowNum];
}

int south(int rowNum) {
    copy[rowNum + 1] = row[rowNum];
    return copy[rowNum + 1] & row[rowNum];
}

int southEast(int rowNum) {
    copy[rowNum + 1] = row[rowNum + 1] <<= 1;
    return copy[rowNum + 1] & row[rowNum];
}

void clearRows(unsigned long row[]) {
    int i;
    system("clear");
    for (i = 0; i < HEIGHT; ++i) {
        row[i] = 0;
    }
}

void displayBinary(unsigned long x) {
    int bit;
    /*int mask;*/
    for (bit = 0; bit < HEIGHT; ++bit)
    {
        mask = 1 << bit;
        printf("%c", (x & mask) ? 'X' : SPACE);
    }
    printf("\n");
}

int main(void) {
    int i, j, alive;
    char ch;
    unsigned long init32;
    srand(time(NULL));

    for (i = 0; i < HEIGHT; ++i) {
        init32 = ((double)rand() / RAND_MAX) * 0xFFFFFFFF;
        row[i] = init32;
        displayBinary(row[i]);
    }

    do {
        system("clear");
        for (i = 0; i < WIDTH; ++i) {
            unsigned long neighbours[COMPASS] = {
                north(i),
                south(i),
                west(i),
                east(i),
                northEast(i),
                northWest(i),
                southEast(i),
                southWest(i)
            };

            for (j = 0; j < COMPASS; ++j) {
                alive += ((mask & neighbours[j]) ? 1 : 0);
            }

            displayBinary(row[i]);
        }
    } while ((ch = getchar()) != 'n');

    return EXIT_SUCCESS;
}

When the application first starts, the output is what I expect (a 32x32 grid of random 'X' and '.' chars), but for each iteration after that, nothing changes. I want each loop to re-calculate neighbours based on the methods I have (north(), west(), etc...) and print the new 'X' and '.' values on the 32x32 grid.

Can anybody offer some sort of assistance on how to get the new values into the array to be printed on screen? I'm new to programming in C by the way. Thanks.

Upvotes: 0

Views: 111

Answers (1)

Wajahat
Wajahat

Reputation: 1633

I do not see where you are updating your row array anywhere in the code. Should not you be updating that in your do while loop ?

Okay I see in your function like one copied below, you are using row >>= 1 to change row as well. But since you are using pass by value instead of pass by reference, the original row value remains unchanged.

int northWest(unsigned long row, int rowNum) {
    copy[rowNum - 1] = row >>= 1;
    return copy[rowNum - 1];
}

You can use pass by value to fix that. OR Since your row array is global, you can change it directly in these functions using the rowNum (index) which you are passing.

Upvotes: 2

Related Questions