I need help with an algorithm

This is a discussion on I need help with an algorithm within the C Programming forums, part of the General Programming Boards category; Hey, I need an algorithm for this homework assignment, I dont even know where to start: Write a program that ...

  1. #1
    Unregistered
    Guest

    I need help with an algorithm

    Hey, I need an algorithm for this homework assignment, I dont even know where to start:




    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
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    how about creating a structure to hold all these variables required (color, engine state, etc) and then decalre a 2D array (which will be your grid) and then maybe even have a go at a function or two, the engine one is the easiest (return 1 if it was successful) that way you can just do

    if(!turnengineon(carname))
    printf("CAR IS ALREADY ON;

    etc

    cant expect people to help you without even attempting somthing yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 01:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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