C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old April 23rd, 2003, 06:03 PM
traixanha traixanha is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 6 traixanha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
please help

Code:

#include 
#include 

#define MAX_SIZE 10
void printGrid(int grid[MAX_SIZE][MAX_SIZE],int size);
void zapGrid(int grid[MAX_SIZE][MAX_SIZE],int size);
void enterGrid(int grid[MAX_SIZE][MAX_SIZE],int size);
int countNeighbors(int row, int col, int grid[MAX_SIZE][MAX_SIZE],int size);
void printNeighbors(int grid[MAX_SIZE][MAX_SIZE],int size);
int countLiveCells(int grid[MAX_SIZE][MAX_SIZE],int size);
void calcNextGen(int grid[MAX_SIZE][MAX_SIZE], int size);

int main()
{
  int life_grid[MAX_SIZE][MAX_SIZE];
  int size = 5;

  zapGrid(life_grid,size);
  enterGrid(life_grid,size);
  printGrid(life_grid, size);
  do{
     printf("\n");
     calcNextGen( life_grid, size);
     printGrid( life_grid, size);
     printf("Number of live cells = %d\n", countLiveCells (life_grid, size));
system("pause");
       }[d]while[/b](2 > 1);

    return 0;
}
void printNeighbors(int grid[MAX_SIZE][MAX_SIZE],int size)
  {
  int i,j;
/*print it out*/
     for(i = 0; i < size; ++i)
       {
           for(j = 0; j < size; ++j)
              {
                   printf("%2d", countNeighbors(i,j,grid, size));
               }
             printf("\n");
        }
  }
void printGrid(int grid[MAX_SIZE][MAX_SIZE],int size)
   {
  int i,j;
 /*print it out*/
       for(i = 0; i < size; ++i)
           {
              for(j = 0; j < size; ++j)
                 {
                     printf("%2d", grid[j]);
                  }
printf("\n");
           }
    }

void zapGrid(int grid[MAX_SIZE][MAX_SIZE],int size)
   {
      int i, j;
[i]/*fill it with all zeros */
         for(i = 0; i < size; ++i)
      {
          for(j = 0; j < size; ++j)
              {
                   grid[j] = 0;
               }
         }
     }

void enterGrid(int grid[MAX_SIZE][MAX_SIZE],int size)
   {
    int row, col;
    printf("Please enter row and col for life or -1 -1 to stop\n");
    do{
           scanf("%d%d", &row, &col);
           if (row > -1 && col > -1)
           grid[row][col] = 1;
         }while(row > -1 && col > -1);

     }

   int countNeighbors(int row, int col, int grid[MAX_SIZE][MAX_SIZE],int size)
    {
     int count = 0;
[i]/* top row */
    if (grid[row -1 ][col-1] == 1)
    count += 1;
    if (grid[row -1 ][col] == 1)
    count += 1;
    if (grid[row -1 ][col+1] == 1)
    count += 1;

/*middle row */
    if (grid[row ][col-1] == 1)
    count += 1;
    if (grid[row ][col+1] == 1)
    count += 1;

/* bottom row */
     if (grid[row +1 ][col-1] == 1)
     count += 1;
     if (grid[row +1 ][col] == 1)
     count += 1;
    if (grid[row +1 ][col+1] == 1)
    count += 1;
  return count;
    }

void calcNextGen(int grid[MAX_SIZE][MAX_SIZE], int size)
   {
      int new_grid[MAX_SIZE][MAX_SIZE]; 
     int count,i, j;

     zapGrid(new_grid, size);

     for(i = 0; i < size; ++i)
          {
              for(j = 0; j < size; ++j)
                {
                   count = countNeighbors(i, j, grid, size);
                   if(grid[j] == 1)
                 { [i]/*live cell, does is keep living? */
   if(count == 2 || count == 3)
   new_grid[j] = 1; [i]/*congratulations on making it to the next      generation */
                 }else{
/*dead cell, is there birth?*/
if(count == 3)
new_grid[i][j] = 1; /*birth!!!! try to hold back the tears */
                           }
             }/*for j*/ 
}/*for i*/


/*copy the new back over the old */
for(i = 0; i < size; ++i)
       {
           for(j = 0; j < size; ++j)
              {
                  grid[i][j] = new_grid[i][j];
              }
         }
}


int countLiveCells(int grid[MAX_SIZE][MAX_SIZE],int size)
    {
        int count=0 ;
        int i;

     for (i=0 ; i< size; ++i)

     count = count + size;



     return count;
       }

i think i did the part "countLiveCells " at bottom last is wrong,would u give give me any idea ?thanks.

p/s : and how do i make the code to ask the user if they want to see the next generation (so they can exit ).thanks alot

Last edited by traixanha : April 23rd, 2003 at 10:37 PM.

Reply With Quote
  #2  
Old April 23rd, 2003, 07:24 PM
Gmorphus Gmorphus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: ISRAEL
Posts: 35 Gmorphus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to Gmorphus
I understand you are trying to code the "game of life" but that you have posted ALL of your code here isn't helpfull.
you have to tell what exactly is the problem, what are the rules, and generally wtf you are talking about...
__________________
"Gravitation can NOT be responsible for people falling in Love"
(one of the most significant characters in the history, can you guess?)

Gmorph.

Reply With Quote
  #3  
Old April 23rd, 2003, 07:35 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 55 m 53 sec
Reputation Power: 437
Also, traixanha, to make your code more readable, please enclose it in "code" tags, which preserves the indenting.

Before the first line of code, print a pair of square brackets with the word, "code" between them (minus the quote marks). Then after the last line of code, print a pair of square brackets with "/code" between them. You can do the same thing with "quote", b (for bold), i (for italics), etc.

For example:
Code:
void printNeighbors(int grid[MAX_SIZE][MAX_SIZE],int size)
{
    int i,j;
    /*print it out*/
    for(i = 0; i < size; ++i)
    {
        for(j = 0; j < size; ++j)
        {
            printf("%2d", countNeighbors(i,j,grid, size));
        }
        printf("\n");
    }
}


Making it easier for us to read makes it easier for us to help.

Reply With Quote
  #4  
Old April 23rd, 2003, 10:41 PM
traixanha traixanha is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 6 traixanha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally posted by dwise1_aol
Also, traixanha, to make your code more readable, please enclose it in "code" tags, which preserves the indenting.

Before the first line of code, print a pair of square brackets with the word, "code" between them (minus the quote marks). Then after the last line of code, print a pair of square brackets with "/code" between them. You can do the same thing with "quote", b (for bold), i (for italics), etc.



hi there
thanks for tell me that,i have changed ,hope it will help ,thanks alot

Reply With Quote
  #5  
Old April 24th, 2003, 03:26 AM
Gmorphus Gmorphus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: ISRAEL
Posts: 35 Gmorphus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to Gmorphus
I this case i helps just a bit.
dwise1_aol meant that you should do also that, in addition to what I said (i think...).

Please tell us what do you wannna do, where is the problem, and what the rules you are playing the game by (there are many versions of this game).

Help us help you.

Reply With Quote
  #6  
Old April 24th, 2003, 10:18 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 55 m 53 sec
Reputation Power: 437
Re: please help

Quote:
Originally posted by traixanha

...
Code:


int countLiveCells(int grid[MAX_SIZE][MAX_SIZE],int size)
    {
        int count=0 ;
        int i;

     for (i=0 ; i< size; ++i)

     count = count + size;



     return count;
       }

i think i did the part "countLiveCells " at bottom last is wrong,would u give give me any idea ?thanks.

p/s : and how do i make the code to ask the user if they want to see the next generation (so they can exit ).thanks alot


As Gmorphus said, we're not quite sure what you are trying to do nor what problems you are encountering.

Regarding the last function (quoted above), what I see it doing is to calculate the square of size (ie, size*size) and returning that value. I assume that is not what you are wanting to do and that the 2D array, grid, is supposed to be used. Were you wanting to nest for loops in order to index each element of grid and add its contents to the running count?

Also, you do not need to specify the size of an array when it is in a function argument list.

Reply With Quote
  #7  
Old April 24th, 2003, 07:45 PM
traixanha traixanha is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 6 traixanha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hi
when i try to run the progame , the 0 is present for dead cell, 1 is present for live cell,and in next generation the cell still alive only they have 2 neighbour plus 1 live cell = 3

Reply With Quote
  #8  
Old April 25th, 2003, 03:39 AM
Gmorphus Gmorphus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: ISRAEL
Posts: 35 Gmorphus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to Gmorphus
I don't want to be rude, but what can't you understand?
you HAVE to tell us what you are trying to do, what are the rules of the game, where exactly do you think is the problem etc.

I realy would like to help you, but I can't.
Do the said above and we will help.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > please help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway