![]() |
|
#1
|
|||
|
|||
Trying to create the game of lifeIv 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:
|
|
#2
|
||||
|
||||
|
Quote:
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
|
|||
|
|||
|
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:
|
|
#4
|
||||
|
||||
|
Quote:
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:
But maybe I misunderstood your problem. |
|
#5
|
|||
|
|||
|
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:
|
|
#6
|
|||
|
|||
|
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:
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:
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:
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:
CPP / C++ / C Code:
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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:
Also could i increment the values c and r instead of timesing them by the variables N and n? |
|
#9
|
||||
|
||||
|
Quote:
Actually it should be more like: CPP / C++ / C Code:
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
|
|||
|
|||
|
Quote:
Code:
|
Recent GIDBlog
Developing GUIs with wxPython (Part 3) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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