Main Site | Forum | Rules | Downloads | Wiki | Features | Podcast

NLSC Forum

Other video games, TV shows, movies, general chit-chat...this is an all-purpose off-topic board where you can talk about anything that doesn't have its own dedicated section.
Post a reply

Can someone Help me with my program?

Sat Jul 18, 2009 11:43 am

i all, I have a program assignment due that's basically the Game of Life written in C. This is in 101 but I'm still having trouble with it. Basically, I get the function started by asking what grids the user wants to fill in. The problem is that the grids that they ask for don't get filled in but random ones do and following generations always stay the same instead of changing. Here is my code:

Code:
#include <stdio.h>
#define HEIGHT 25
#define WIDTH 25

void clearScreen(char tableA[HEIGHT][WIDTH], char tableB[HEIGHT][WIDTH])
{
   int height;
   int width;

   for (height = 0; height < HEIGHT; height++) {
      for (width = 0; height < HEIGHT; height++)
         tableA[height][width] = 0;
         tableB[height][width] = 0;
   }
   for (height = 0; height < HEIGHT; height++) {
      printf("\n");
      for (width = 0; width < WIDTH; width++)
            printf("-");
   }
   printf("\n");
}

void askUser(char tableA[HEIGHT][WIDTH])
{
   int i;
   int n;
   int height, width;

   printf("Enter the amount of initial organisms: ");
   scanf("%d", &n);
   for (i = 0; i < n; i++) {
      printf("Enter dimensions (x y) where organism %d will live: ", i + 1);
      scanf("%d%d", &height, &width);
      tableA[height][width] = 1;
   }
}

void calculate(char tableA[HEIGHT][WIDTH], char tableB[HEIGHT][WIDTH])
{
   int neighbor;
   int height;
   int width;

   for (height = 0; height < HEIGHT; height++) {
      for (width = 0; width < WIDTH; width++)
         neighbor = 0;
         if (tableA[height - 1][width - 1] == 1) {
            neighbor++;
         }
         if (tableA[height - 1][width] == 1) {
            neighbor++;
         }
         if (tableA[height - 1][width + 1] == 1) {
            neighbor++;
         }
         if (tableA[height][width - 1] == 1) {
            neighbor++;
         }
         if (tableA[height][width + 1] == 1) {
            neighbor++;
         }
         if (tableA[height + 1][width - 1] == 1) {
            neighbor++;
         }
         if (tableA[height + 1][width] == 1) {
            neighbor++;
         }
         if (tableA[height + 1][width + 1] == 1) {
            neighbor++;
         }
         if (tableA[height][width] == 1 && neighbor < 2) {
            tableB[height][width] = 0;
         }
         else if (tableA[height][width] == 1 && neighbor > 3) {
            tableB[height][width] = 0;
         }
         else if (tableA[height][width] == 1 && (neighbor == 2 || neighbor == 3)) {
            tableB[height][width] = 1;
         }
         else if (tableA[height][width] == 0 && neighbor == 3) {
            tableB[height][width] = 1;
         }
   }
}

void swap(char tableA[HEIGHT][WIDTH], char tableB[HEIGHT][WIDTH])
{
   int height;
   int width;

   for (height = 0; height < HEIGHT; height++) {
      for (width = 0; width < WIDTH; width++)
         tableA[height][width] = tableB[height][width];
   }
}

void printTable(char tableA[HEIGHT][WIDTH])
{
   int height;
   int width;

   for (height = 0; height < HEIGHT; height++) {
      printf("\n");
      for (width = 0; width < WIDTH; width++)
         if (tableA[height][width] == 1) {
            printf("X");
         }
         else {
            printf("-");
         }
   }
   printf("\n");
}

int main(void)
{
   char tableA[HEIGHT][WIDTH];
   char tableB[HEIGHT][WIDTH];
   char end;

   end = 'w';
   clearScreen(tableA, tableB);
   askUser(tableA);

   while (end != 'q') {
      calculate(tableA, tableB);
      swap(tableA, tableB);
      printTable(tableA);

      printf("Press q to quit or 1 to continue: ");
      scanf(" %c", &end);
   }
   return 0;
}

Re: Can someone Help me with my program?

Sat Jul 18, 2009 1:09 pm

[nerd]You lack a line to display your program.[/nerd]

Re: Can someone Help me with my program?

Sat Jul 18, 2009 1:34 pm

Did you just solved his problem with one single sentence Lean?

Re: Can someone Help me with my program?

Sat Jul 18, 2009 3:19 pm

No not really. :lol: I didn't even read the entire code. I just noticed that he lacked the getch() command at the end of the main method.

:oops:

Re: Can someone Help me with my program?

Sat Jul 18, 2009 3:54 pm

"Enter the amount of initial organisms: "


I initially read that as 'Enter the amount of initial orgasms'. Hmmm...

Re: Can someone Help me with my program?

Sat Jul 18, 2009 4:46 pm

I dont blame you at all, that happened to me on a numerous occasions.. :oops:

Re: Can someone Help me with my program?

Sat Jul 18, 2009 8:41 pm

Here ive changed it a bit but the prob is it will just generate its own cells

Code:
#include <stdio.h>
#define HEIGHT 25
#define WIDTH 25
#define LIFE_YES 1
#define LIFE_NO 0


typedef int TableType[HEIGHT][WIDTH];

void printTable(TableType table) {
        int height, width;

        for (height = 0; height < HEIGHT; height++) {
                for (width = 0; width < WIDTH; width++) {
                        if (table[height][width] == LIFE_YES) {
                                printf("X");
                        } else {
                                printf("-");
                        }
                }
                printf("\n");
        }
        printf("\n");
}



void clearTable(TableType table) {
        int height, width;
        for (height = 0; height < HEIGHT; height++) {
                for (width = 0; width < WIDTH; width++) {
                        table[height][width] = LIFE_NO;
                }
        }
}

void askUser(TableType tableA) {
        int i;
        int n;
        int height, width;

        printf("Enter the amount of initial organisms: ");
        scanf("%d", &n);
        for (i = 0; i < n; i++) {
                printf("Enter dimensions (x y) where organism %d will live: ", i + 1);
                scanf("%d %d", &height, &width);
                tableA[height][width] = LIFE_YES;
        }
       
        printTable(tableA);
        printf("Generation 0");
}


int getNeighborValue(TableType table, int row, int col) {
        if (row < 0 || row >= HEIGHT
                || col < 0 || col >= WIDTH
                || table[row][col] != LIFE_YES )
        {
                return 0;
        } else {
                return 1;
        }
}


int getNeighborCount(TableType table, int row, int col) {
        int neighbor = 0;

        neighbor += getNeighborValue(table, row - 1, col - 1);
        neighbor += getNeighborValue(table, row - 1, col);
        neighbor += getNeighborValue(table, row - 1, col + 1);
        neighbor += getNeighborValue(table, row, col - 1);
        neighbor += getNeighborValue(table, row, col + 1);
        neighbor += getNeighborValue(table, row + 1, col - 1);
        neighbor += getNeighborValue(table, row + 1, col);
        neighbor += getNeighborValue(table, row + 1, col + 1);
       
        return neighbor;
}

void calculate(TableType tableA) {
        TableType tableB;
        int neighbor, height, width;

        for (height = 0; height < HEIGHT; height++) {
                for (width = 0; width < WIDTH; width++) {
                        neighbor = getNeighborCount(tableA, height, width);
                       
                        if (neighbor==3) {
                                tableB[height][width] = LIFE_YES;
                        } else if (neighbor == 2 && tableA[height][width] == LIFE_YES) {
                                tableB[height][width] = LIFE_YES;
                        } else {
                                tableB[height][width] = LIFE_NO;
                        }
                }
        }
       
        for (height = 0; height < HEIGHT; height++) {
                for (width = 0; width < WIDTH; width++) {
                        tableA[height][width] = tableB[height][width];
                }
        }
}


void loadTestData(TableType table) {
        // toggle
        table[3][4] = LIFE_YES;
        table[3][5] = LIFE_YES;
        table[3][6] = LIFE_YES;
       
        // glider
        table[10][4] = LIFE_YES;
        table[10][5] = LIFE_YES;
        table[10][6] = LIFE_YES;
        table[11][6] = LIFE_YES;
        table[12][5] = LIFE_YES;
}

int main(void) {
        TableType table;
        char end;
        int generation = 0;

        clearTable(table);
        // askUser(table);
        loadTestData(table);
        printTable(table);

        do {
                calculate(table);
                printTable(table);
                printf("Generation %d\n", ++generation);
                printf("Press q to quit or 1 to continue: ");
                scanf(" %c", &end);
        } while (end != 'q') ;
        return 0;
}

Re: Can someone Help me with my program?

Sun Jul 19, 2009 12:26 am

Why that's the thing right? You input the number of your "organisms" and it will generate itself. You did use arrays on those didn't you?

Re: Can someone Help me with my program?

Sun Jul 19, 2009 1:08 am

Yeah i used arrays but the thing is the "organisms" that they are generating are not in the right place..try to compile the program and youll see the prob :(

Re: Can someone Help me with my program?

Sun Jul 19, 2009 1:55 am

Have you tried controlling them per index and not by using a loop?

(yeah sounds a bit lame, but if it works then...)

EDIT: Too lazy to get another Turbo C copy. :oops:

What's wrong then? Syntax errors? Or a logical error?

Re: Can someone Help me with my program?

Mon Jul 20, 2009 12:04 am

i believe its a logical error and btw i hope i can find the solution tomorrow cuz i have to pass it this tuesday :(

Re: Can someone Help me with my program?

Mon Jul 20, 2009 2:05 pm

Try controlling it directly using the indeces.

Re: Can someone Help me with my program?

Mon Jul 20, 2009 2:43 pm

ill try

btw i really like your sig i am now thinking to make some of my own :mrgreen:
Post a reply