GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Jan-2004, 08:30
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough
Arrow

Trying to create the game of life


Iv been given the task by my lecturer to make the "Game of life", this is a simple program that decides whether or not a cell survives using predetermined laws:Rules and example . I have a segement of code and i keep gettin the error "0025 Error:An object of type '<ptr><ptr>-file' cannot b assigned to an object of type '<ptr>int'. Iv spent 3 days trying to solve it on my own and cant find anything on the internet to help me(or Im too dumb to realise it), Im trying to read from the file "ref" then transfer it into the array "matrix". My code is as follows and is incomplete,as i still need to insert a pointer to read the text in the matrix and then write back to a separate file but Im currently trying to do this myself. I would appreciate any help or pointers(no pun intended).

CPP / C++ / C Code:
#include <stdio.h>
int main()
{
  int row, column;
  FILE *game;
  int *reference_ptr;

  game = fopen ("ref.txt", "r");

  if (game == NULL)
{
      printf ("File could not be opened\n");
}
  else
{
      printf ("File opened!  Closing it now...\n");
      fclose (game);
      }
  reference_ptr=&game;
  int matrix[10][10] =
  {  
  *reference_ptr  
  };
}
  #2  
Old 20-Jan-2004, 16:12
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
reference_ptr=&game;
int matrix[10][10] =
{
*reference_ptr
};
}

This first line shown here is where your error is coming and I am not sure what the rest of your code is trying to do.

You have defined reference_ptr as an int* and game is a FILE*. From what I can see your reference_ptr is not needed at all. What you really want to do is use a routine such as fread, fscanf or the like to read in your integers and place them into your array.

Read up on fscanf. I think that is what you would want to use.

Good luck
  #3  
Old 20-Jan-2004, 16:49
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough
Thanks for responding but after alot of messing around i managed to get wot i think i want, sadly im not sure if its worked coz i cant get a printf statement to work to show if the matrix really holds the correct values.

[c]
#include <stdio.h>
int main(void)
{
int row, column;
FILE *game;
int r,c;
int matrix[10][10];

game = fopen ("ref.txt", "r");

if (game == NULL)
{
printf ("File could not be opened\n");
}
else
{
printf ("File open\n");

for ( r = 0 ; r < 10 ; r++ )
for ( c = 0 ; c < 10 ; c++ )
fscanf( game, "%d", &matrix[r]
CPP / C++ / C Code:
 );  

     printf ("Closing file\n");
     fclose (game); 
  }
}
  #4  
Old 20-Jan-2004, 16:59
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by warny_maelstrom
Thanks for responding but after alot of messing around i managed to get wot i think i want, sadly im not sure if its worked coz i cant get a printf statement to work to show if the matrix really holds the correct values.
[c]
#include <stdio.h>
int main(void)
{
int row, column;
FILE *game;
int r,c;
int matrix[10][10];

game = fopen ("ref.txt", "r");

if (game == NULL)
{
printf ("File could not be opened\n");
}
else
{
printf ("File open\n");

for ( r = 0 ; r < 10 ; r++ )
for ( c = 0 ; c < 10 ; c++ )
fscanf( game, "%d", &matrix[r]
CPP / C++ / C Code:
 );  

     printf ("Closing file\n");
     fclose (game); 
  }
}


That looks good. One question what are the row & column variables for. I would get rid of those if you are not using them. As for the printf statement you should be able to do that like you did the fscanf statement.

[c]
for ( r = 0 ; r < 10 ; r++ ) {
for ( c = 0 ; c < 10 ; c++ )
printf("%d ", matrix[r]
CPP / C++ / C Code:
;
    printf("\n");   //Give a new line at each row.
}

But maybe I misunderstood your problem.
  #5  
Old 21-Jan-2004, 03:19
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough
My code so far works perfectly and iv put in a printf to verify that my file is in the matrix. I then need to create a pointer to check if the variables surrounding my chosen point collectively equal 2or3. iv never used a pointer and would welcome any help or hints on how to use it.
(x-1,y-1),(x,y-1),(x+1,y-1)
(x-1,y),(x,y),(x+1,y)
(x-1,y+1)(x,y+1),(x+1,y+1)

Code:
#include <stdio.h> int main(void) { FILE *game; int r,c; int matrix[100][100]; printf ("U can be a winner at the Game of Life"); if((game=fopen("C:\\Documents and Settings\\lxh346\\Desktop\\ref.txt", "r")) != NULL) { printf ("\nFile open\n"); for ( r = 0 ; r < 100 ; r++ ) { for ( c = 0 ; c < 100 ; c++ ) { fscanf( game, "%d", &matrix[r][c] ); printf("%d", matrix[r][c]); //Give a new line at each row. } printf("\n"); } } else printf ("File could not be opened\n"); printf ("Closing file\n"); fclose (game); }
  #6  
Old 21-Jan-2004, 05:45
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Hey, welcome to the boards.

Righto, quick thing on pointers & arrays (or matrices). Declaring, say, a 1-dimensional integer array "arr" of size N is straightforward, as is assigning stuff to it:
CPP / C++ / C Code:
int arr[N] = {7,2,8,.....,4}
If you want ot get the value 8 from it, you write arr[2]. This you know.

But you can also get values from the array using pointers. What you need to know is, when you declare an array, the compiler sets aside a consecutive chunk of memory for it. So the array arr will look like this in the memory:
Code:
____________ |_____7_____| 0x100 <---arr |_____2_____| 0x102 |_____8_____| 0x104 ...... |_____4_____| 0x?
But where do pointers fit in? C does the following:
The name of the array is a pointer to the first element of the array

So printing the value of "arr" will give you something like "0x100" It's the equivalent of printing &arr[0]. Which is great and all, but how is that useful.

You can do arithmetic on pointers. You can add to them, which for C means that you move the pointer to the following slots in the array. For example
CPP / C++ / C Code:
printf("arr[2] = %d", *(arr+2) ); //prints 8

We've nudged the pointer on 2 spaces. So to get the ith element of the array arr, you dereference the memory address given by arr+i, i.e. *(arr+i).

Note the brackets, if you type *arr + 2, it will give you 8+2 = 10. Operator precedence at it's worst! Also beware that it's easy to push the pointer beyond the end of the array, into other random bits of memory, so be careful.

For 2D array, you've to be a little more cunning with your math! (Yippee sez the mathematician). 2D arrays are laid out in memory just like 1D, in one big self-contained chunk, like so:
Code:
_______________ |___arr[0][0]___| <-- arr |___arr[0][1]___| |___arr[0][2]___| |___.........___| |___arr[0][N-1]_| |___arr[1][0]___| |___arr[1][1]___|
and so on. As this is an NxN matrix (say), and you want the [i][j] slot of the matrix, you've to do
CPP / C++ / C Code:
*(arr +(i*N) + j);

Make sense? Draw a small matrix's memory map out on paper and test it out. Remember C handles adding numbers to memory addresses nicely. You don't have to worry about different types & how many bytes they use.

Hope this helps!
GF
  #7  
Old 21-Jan-2004, 10:23
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Warny:
That pointer primer by Garth is excellent. That should really help in coding this. I am assuming that your assignment calls for you to use the pointer addressing.

Also, a couple of points on doing this.
First, I would put your process into a seperate function for clarity. You can do everything in your main(), but it really helps readability in my opinion to modularize.

Second, you will probably need to do two sweeps of your matrix for each process. You need to calculate, whether a node will live or die based upon the current status of all the nodes before changing them. I suggest that you use a flag on the first sweep such as:
0: dead cell that is to remain dead
1: live cell that is to remain alive
2: live cell that is to be killed
-1: dead cell that is to be resurected.

then you can compare cells that are alive as anything greater than 0 and cells that are dead as anything less than or equal to 0.

Then on the second sweep, simply change cells that are 2 to 0 and cells that are -1 to 1.

Finally, be careful on the edges of your matrix. C doesn't do boundary checking at all. So if you try to address a memory space outside of your boundaries your program will crash. For example, if you are testing the first cell - matrix[0][0] and try to test the cell at location matrix[-1][-1] it will bring down your program.

Good luck!
  #8  
Old 21-Jan-2004, 11:58
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough
For simplicities sake would this code give me a value for the sum of all the values surrounding (c*N,r*n):
(c-1,r-1) (c,r-1) (c+1,r-1)
(c-1,r) (c,r), (c+1,r)
(c-1,r+1) (c,r+1) (c+1,r+1)


Code:
total=*(matrix+(c-1)*N+(r-1)*n)+*(matrix+(c*N)+((r-1)*n))+*(matrix+((c+1)*N)+((r-1)*n)) +*(matrix+((c-1)*N)+(r*n))+*(matrix+((c+1)*N)+(r*n))+ *(matrix+((c-1)*N)+((r+1)*n))+*(matrix+(c*N)+((r+1)*n))+*(matrix+((c+1)*N)+((r+1)*n))

Also could i increment the values c and r instead of timesing them by the variables N and n?
  #9  
Old 21-Jan-2004, 13:46
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by warny_maelstrom
For simplicities sake would this code give me a value for the sum of all the values surrounding (c*N,r*n):
(c-1,r-1) (c,r-1) (c+1,r-1)
(c-1,r) (c,r), (c+1,r)
(c-1,r+1) (c,r+1) (c+1,r+1)


Code:
total=*(matrix+(c-1)*N+(r-1)*n)+*(matrix+(c*N)+((r-1)*n))+*(matrix+((c+1)*N)+((r-1)*n)) +*(matrix+((c-1)*N)+(r*n))+*(matrix+((c+1)*N)+(r*n))+ *(matrix+((c-1)*N)+((r+1)*n))+*(matrix+(c*N)+((r+1)*n))+*(matrix+((c+1)*N)+((r+1)*n))

Also could i increment the values c and r instead of timesing them by the variables N and n?

Actually it should be more like:
CPP / C++ / C Code:
total=*(matrix+(c-1)*N-(r-1)) + *(matrix+c*N+(r-1)) + ...

Also, N is a constant which is the number of items in a row. It should not be indexed at all. Only c and r should be indexed.

This is a pretty good start, but you may want to think a little bit more about your approach. See my comments above for things to watch out for.
  #10  
Old 21-Jan-2004, 19:13
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough
Quote:
Originally Posted by dsmith
Also, N is a constant which is the number of items in a row. It should not be indexed at all. Only c and r should be indexed.
I really have no idea wot to do with N, iv tried to construct as much as i could by using my prior knwledge and wot uv told me about pointers and iv come up with the following but i get the errors:"0037 Illegal pointer arithmetic-you cannot add two pointers" four times , and "0038 'matrix' is not a function" once.
Code:
#include <stdio.h> #include<clib.h> int main(void) { FILE *game; int r,c,total,N; int matrix[100][100]; if((game=fopen("C:\\Documents and Settings\\lxh346\\Desktop\\ref.txt", "r")) != NULL) { printf ("Game of Life rules\n"); for ( r = 0 ; r < 100 ; r++ ) { for ( c = 0 ; c < 100 ; c++ ) { fscanf( game, "%d", &matrix[r][c] ); printf("%d", matrix[r][c]); } printf("\n"); } } else printf ("File could not be opened\n"); { printf ("\nThis is the Original Generation\n\nPress a Key to create Second Generation\n"); getch(); //Stops programme until user inputs a value// system("cls"); //Clears Screen// printf("************************************\n* Welcome to the Game of Life. *\n************************************\n"); printf ("Updating Matrix\n\n"); for (r = 0 ; r < 10 ; r++ ) //increments rows// { for ( c = 0 ; c < 10 ; c++ ) //increments Columns// { total=*(matrix+(c-1)*N+(r-1))+*(matrix+(c*N)+((r-1)))+*(matrix+((c+1)*N)+((r-1)))+*(matrix+((c-1)*N)+(r))+*(matrix+((c+1)*N)+(r))+*(matrix+((c-1)*N)+((r+1)))+*(matrix+(c*N)+((r+1)))+*(matrix+((c+1)*N)+((r+1))); if ((matrix(c)+(r))==0 && total==3 || (matrix(c)+(r))==1 && total==2 || (matrix(c)+(r))==1 && total==3) { matrix(c)(r)=1; } else if *(matrix(c)(r)==1 && total>3 || matrix[r][c]==1 && total<2) { matrix(r)(c)=0; //sets value to dead if fitting rules// } printf(" %d", matrix[r][c]); //prints new matrix values// } printf("\n"); } game = fopen ( "ref.txt", "w" ); //open file with write access// if ( game == NULL ) { printf ( "File open failure" ); //incase of missing link// } fprintf ( game, "%d", matrix[r][c]); //print values back into text file// fclose (game); //closes file// printf ("\nDo you want to run it again? Y/N\n"); //option to re-run programme// repeat=getch(); system ("cls"); //Clears Screen// } while (repeat=='y'); //if user types 'y', programme repeats// }
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Game Cheating? pcxgamer Computer Software Forum - Games 18 24-Nov-2005 11:12
Tips for game troubleshooting pcxgamer Computer Software Forum - Games 0 02-Jan-2004 05:27
X2 The Threat pcxgamer Computer Software Forum - Games 0 25-Dec-2003 09:16
Can't seem to create db Tigress7 MySQL / PHP Forum 3 19-Aug-2003 09:19
How do I create JavaScript Links? JdS Web Design Forum 8 29-Jan-2003 15:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:53.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.