i really need help here

This is a discussion on i really need help here within the C Programming forums, part of the General Programming Boards category; Sorry, i posted this before, somebody responded but not really with what I needed. I haev not learned arrays so ...

  1. #1
    Unregistered
    Guest

    Question i really need help here

    Sorry, i posted this before, somebody responded but not really with what I needed. I haev not learned arrays so im somewhat limited in how i solve this, I really don't know where to begin. I just need an algorithm for it. Thanx..........



    Write a program that simulates and stores the qualities of a car. The qualities you should store are the following:

    Color ('R'=red; 'G'=green; 'B'=blue; 'W'=white; 'S'=Silver)
    Ignition (0=off; 1=on)
    Position (any X, Y coordinate in a 20 X 20 grid)

    You should write a function for each of the following:


    color assignment: Randomly picks one of the five colors and returns the corresponding char to the calling function. This function should be called once at the beginning of the program. The color will not change during the execution of the program.

    move car horizontally: This function will need the original X coordinate and the number of spaces you would like to move the car (a negative value will move the car left; positive value will move the car right).
    If the car's ignition is not on, this function tells the user that s/he must turn the ignition on first.

    If the user tries to move the car beyond the border of the 20 X 20 grid, you should not move the car; instead you should report an error message.

    Hints: You may need more parameters than you think.
    For either error condition, what value do you think the function should return?


    move car vertically: This function will need the original Y coordinate and the number of spaces you would like to move the car (a negative value will move the car up; positive value will move the car down).
    If the car's ignition is not on, this function tells the user that s/he must turn the ignition on first.

    If the user tries to move the car beyond the border or the 20 X 20 grid, you should not move the car; instead you should report an error message.

    Hints: You may need more parameters than you think.
    For either error condition, what value do you think the function should return?


    turn engine on: This function will turn the car on when it is off. If it is called while the car is on, it should print an error message. In such case, the state of the car should not be changed.

    turn engine off: This function will turn the car off when it is on. If it is called while the car is off, it should print an error message. In such case, the state of the car should not be changed.
    Hints: What value do you need to give these functions so they can check if the car is on or off?
    What is a good value for off and on (like true and false)?


    report the state of the car: At any point in the program, the user may want to know the "state" of the car. This function will take all the car's characteristics as parameters and report the status of each (including a copy of the cars position in the grid).
    For example:
    Car Stats:
    Color: Red
    Ignition: On
    Location: 5,17
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    ----R--------------- (the R correspons to the color of the car)
    --------------------
    --------------------
    --------------------

    The initial position of the car should be assigned randomly.

    After calling the function which assigns a color to the car and assigning the car a position, your program should have an infinite loop. Within the loop, ask the user what s/he would like to do next. For example:

    What would you like to do next (1 - turn car on; 2 - turn car off; 3 - change position of car)?

    If the user wants to change the car's position, main will prompt the user for a direction (horizontal or vertical) and the distance to move in that direction. Then the appropriate function would be called with suitable arguments.

    After the user selects an action, you should call the function which prints the status.

    You cannot use any global variables.

  2. #2
    Nit
    Nit is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    42
    Here's a start.

    Color ('R'=red; 'G'=green; 'B'=blue; 'W'=white; 'S'=Silver)
    Ignition (0=off; 1=on)
    Position (any X, Y coordinate in a 20 X 20 grid)
    Code:
    struct car {
          char color;
          int ignition;
          int xCoordinate;
          int yCoordinate;
          struct car *nextCar;
    };
    Take away sturct car *nextCar if you don't want to keep track of mulitple cars in a linked list. Now just think of how you can get a value into each one of these data fields by coding your own functions.
    http://www.KBeutler.com

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    I remember you posted this and I'm not sure what everyone else said but I gave you a structure definition and an example on start car and said to show some code of your own before asking. Cant expect people to do your homework but I'm pretty sure everyone is happy to help u.

  4. #4
    Unregistered
    Guest
    you were the only person to reply to my other post, and ur post used an array, which I havent learned yet. I really dont know how to start this thing still, becuase each part needs a function and needs to be put together. I dont know how to brgin, and I feel completely overwhlemed. I starts with this

    Code:
    
    /* This is a program that simulates and stores the qualities of a car */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    char Set_color ();
    
    /* start main program */
    
    int main()
    {
    
    		Set_color(0);
    		Set_color(1);
    		Set_color(2);
    		Set_color(3);
    		Set_color(4);
    
        return 0;
    }
    
    char Set_color()
    {
    	int color;
    	srand((int)time(NULL));
    	color = rand()%5;
    
    	switch(color) {
    		case 0:
    		printf ("Red\n");
    		break;
    
    		case 1:
    		printf ("Green\n");
    		break;
    
    		case 2:
    		printf ("Blue\n");
    		break;
    
    		case 3:
    		printf ("White\n");
    		break;
    
    		case 4:
    		printf ("Silver\n");
    		break;
    		
    		return color;
    
    	}
     
    }

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    ok well an array is heaps easier than dynamic memory and probly the only way. Now since you basically want a graph of a fixed size then you should use a 2D array which is defined like:
    Code:
    int color[rows][cols]
    where you change the word rows with a number and cols with a number, then any time you need to put a value in you would do:
    Code:
    color[1][4] = 5;
    and that would assign in row 1, col 4 the value 5. Just remember that when accessing a 2D array it starts from 1.
    we'll your color function looks fine, just assign the value once you call it to a variable, ie:
    Code:
    struct car {
          char color;
          int ignition;
          int xCoordinate;
          int yCoordinate;
          struct car *nextCar;
    } CAR;
    
    int main()
    {
      char color;
      CAR carrecords[20][20];
    
      color=Set_color();
      return 0;
    }
    but to do this unless you use global variables you'll need to learn pointers and arrays.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21