CSE 100 Assignment 4 Maximum points 50 pts Topics Structs
CSE 100 – Assignment #4 Maximum points: 50 pts Topics • Structs • Arrays • Loops • Functions Use the following Guidelines: • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). • Keep identifiers to a reasonably short length. • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects). • Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent. • Use white space to make your program more readable. Important Note: All submitted assignments must begin with the descriptive comment block. To avoid losing trivial points, make sure this comment header is included in every assignment you submit, and that it is updated accordingly from assignment to assignment. Note: There are 2 parts described – make sure you read all the pages Part 1: Writing Exercise: (10 pts) Include answers to the following questions in a comment block after your header comment block A) Do a little discovery learning and research the basic idea of a Class. Compare and contrast a Class and a Struct (5pts) B) Read Part 2… If you were going to implement this assignment with Object Orientation … how would that change the Car? (5pts) Part 2: Programming (40 pts) This project is more of a simulator than an input->output program. You may choose to add some input to the program though. Specifications (20 points) Text Based Car Race! Skills: • Input/Output • Arrays • Loops • Structs Basic Description: You are going to create a simple car race program using the ideas of Structs and functions to make several racing cars. These cars will “compete” to get to the end of the line first. The hard part will be creating and using the Car struct… but ultimately if you can make an array of integers and loop through that array increasing each integer … you can make this work too. Specifications: Define a struct called \"Car\" with the following member variables: • Total Odometer Miles • Speed in miles per hour • Driver Name • Sponsor • Car Number You should also include the following functions: • A function to make progress down the course • A function to convert your speed from MPH to MPS • A function to “pretty print” your car’s information including Number, Sponsor, Driver and Progress • A function to re-randomize speed • Include any useful functions you might need to accomplish the goal The total odometer miles should be initialized to zero, and speed initialized to a random value between 60 and 120. Create a list of 20 vehicles with driver and sponsor names. You should not re-use a driver name or sponsor name (note – worry about this specification last, if you can make the race work w/o this, that’s better than getting hung up on this algorithm) Your main program should simulate the progress of the vehicles in the race. You should accomplish this with a loop. Every 5 iterations of the loop (should be considered 1 minute of time), the vehicles pick a new random speed. Every iteration through the loop their odometer miles are updated: odometer_miles = odometer_miles + speed*time Since speed is in miles per hour, time should be in hours as well (1 minute is 1/60th of an hour). Remember for the sake of this assignment 1 minute = 5 loop iterations. The first car to reach the race’s limit (100 miles is a good number) should be declared the winner by printing the driver name and sponsor name. You should set this as a variable – consider asking the user for the value as well. +5 Extra Credit – Use correct object orientation instead of structs and functions. +3 Extra Credit – Output your racing results to a text-file as well as the screen. Here are some String arrays that you can use to extract Driver and Sponsors: string driverNameList = {\"William Swopes\", \"Margurite Miland\", \"Alva Liska\", \"Bruna Breen\", \"Luciana Deshotel\", \"Jeannetta Nesbitt\", \"Donny Ledger\", \"Modesto Tennant\", \"Imelda Hassler\", \"Karma Agar\", \"Janis Wisneski\", \"Micha Dillow\", \"Mattie Boulden\", \"Ethelyn Boswell\", \"Erica Corvin\", \"Marion Holliday\", \"Laticia Repka\", \"Desirae Guarino\", \"Ines Wallach\", \"Deloris Kimbler\", \"Elliot Shatley\", \"Millicent Koller\", \"Bess Medellin\", \"Marcy Lydick\", \"Garnet Mccabe\", \"Donna Tannehill\", \"Dusti Devillier\", \"Leland Slemp\", \"Keiko Dolph\", \"Maybell Berggren\", \"Jeni Crew\", \"Christi Birdwell\", \"Una Sprague\", \"Sheba Mirza\", \"Alanna Wawrzyniak\", \"Francina Rippel\", \"Fermin Layne\", \"Taren Stetson\", \"Serina Reveles\", \"Maegan Arvizo\", \"Tajuana Behringer\", \"Tamara Havel\", \"Anya Gemmill\", \"Mel Bustle\", \"Tandy Nash\", \"Hue Stefan\", \"Reina Streett\", \"Glennie Kist\", \"Latricia Li\", \"Juliette Bureau\"}; string listOfSponsors = {\"Abercrombie & Fitch Co.\", \"ABM Industries Incorporated\", \"Ace Hardware Corporation\", \"ACT Manufacturing Inc.\", \"Acterna Corp.\", \"Adams Resources & Energy, Inc.\", \"ADC Telecommunications, Inc.\", \"Adelphia Communications Corporation\", \"Administaff, Inc.\", \"Adobe Systems Incorporated\", \"Adolph Coors Company\", \"Advance Auto Parts, Inc.\", \"Advanced Micro Devices, Inc.\", \"AdvancePCS, Inc.\", \"Advantica Restaurant Group, Inc.\", \"The AES Corporation\", \"Aetna Inc.\", \"Affiliated Computer Services, Inc.\", \"AFLAC Incorporated\", \"AGCO Corporation\", \"Agilent Technologies, Inc.\", \"Agway Inc.\", \"Apartment Investment and Management Company\", \"Air Products and Chemicals, Inc.\", \"Airborne, Inc.\", \"Airgas, Inc.\", \"AK Steel Holding Corporation\", \"Alaska Air Group, Inc.\", \"Alberto-Culver Company\", \"Albertson\'s, Inc.\", \"Alcoa Inc.\", \"Alleghany Corporation\", \"Allegheny Energy, Inc.\", \"Allegheny Technologies Incorporated\", \"Allergan, Inc.\", \"ALLETE, Inc.\", \"Alliant Energy Corporation\", \"Allied Waste Industries, Inc.\", \"Allmerica Financial Corporation\", \"The Allstate Corporation\", \"ALLTEL Corporation\", \"The Alpine Group, Inc.\", \"Amazon.com, Inc.\", \"AMC Entertainment Inc.\", \"American Power Conversion Corporation\", \"Amerada Hess Corporation\", \"AMERCO\", \"Ameren Corporation\", \"America West Holdings Corporation\", \"American Axle & Manufacturing Holdings, Inc.\", \"American Eagle Outfitters, Inc.\", \"American Electric Power Company, Inc.\"}; Breaking it down • Create a struct called RaceCar or simply Car with the listed member variables • Create an array of 20 Cars each with a Driver and Sponsor, initialized as seen above. o Create logic to prevent a duplicate name, but once again – don’t focus on this, get the race to work first Create a function to report the progress of the cars. • Create a loop to make the cars progress towards the goal! Make sure you frequently report the progress of the cars. • Declare a winner by finding the first car to cross the finish line! o You may consider writing a sort for your array to make this easier. NOTES: • Remember, you are only turning one file (yourName_Assn4.cpp) in that file should include: o Comment header identifying yourself and the program information o Comment block for written portion o Code for part 2 (40 points) § 20 pts (listed above) specifications § 12 pts for code quality § 8 points for efficiency • Remember Code Quality includes commenting. You should not comment every line, but you should insert comments explaining what you are doing. • Use only the C++ statements that have been covered in class to date. You may use any and all techniques we’ve used in class so far!! Everything (delivered in class or labs) at this point is fair game! If you want to, you may use Object Orientation. If in doubt, ask. If you use them, then you lose the points of task. Complete each step one by one. Don\'t copy any code developed by others. Don\'t give your code to others. Don\'t use any algorithm, which you cannot understand. Your assignment file is checked by the MOSS (by Stanford Univ.), which is a program to detect cheatings. Hints and Help: This assignment can seem very intimidating … but it’s actually just solving a bunch of small problems. Don’t let it overwhelm you and don’t over think the problems. Start by creating your struct and making exactly one car complete the race. You can even do this without writing ay functions. After you are able to do this … start writing the functions and make them work with that single car Remember you can return Structs from functions – so you can use this to over-write previous information… myCar = DoSomething(myCar); After you have successfully set up functions, then incorporate the array of cars and the logic to work through everything. Here is an outline of the main algorithm logic flow: While there is not a winner For each Car in the Array The Car should make progress Output the Car’s progress If this iteration (of the while loop) is a multiple of 5 Then re-randomize the speed of the Car If this car has traveled more than the race’s length Set winner to be true Record the index of the winning car The loop is broken when the winner is found Output the winner and their information!
How do I do this?
Solution
Part 1 : Ans
Class : Class is an advance structure, which contain data members and data functions.
Class structure
By default members are private. By default members are public.
By default privat inhertance . By default public inheritance .
****************************************************************************************************
Part 2 : Ans =>
#include<iostream>
#include<stdlib.h>
using namespace std;
struct car {
float totalOdometerMiles, Speed;
string driverName, Sponsors;
int carNumber;
};
struct car C[20];
int re_randomizeSpeed();
/* speed convert MPH to MPS */
float speedMPH_to_MPS (float &speed){
speed = speed * 0.44704;
}
/* Display information of cars */
void pretyyPrint(int n[], string driverNameList[], string listOfSponsors[]){
cout<<\"CarNumber\\tDriverName\\t\\tSponsor\\t\\tDistance(MPH)\ \";
cout<<\"----------------------------------------------------------------------------\ \";
for(int i=0; i<20; i++){
C[i].carNumber = n[i];
C[i].driverName =driverNameList[i];
C[i].Sponsors = listOfSponsors[i];
C[i].totalOdometerMiles = re_randomizeSpeed();
cout<<C[i].carNumber<<\"\\t\\t\"<<C[i].driverName<<\"\\t\\t\"<<C[i].Sponsors<<\"\\t\\t\"<<C[i].totalOdometerMiles<<\"\ \";
}
}
/* Display the details about winner */
void winner() {
float max = C[0].totalOdometerMiles;
int M = 0;
for(int i=0; i<20; i++){
if(max < C[i].totalOdometerMiles)
M = i;
}
cout<<C[M].carNumber<<\"\\t\"<<C[M].driverName<<\"\\t\\t\"<<C[M].Sponsors<<\"\ Distance : \"<<C[M].totalOdometerMiles<<\" MPS\ \";
}
/* Car pick the random speed */
int re_randomizeSpeed(){
for(int i=0; i<5 ;i++);
int time = 1, odometerMiles = 0;
float speed = (rand()%40 + 60);
speedMPH_to_MPS(speed);
odometerMiles = odometerMiles + speed*time;
return odometerMiles;
}
int main()
{
int car_Number[] = {5676,5676,5768,6766,5665,7678,7898,8976,9787,8797,8798,8097,6785,6576,4655,6574,6733,4563,4554,5445,};
string driverNameList[] = {\"William Swopes\", \"Margurite Miland\", \"Alva Liska\", \"Bruna Breen\", \"Luciana Deshotel\", \"Jeannetta Nesbitt\", \"Donny Ledger\", \"Modesto Tennant\", \"Imelda Hassler\", \"Karma Agar\", \"Janis Wisneski\", \"Micha Dillow\", \"Mattie Boulden\", \"Ethelyn Boswell\", \"Erica Corvin\", \"Marion Holliday\", \"Laticia Repka\", \"Desirae Guarino\", \"Ines Wallach\", \"Deloris Kimbler\"};
string listOfSponsors[] = {\"Abercrombie & Fitch Co.\", \"ABM Industries Incorporated\", \"Ace Hardware Corporation\", \"ACT Manufacturing Inc.\", \"Acterna Corp.\", \"Adams Resources & Energy, Inc.\", \"ADC Telecommunications, Inc.\", \"Adelphia Communications Corporation\", \"Administaff, Inc.\", \"Adobe Systems Incorporated\", \"Adolph Coors Company\", \"Advance Auto Parts, Inc.\", \"Advanced Micro Devices, Inc.\", \"AdvancePCS, Inc.\", \"Advantica Restaurant Group, Inc.\", \"The AES Corporation\", \"Aetna Inc.\", \"Affiliated Computer Services, Inc.\", \"AFLAC Incorporated\", \"AGCO Corporation\"};
pretyyPrint(car_Number, driverNameList, listOfSponsors);
cout<<\"***** Winner *****\ \";
winner();
}


