![]() |
|
#1
|
|||
|
|||
Linked Lists / Structures / Memory AllocationHello
I'm having trouble getting my program to do what it needs because i don't completely understand how linked lists work. If i could get a some help i'd greatly appreciate it. I mainly need help with making it go through the 1,000 time steps, remove cars that have crashed, and debugging a few problems. Here's the question: Consider a square domain partitioned into 250,000 equally spaced grid cells (500 by 500). We will randomly distribute some cars into this domain such that no more than 1 car is located in any grid cell initially. Each car has a random mass between 1-50. At each time step of the simulation, each car randomly moves one grid cell in the x or y direction (4 possible directions). If a car touches the edges of the domain then it “rebounds” 180o towards the interior. If two cars land on the same grid cell, then they have had an accident, and the smaller car is removed from the simulation. Write a program to simulate this system. Use structures to represent the cars in the simulation. Make your program efficient by only allocating memory for active cars, and free memory for cars that are no longer active. Hint: use a linked list to represent the cars. Show the initial distribution of cars and the final distribution of surviving cars (the number of cars at each possible mass) after 1000 time steps if the simulation starts with 500 and 2000 cars (2 cases). Make a histogram of your results using excel. The bins on the x-axis of the histogram will be the mass of each car. The value on the y-axis will be the number of cars in each bin. Here's my code: CPP / C++ / C Code:
-Any help or alterations you see neccessary would be of great appreciation Thanks Last edited by admin : 17-Mar-2009 at 23:51.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|||
|
#2
|
|||
|
|||
Re: Linked Lists / Structures / Memory AllocationI'm not a C person and I cannot make this program for you (not saying you'd want that...), but since Dave and Bob are on holiday, let me say something in general.
First of all, it's not a good idea to write a thousand lines before trying to compile. You didn't try to compile it even once, did you? I tried and I got Code:
Based on this compiler output I'd say getting linked lists to work are your least worries. CPP / C++ / C Code:
CPP / C++ / C Code:
Randomize X, Y, Weight. Create an array of 500 cars (why, since you were supposed to make linked list?) and assign X, Y and Weight to the first car in the list. Then create a pointer to point at the just created array of cars and assign to it the address of the first car in the array. Then assign to the first car in the list values of X, Y and Weight (Why do this twice?). This is what I think you're doing: Randomize X, Y and Weight. Create an array of 500 floats and assign X, Y and Weight to the first three elements in the array, respectively. Then the next line already generates an error, since there's not a type 'car' in your program. car_t is a struct you have just defined. Later in your loop you use CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
I'm also not sure about the way you randomize the locations of the cars. While it's unlikely, given 250000 possibilities, it's still possible that two cars would get the same location. Your assignment didn't mention it, but I'm assuming you cannot have two cars in the same location? You could create an array of 250000 bools or bits and set a bool or a bit each time you create a new location. Then, when you generate another location, you could easily check if that location is already in use (bool is true or a bit is set). Maybe... CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
As for the actual check if two cars are on the same location, I have no idea what would the best way to go about it. A primitive way would be to check the locations of all cars against the locations of all the other cars. A little more sophisticated way would be to use your (would-be) array of bools or bits to check if there actually IS a car in the same location (you would of course update that array each time the cars move) and if there is, THEN go through all the cars and find the one(s) in the same location. Perhaps again a little more sophisticated way would be to turn your (would-be) array of bools or bits into an array of pointers to cars. So, the pointers would point to the car currently at a given location. Then (I think) you could easily check which of the cars has more mass and delete the approriate one and assign the pointer to point at the new car. (In fact, at the moment it seems to me that the last way would make using the linked list obsolete, since you would already have pointers keeping track of all the cars.) The last way, of course, would only work if your cars move one by one. Probably not overly complicated to make it work even if they all move at once, but... I think a bottom line is justified: Get the basics of C handled first. |
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| Singly linked to doubly linked | wbaker01 | C++ Forum | 2 | 22-Jan-2007 13:23 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| Memory de-allocation during debugging | gaoanyu | C Programming Language | 12 | 19-Dec-2005 04:50 |
| Linked List | silicon | C++ Forum | 2 | 22-Jul-2004 03:29 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The