Thread: Help! -Linked Lists, Memory Allocation, Time Steps, Debugg

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    UC Davis, California
    Posts
    4

    Help! -Linked Lists, Memory Allocation, Time Steps, Debugg

    Hello,
    I'm having trouble with effectively getting my code to do what my assignment says. Initially I can't debug it to get it to compile for what i have so far. Next, I'm not sure where and how i should add a linked list in my code to allocate memory after a crash has occured. Then lastly i'm not sure how to make the cars go through 1000 time steps in the simulation.

    Here is 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.

    -I would greatly appreciate any help i could get, seeing how i'm stuck and just going in circles.
    Thank You in advance for trying to help!

    Here is my Code:

    Code:
    /* ************************************************************************
       written by: Robert Stange (Feb09)
                   Student of Civil and Environmental Engineering
                   UC Davis ECI19
    
       The purpose of this program is to predict the distribution of surviving cars.
              ------------Inputs--------------
    *The program must generate a 250,000 equally spaced grid with 500 cars ranging in 
    mass from 1-50. Each car must beable to randomly move in 4 possible directions.
    If there is a crash the smaller car is removed from the simulation. Then you must
    allocate the memory for that crashed car and free it to the system.
            -The program randomly generates the following data:
                     1)position
                     2)weight
                     3)directon
              ------------Output--------------
            -The program will output the surviving cars after 1000 time steps.
       *********************************************************************** */
    
    //prototype definitions
    #include<stdio.h>   
    #include<stdlib.h>
    
    //structure for x,y,and weight of an array of cars
     typedef struct 
       {
         float x;
         float y;
         float weight;
       } car[];
    
    int location()
    
    int main()
     {
    //variable definitions
       int i = 0;
       int X = rand() % 500;
       int Y = rand() % 500;
       int Weight = rand() % 50;
       float car[5]={X, Y, Weight};
    
    //generate cars
      for (i=0;i<500;i++)
         {
             car[i]={X, Y, Weight};
             printf("location of car is %d,%d and weight is %d\n",X.x, Y.y, Weight.weight);
         }      
     return 0;
    }
    
    //-------------------------------------
    //Function for the location of the cars
    //-------------------------------------
    
    int location()
      {
         int m[5] = rand() % 4;
         int g = 5;     
    
    //loop through the different randimizations
       for (i=0; i<=g; i++)
         {
           if(m==1)
            car[i]=rand() % (X+1).X;
           if(m==2)
            car[i]=rand() % (X-1).X;
           if(m==3)
            car[i]=rand() % (Y+1).Y;
           if(m==4)
            car[i]=rand() % (Y-1).Y;
          }
     return m;
    }
    
    //--------------------------------
    //Function to check for collisions
    //--------------------------------
    
    int check()
     {
        int ix=0;
        int iy=0;
    
    //loop checking for collisions
      for(i=o; i<=500; i++)
        {
          for(ix=0; ix<=500; ix++)
           {
             for(iy=0; iy<=500; iy++)
              {
                if(iy=ix)
              }
                car[i]= 0.x
                car[i]= 0.y
            }
        }
     }
    }
     return 0;
    }

    Thanks for all you time and help!
    -Robert

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Let's start with the following code:
    Code:
    int check()
     {
        int ix=0;
        int iy=0;
    
    //loop checking for collisions
      for(i=o; i<=500; i++)
        {
          for(ix=0; ix<=500; ix++)
           {
             for(iy=0; iy<=500; iy++)
              {
                if(iy=ix)
              }
                car[i]= 0.x
                car[i]= 0.y
            }
        }
     }
    }
     return 0;
    }
    Take a careful look at that function, and figure out why it won't compile. Start with counting the number of start brackets ({) vs the number of end brackets (}).

    Next, take at look at your if statement.
    Code:
     if(iy=ix)
    What do you want to do if iy=ix?

    Code:
    car[i]= 0.x
    What in the world is going on here? This doesn't even resemble valid C code. You need to do a bit more reading from your course book.

    You need to start with less code. Start with a single function, and get that to compile. Then start adding more code, compiling as you go.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MetallicaX View Post
    Next, I'm not sure where and how i should add a linked list in my code to allocate memory after a crash has occured.
    I think the idea is to use the linked list for the cars so you can de-allocate the memory when a car is destroyed. So you want your car typedef to reflect this:
    Code:
     typedef struct _car
       {
         float x;
         float y;
         float weight;
         struct _car* next;
       } car[];
    Then lastly i'm not sure how to make the cars go through 1000 time steps in the simulation.
    Use another for loop after you "generate cars".

    ps. nice assignment
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Linked Lists and Memory
    By John_L in forum C Programming
    Replies: 1
    Last Post: 10-27-2007, 12:27 PM
  3. Memory allocation in linked lists
    By Panserbjorn in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 11:02 PM
  4. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  5. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM