Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Dan built his own car and he wants to test how fast it can go. The file carGears

ID: 3794822 • Letter: D

Question

Dan built his own car and he wants to test how fast it can go. The file carGears.cpp given is a simulation of how his car works. You are supposed to create the chooseGear() function to make the car go as fast as possible by the end of the loop (it will not compile until you make this properly). The accelerate() function is purposefully confusing to make you find the best gears through trial and error (rather than math).

For this problem, you need to modify carGears.cpp (by making chooseGear()) and resubmit this modified file. You may want to slightly modify main() to facilitate easier information gathering, but DO NOT CHANGE EITHER main() OR accelerate() IN YOUR FINAL SUBMISSION. Although the units in the program are not mph or km/h, choosing the appropriate gear works similar to the real world: once you hit a certain speed (or rpm) you will want to switch to a higher gear. The formula in accelerate() will never make you accelerate faster if you go to a lower gear at a higher speed/velocity. (i.e. gears should monotonically increase with speed).

For this program, your points will be based on how fast you can get your car going at the end of the program: Speed:

7.5+ = 20 points

Speed: 7.25 - 7.5 = 19 points

Speed: 7.00 - 7.25 = 18 points

... Speed: 3.00 - 3.25 = 2 points

Speed: 2.75 - 3 = 1 point

Example 1:

Final velocity:

7.73639

Example 2:

Final velocity:

7.47622

// carGears.cpp

Explanation / Answer


// carGears.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

double accelerate(double v, int g);
int chooseGear(double v);

// YOU CANNOT CHANGE main() IN YOUR SUMBISSION
int main() {
int time = 70;
double vel = 0;
  
for(int i=0; i < time; i++)
{
int gear = chooseGear(vel);
  
vel += accelerate(vel, gear);
}
  
cout << "Final velocity: " << vel;
return 0;
}

// new chooseGear funciton using trail and error method, which gives the highest possible velocity
int chooseGear(double vel)
{
if(vel < 1)
return 1;
else if(vel < 2)
return 2;
else if(vel < 3)
return 3;
else if(vel < 5)
return 4;
else
return 5;
}

// You are not supposed to know the formula for accelerate.
// ... gl trying to find it out
// YOU CANNOT CHANGE THIS FUNCTION IN YOUR SUMBISSION
double accelerate(double v, int g)
{
if(g<1 || g >6)
{
cout << "No such gear!";
exit(1);
}
double i=v;
double n=54%3;
int x = 256;
int y = 256;
for(int i=0; i < g; i++)
{
x>>=i%true|1;
y>>=!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!true;
if(!(i^(299&1750))) {x+=x/2;}
if(!((i-1)*(i+1)-2*(i-1)+-4)) {x&=y;x<<=1;}
}
double j=true;
int s=(1<<2)+(1<<0);
int m=0x0F>>1;
int b=0x21;
double V=0;
for(int z=1; z < 7; z++)
{
double X=1;
for(int j=1; j < 7; j++)
{
if(j!=z)
{
X*=(g-j)/(z-j);
}
}
X*=(z==(199^0xC2)? -b/4.0 : z>(3^1)? !(z^(132&68))?-(s|=7+(m>>=1)):-s:s);
s^=m;
!s?V+=n+i*(4+(g>4?4-g:g-4)-v*x/64.0):V*=j+(m^g)*(i-j*v);   
m>>=1;
V+=X;
}
return V/10;
}


/*
output:

Final velocity:
7.21022

*/