Question

In: Computer Science

Car Race Outcome: Student will demonstrate the ability to generate random numbers Student will demonstrate the...

Car Race

Outcome:

  • Student will demonstrate the ability to generate random numbers
  • Student will demonstrate the ability to use a switch statement with cases
  • Student will demonstrate the ability to use loop structures
  • Student will demonstrate the ability to create program logic
  • Student will demonstrate the ability to produce nicely formatted output

Program Specifications:

You will design a simulated car race. The race will consist of three cars: car A, car B, and car C.

Your program will show the cars (the letters A, B, and C) moving across the screen toward the finish line. The finish line will be shown on the screen at all times during the race. Your program will randomly move each car (between 1 and 3 spaces) at randomly selected intervals. The race ends when a car moves 80 spaces. After the race ends, you will display the results:

First Place:           carX

Second Place:       carX

Third Place:          carX

After displaying the results, the program will ask the user if he or she wishes to run another race.

Submission Requirements:

You must follow the rules from the first assignment.

YOU MUST:

  • Use functions for as much of the code as possible
  • Turn in a design tool
  • Have a switch statement
  • Use the random number functions

YOU CANNOT:

  • Use global variables
  • Use the word goto
  • Use the break command outside a case statement

Solutions

Expert Solution

#include <iostream>
#include <ctime>
#include <unistd.h>

using namespace std;

int random()
{
    int x =0;
    while(!x)
    {
        x = rand() % 4;
    }
    return x;
}
void printSpace(int posA)
{
    int i = 0;
    while(i < posA)
    {
        cout << " ";
        i++;
    }
}
void stepMove(int step)
{
    int x = step-1;
    switch (x)
    {
    case 0:
        break;
    case 1:
        cout << " ";
        break;
    case 2:
        cout << "  ";
        break;
    case 3:
        cout << "   ";
        break;
    default:
        break;
    }

}
int minimum(int posA, int posB, int posC)
{
    if(posA <= posB && posA <= posC)
        return posA;
    else if(posB <= posA && posB <= posC)
        return posB;
    else
        return posC;
}
int raceA(char A,int posA)
{
    if(posA <= 80)
    {
    int step;
    step = random();
    printSpace(posA);
    if(posA < 80)
        stepMove(step);
    cout << A;
    posA = posA + step;
    printSpace(80-posA-1);
    cout << "|";
    }
    return posA;
}

int raceB(char B,int posB)
{
    if(posB <= 80)
    {
    int step;
    step = random();
    printSpace(posB);
    if(posB < 80)
        stepMove(step);
    cout << B;
    posB = posB + step;
    printSpace(80-posB-1);
    cout << "|";
    }
    return posB;
}
int raceC(char C,int posC)
{
    if(posC <= 80)
    {
    int step;
    step = random();
    printSpace(posC);
    if(posC < 80)
        stepMove(step);
    cout << C;
    posC = posC + step;
    printSpace(80-posC-1);
    cout << "|";
    }
    return posC;
}
void race(char A, char B, char C)
{
    int loop = 1;
    int posA = 0;
    int posB = 0;
    int posC = 0;
    char winner[3] = {'z','z','z'};
    int index = 0;
    bool passA = false;
    bool passB = false;
    bool passC = false;

    while(loop <= 81 )
    {
        posA = raceA(A,posA); cout << "\n";
        posB = raceB(B,posB); cout << "\n";
        posC = raceC(C,posC); cout << "\n";

        if(posA >= 80 && !passA)
        {
            winner[index] = A;
            passA = true;
            index++;
        }
        if(posB >= 80 && !passB)
        {
            winner[index] = B;
            passB = true;
            index++;
        }
        if(posC >= 80 && !passC)
        {
            winner[index] = C;
            passC = true;
            index++;
        }

        loop = minimum(posA,posB,posC);//cout << "loop:" << loop;
        sleep(1);
        cout << "\n";
        if(loop >= 80)
            loop = loop + 1;
    }
    cout << "\nShowing the Results in...";
    cout << "3 ";sleep(2);
    cout << "2 ";sleep(2);
    cout << "1 ";sleep(2);

    cout << "\nFirst Place:  car" << winner[0] << endl;
    cout << "Second place: car" << winner[1] << endl;
    cout << "Third place:  car" << winner[2] << endl;
}

int main()
{
    char A = 'A';
    char B = 'B';
    char C = 'C';
    int finish = 80;
    srand(time(0));
    cout << "Race starts: \n \n" ;
    race(A,B,C);
    cout << "\n\t\t ------Race Ends------";
    return 0;
}

Related Solutions

C Programming Game Scores Assignment Outcome: Student will demonstrate the ability to design a menu driven...
C Programming Game Scores Assignment Outcome: Student will demonstrate the ability to design a menu driven program. Student will demonstrate the ability to create and use a 2D array on the stack. Student will demonstrate the use of functions. Student will demonstrate good programming style. Program Specifications: *********************************************** ** MAIN MENU ** *********************************************** A) Enter game results B) Current Record (# of wins and # of losses and # of ties) C) Display ALL results from all games WON D)...
Generate 1000 random numbers from ??3? starting with standard normal random numbers in R.
Generate 1000 random numbers from ??3? starting with standard normal random numbers in R.
Write a function that will generate an array of random numbers. It needs to:
DO IN C++Write a function that will generate an array of random numbers. It needs to:Take 3 integers as parameters-The first is the minimum value-the second is the maximum value-the third is the size of the new array-create a dynamically allocated array of the correct size-generate a random number (between min and max) for each element in the array-return a pointer to the arrayCreate a main() function that tests the above function and displays the values in the random array.
Generate 1000 random numbers from ??2, 5? starting with standard normal random numbers in R.
Generate 1000 random numbers from ??2, 5? starting with standard normal random numbers in R.
Write the R code to generate five independent uniform random numbers and use that to generate...
Write the R code to generate five independent uniform random numbers and use that to generate 5 independent Bernoulli random variables with probability of success p = 0.4. please use RStudio. please do not use lab nor rbern for calculations.
"Gambling Greg" Assignment Outcomes: Demonstrate the ability to create and use structs Demonstrate the ability to...
"Gambling Greg" Assignment Outcomes: Demonstrate the ability to create and use structs Demonstrate the ability to create and use menus Demonstrate the ability to create and use an array of structs Demonstrate the ability to generate and use random numbers Program Specifications: Assume that gambling Greg often goes to the Dog Racing Track. Greg loves to bet on the puppies. In each race Greg will place a wager and pick a dog. The dog information will be stored in a...
Playing with strings Assignment Outcomes: Demonstrate the ability to create strings. Demonstrate the ability to manipulate...
Playing with strings Assignment Outcomes: Demonstrate the ability to create strings. Demonstrate the ability to manipulate strings. Demonstrate the ability to write well written code. Program Specifications: DESIGN and IMPLEMENT a short program that will: Allow the user to enter a string with up to 100 letters. Display the user-entered string: Forward Backward Vertical As a triangle made from the letters of the string Display the number of letters in the string. Once everything above is displayed, the program will...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100 and 300. Pick a number at random out of these 50 random variables. a. Determine the probability of the chosen number being greater than 200. This may be achieved by counting the numbers that are greater than 200 and dividing the count by 50. Make sure you, i.Formulate the appropriate if-conditions to check for a number being greater than 200 ii. Use a for-loop...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs Program Specifications: DESIGN and IMPLEMENT a program that will CREATE and use three different variables of type PERSON. Create a struct using the typedef command for a DATE. Create a struct for a PERSON with the following fields. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs Program Specifications: DESIGN and IMPLEMENT a program that will CREATE and use three different variables of type PERSON. Create a struct using the typedef command for a DATE. Create a struct for a PERSON with the following fields. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT