Prep for my first lab in C++

I am taking my first coding class this semester, and wanted to get some help with ways to do some of the coding. Below is a non graded practice to help get ready for our first lab and I would like to know how you all would go about the code.



Introduction
The 2018 Winter Olympics in Pyeong Chang, South Korea, were very exciting with the US winning a total of 23 medals. This lab takes a look at the short track speed skating event whose finals had tickets starting at 150,000 Won (about $140 on 1/17/2018). Of course, airfare and lodging would have been over $1,200 and you’d have had to miss class to go ...

So, what is short track speed skating? “Short track speed skating is an umbrella term that encompasses four men's and four women's races. Each race takes place on an oval track of ice that is 111.2 meters long, while the entire sheet of ice is 60 meters long and 30 meters wide.
The simplest way to conceptualize short track speed skating—and there's really no reason to complicate things—is by thinking of it as a track and field race, but on ice. The one main difference between the short-distance track races and short track speed skating is that there are no lanes in the short track, so there is a bit of an advantage for those who start on the inside starting position.
Skaters travel in a counterclockwise direction, which means only left turns are made, just as in NASCAR.” (https://www.si.com/olympics/2017/12/18/2018-winter-olympics-rookies-guide-short-track-speed-skating-pyeongchang 1/17/2018)

Individual (non-relay) short track speed skating has 3 different distances: 500, 1000 and 1500 meters. See this video of the 500 meter short track speed skating finals from the 2006 Olympics in Turin, Italy where the United States competitor Apolo Ohno won a gold medal: https://www.olympic.org/videos/apolo-anton-ohno-becomes-the-fastest-man-in-500m

Detailed Information
This program computes the average acceleration of a short track speed skater during each lap of a competition. The program asks the user for a short track event (500, 1000, or 1500 meter), and for the name of the skater. Then, for each 100 meter “lap”, the program asks the completion time. The program computes and prints the acceleration at the end of each lap. Finally, the program asks the user for another short track event and continues processing until the user enters 0 for the event as shown below in the Sample Execution.

The formula for calculating average acceleration in meters per second squared, a, from point A to point B is given below where sA = average speed at A (from the previous point to A), sB = average speed at B (from A to B), tA = time at A, tB = time at B.
a=(sB-sA)/(tB-tA)

A positive acceleration means the skater sped up; a slower one means the skater slowed down.

Note: if we wanted to compute the instantaneous acceleration of a skater, the acceleration at any particular moment in time, not just the average acceleration between two points that are 100 meters apart as above, we would need to use Calculus. So, pay attention in your Calculus classes!

Sample execution (using times from above video for Apolo Ohno)
Welcome to the Short Track Speed Skating Analyzer!
Please choose an event
1 500 meters
2 1000 meters
3 1500 meters
0 exit
Event: 1
Skater first and last name: Apolo Ohno
End time of lap 1 (in seconds): 6.65
Acceleration in m/s^2: 2.26
End time of lap 2 (in seconds): 15.83
Acceleration in m/s^2: -0.45
End time of lap 3 (in seconds): 24.6
Acceleration in m/s^2: 0.06
End time of lap 4 (in seconds): 33.14
Acceleration in m/s^2: 0.04
End time of lap 5 (in seconds): 41.93
Acceleration in m.s^2: -0.04
End of analysis for Apolo Ohno
Please choose an event
1 500 meter
2 1000 meter
3 1500 meter
0 exit
Event: 0
Thank you for using the Short Track Speed Skating Analyzer!

DO NOT write your program under the assumption that the user will choose a valid event number. If the user enters an integer outside the range [0, …, 3], print an error message, Invalid event x where x is the integer entered. Then print the main menu again. YOU MAY write your program under the assumption that the user will enter an integer (not a floating-point number or a bunch of letters, for example).

Display exactly 2 digits to the right of the decimal for all accelerations as shown in the Sample Execution.

Hint: draw a number line and compute acceleration by hand for all laps for the Sample Execution above. This will help you figure out the algorithm.
all programming problems ... you do some basic stuff.
- what data do I need to store (names, averages)
- how should I store it (can i use a built in container or do I need my own class)
- what do I need to do with it (present to user, get from user, compute a value, validate it)

what do I need to know to do this? (Solve the example problems by hand, re-familiarize yourself with the basic math and physics here). Then resolve those using c++ like ideas... what variables do you need, how do you do the steps?

can you do this with the highschool algebra equations (d = 1/2 att +vo etc) or do you really need to do calc? If you need to do calc, is it just the basic derivation formula f(x1)- f(x2) /(x1-x2) ... if you need that, do you need a routine for f(x) ? ... etc (the highschool formulas came from calc, of course, but you don't have to reinvent that to use them).

I personally would knock the math part out first. write just some basic c++ code to solve the equations you need, hard code the example data and get it working. Then you can wrap the I/O and data storage around it.

this is a pretty advanced lab for first class. Did you accidentally sign up for the 2nd class instead of the first? Or have you been learning the things you need to know to do this in the classroom already?
Last edited on
I agree with jonnin, this practice exercise you found is not meant for beginners.

With that in mind, you can breath a sigh of relief. But to solve your problem...

I would create a struct that contained all the hard data, name, and time info for each lap. Then work on a function that took this data and output the desired results.

I guess you could say, I would work it out in the opposite order of jonnin. :)
I don't think this is hard. The problem statement gives you the formula to use. Here are a few pointers:
- To compute the acceleration, you need to remember the time and speed from the previous lap. For the first lap, the previous time and speed are both zero. For the second lap, they are the time and speed from the first lap, etc. So after reading each line of input, you compute the output, print it, and then set the previous time & speed variables to be the newly entered values.

- Each race has a different number of laps. After the user selects a race, set a variable to the number of laps. Then loop that many times, prompting for the times/speeds for the lap.
I don't think this is hard. The problem statement gives you the formula to use.

Agreed. Simple loops.
We have had 4 classes so far, the prof is really hard to understand and moves quickly and refuses to stop to go back over things. Also yes this is the 1010 class, but the issue comes from the prof not stopping to explain saying that is what the tutoring center is for, but I work full time during their hours. I think my biggest issue is getting my mind around a set structure for things.
see what you can do. post it here, and ask if you get stuck. Use the things from your book and class. We can help if you get in trouble.
#include<iostream>
#include<iomanip> // to use setprecision() and fixed
using namespace std;
int main()
{
int choice,pos; // choice
char fname[50],lname[50];
while(true)
{
cout<<"\nWelcome to the Short Track Speed Skating Analyzer!";
cout<<"\nPlease choose an event\n1 500 meters\n2 1000 meters\n3 1500 meters\n0 exit";
cout<<"\nEvent: ";
cin>>choice;

int x=choice;
float time,accelaration,initialspeed=0.0,finalspeed,initialtime=0.0,finaltime;
switch(choice)
{
case 1:
cout<<"Skater first and last name:";
cin>>fname>>lname;
for(int i=1;i<=5;i++)
{
pos=100*i;
cout<<"\nEnd time of lap "<<i<<" (in seconds): ";
cin>>time;
finaltime=time;
float timeinterval=finaltime-initialtime;
finalspeed=pos/time;
accelaration=(finalspeed-initialspeed)/timeinterval;
initialspeed=finalspeed;
initialtime=finaltime;
cout<<"\nAcceleration in m/s^2:"<<fixed<<setprecision(2)<<accelaration;
}
cout<<"\nEnd of analysis for "<<fname<<lname;
break;
case 2:
cout<<"Skater first and last name:";
cin>>fname>>lname;
for(int i=1;i<=10;i++)
{
pos=100*i;
cout<<"\nEnd time of lap "<<i<<" (in seconds): ";
finaltime=time;
float timeinterval=finaltime-initialtime;
finalspeed=pos/time;
accelaration=(finalspeed-initialspeed)/timeinterval;
initialspeed=finalspeed;
initialtime=finaltime;
cout<<"\nAcceleration in m/s^2:"<<fixed<<setprecision(2)<<accelaration;
}
cout<<"\nEnd of analysis for "<<fname<<lname;
break;
case 3:
cout<<"Skater first and last name:";
cin>>fname>>lname;
for(int i=1;i<=15;i++)
{
pos=100*i;
cout<<"\nEnd time of lap "<<i<<" (in seconds): ";
finaltime=time;
float timeinterval=finaltime-initialtime;
finalspeed=pos/time;
accelaration=(finalspeed-initialspeed)/timeinterval;
initialspeed=finalspeed;
initialtime=finaltime;
cout<<"\nAcceleration in m/s^2:"<<fixed<<setprecision(2)<<accelaration;
}
cout<<"\nEnd of analysis for "<<fname<<" "<<lname;
break;
case 0:
cout<<"\nThank you for using the Short Track Speed Skating Analyzer!";
exit(0);
default:
cout<<"\nInvalid event "<<x;
break;
}
}
return 0;
}
Not bad. Just needed a slight tweek.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
 #include<iomanip> // to use setprecision() and fixed
 using namespace std;
 int main()
 {
 int choice,pos; // choice
 char fname[50],lname[50];
 while(true)
 {
 cout<<"\nWelcome to the Short Track Speed Skating Analyzer!";
 cout<<"\nPlease choose an event\n1 500 meters\n2 1000 meters\n3 1500 meters\n0 exit";
 cout<<"\nEvent: ";
 cin>>choice;

 int x=choice;
 float time,accelaration,initialspeed=0.0,finalspeed,initialtime=0.0,finaltime;
 switch(choice)
 {
 case 1: 
 cout<<"Skater first and last name:";
 cin>>fname>>lname;
 for(int i=1;i<=5;i++)
 {
 pos=100*i;
 cout<<"\nEnd time of lap "<<i<<" (in seconds): ";
 cin>>time;
 finaltime=time;
 float timeinterval=finaltime-initialtime;
 finalspeed=pos/time;
 accelaration=(finalspeed-initialspeed)/timeinterval;
 initialspeed=finalspeed;
 initialtime=finaltime;
 cout<<"\nAcceleration in m/s^2:"<<fixed<<setprecision(2)<<accelaration;
 }
 cout<<"\nEnd of analysis for "<<fname<<lname;
 break;
 case 2:
 cout<<"Skater first and last name:";
 cin>>fname>>lname;
 for(int i=1;i<=10;i++)
 {
 pos=100*i;
 cout<<"\nEnd time of lap "<<i<<" (in seconds): ";
 cin>>time;
 finaltime=time;
 float timeinterval=finaltime-initialtime;
 finalspeed=pos/time;
 accelaration=(finalspeed-initialspeed)/timeinterval;
 initialspeed=finalspeed;
 initialtime=finaltime;
 cout<<"\nAcceleration in m/s^2:"<<fixed<<setprecision(2)<<accelaration;
 }
 cout<<"\nEnd of analysis for "<<fname<<lname;
 break;
 case 3:
 cout<<"Skater first and last name:";
 cin>>fname>>lname;
 for(int i=1;i<=15;i++)
 {
 pos=100*i;
 cout<<"\nEnd time of lap "<<i<<" (in seconds): ";
 cin>>time;
 finaltime=time;
 float timeinterval=finaltime-initialtime;
 finalspeed=pos/time;
 accelaration=(finalspeed-initialspeed)/timeinterval;
 initialspeed=finalspeed;
 initialtime=finaltime;
 cout<<"\nAcceleration in m/s^2:"<<fixed<<setprecision(2)<<accelaration;
 }
 cout<<"\nEnd of analysis for "<<fname<<" "<<lname;
 break;
 case 0:
 cout<<"\nThank you for using the Short Track Speed Skating Analyzer!";
 exit(0);
 default:
 cout<<"\nInvalid event "<<x;
 break;
 }
 }
 return 0;
 } 
very nice work.
little stuff: don't use exit(0). It is intended to be used as an error-exit, not as a normal way to exit the program. use return(0) instead, or let the natural return(0) at the end of main handle it (you would have to change your while(true) to while(choice != 0) to do this, initialize choice to not zero, or (better) use a do/while loop so it loops once, gets choice, and while's out on zero).

soon you will learn about functions. if you already know about them, can you take the repeated code in the cases and call a single function instead? If you have not seen this yet, wait for it, it will be soon in your classwork.




Last edited on
Double check your math. You'll find that if you run the program with the prof's input, your results are different.

Any time you find yourself writing duplicate code, it should be a red flag that there's a different way to do it. Here is a version of your program with that avoids the duplicates.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<iomanip>				 // to use setprecision() and fixed
using namespace std;
int
main()
{
    int choice;    // You don't need pos
    char fname[50], lname[50];
    while (true) {
	cout << "\nWelcome to the Short Track Speed Skating Analyzer!";
	cout <<
	    "\nPlease choose an event\n1 500 meters\n2 1000 meters\n3 1500 meters\n0 exit";
	cout << "\nEvent: ";
	cin >> choice;

	// validate the choice
	if (choice < 0 || choice > 3) {
	    cout << "\nInvalid event " << choice;
	    continue;   // this goes back to the top of the loop.
	}

	if (choice == 0) break;    // break out of the while loop

	// If you get here then the choice is 1, 2, or 3

	// Get the name
	cout << "Skater first and last name:";
	cin >> fname >> lname;
	
	int laps = 5*choice;    // math is your friend.
	
	// Now compute the values
	float time, accelaration, initialspeed = 0.0, finalspeed, initialtime = 0.0, finaltime;
	
	for (int i = 1; i <= laps; i++) {
	    cout << "\nEnd time of lap " << i << " (in seconds): ";
	    cin >> time;
	    finaltime = time;
	    float timeinterval = finaltime - initialtime;
	    finalspeed = XXXX; // Your bug is here. finalspeed shoudl be the speed for THIS LAP.
	    accelaration = (finalspeed - initialspeed) / timeinterval;
	    initialspeed = finalspeed;
	    initialtime = finaltime;
	    cout << "\nAcceleration in m/s^2: " << fixed << setprecision(2) <<
		    accelaration;
	    }
	cout << "\nEnd of analysis for " << fname << lname;
    }
    return 0;
}

Did I not say this crap wasn't easy...

Anyways, the math error can be solved by fixing these two lines....

pos=100*i;

Should be...

pos=100;

Then...

finalspeed=pos/time;

Should be...

finalspeed=pos/timeinterval;

Topic archived. No new replies allowed.