Monday, November 29, 2010

/* Sean Lunceford
COP 3223
Sec 3
Programming Assignment #1
11/7/2010
Program Description: A program to combine several of are previous programs */


#include
#include
#include
#define PI 3.14159
#define INCHES_IN_MILE 63360
#define TIME_LEFT (timetillwork-(traveltime+(timepast*2)))
#define TIME_LEFT2 (timetillwork-traveltime)
#define NUM_SEC_MAX 30*60
#define FILE_NAME_SIZE 100
#define MPG_ARRAY_SIZE 6

//establishing all my funtions to be called
void printrating(int MPG);
void printmenu();
double tripfueleff( double radius, double triptime);
double totalturn_1 (double totalturn, double turn);
double totalgas_1 (double totalgas, double gas);
void printfuelgraph(FILE* ifp);
void work( double traveltime, double timetillwork, double timepast);
double findfueleff( double radius, double revolutions, double gallons);
void multgame( int inputturns);
void logo( int outrad, int inrad);
void printfuelgraph(FILE* ifp);


int main() {
char filename[MPG_ARRAY_SIZE];
//declaring my variable
int outrad=0, inrad=0;
int menuchoice, inputturns;
double radius, revolutions, gallons, fueleff;
double eff, traveltime, timetillwork, timepast;
double triptime, fueleff1;

// specifying what to do with each coice
while (menuchoice != 6)
{
printmenu();

scanf("%d",&menuchoice);



if (menuchoice==1)

{
printf("How long is your trip, in minutes?\n");//ask the user for the radius of thier tires and their toral trip time
scanf("%lf", &triptime);
printf("What is the radius of your tires, in inches?\n");
scanf("%lf", &radius);
tripfueleff(radius, triptime);

}

if(menuchoice==2)
{
printf("How long does it take to drive to work in minutes?\n");
scanf("%lf", &traveltime);
printf("How many minutes before work did you start?\n");
scanf("%lf", &timetillwork);
printf("How many minutes did it take to realize you forgot an item?\n");
scanf("%lf", &timepast);
work(traveltime, timetillwork, timepast);
}

if(menuchoice==3)
{
printf("Enter the outer radius of your wheel?\n");//Asking the user for the inner radius and the outside radius.
scanf("%d",&outrad);
printf("Enter the inner radius of your wheel?\n");
scanf("%d",&inrad);
logo(outrad,inrad);
}

if(menuchoice==4)
{
printf("How many problems do you want?\n");// asking the user for inputs
scanf("%d", &inputturns);
multgame(inputturns);
}

if(menuchoice==5)
{
FILE* ifp;
printf("Please specify your gas mileage file.\n");
scanf( "%s", filename);
ifp = fopen(filename, "r");
printfuelgraph(ifp);
}

if(menuchoice > 6)
printf("Please enter a valid number, try again\n\n"); // stating if they enter a incoreect number print menu again

if (menuchoice==6)
break;
}
system("pause");
return 0;
}

void printmenu()
{
printf("1) Calculate fuel efficiency for a trip.\n");
printf("2) Determine whether or not to go back home and pick up an item.\n");
printf("3) Print the car logo (a wheel) to the display.\n");
printf("4) Play a multiplication game for the kids.\n");
printf("5) Print a visual display of the recent fuel efficiency in five minute intervals.\n");
printf("6) Quit\n");
return ;
}

double findfueleff( double radius, double revolutions, double gallons)
{
double fueleff;
fueleff = (((2*PI*radius*revolutions)/INCHES_IN_MILE)/gallons);
return fueleff;
}

double tripfueleff( double radius, double triptime){
int revolutions, minutes=0, intervals=1;
double mpg, totalrev=0, totalgallons=0, gallons, totalmpg;



while(minutes
printf("During time interval #%d, how many revolutions did your car's tires make?\n", intervals);//asl the user for the revolutions and gallons used in the 5 minutes interval
scanf("%d", &revolutions);
printf("During time interval #%d,how many gallons of gas did your car use?\n", intervals);
scanf("%lf",&gallons);
printf("\n");
intervals=intervals+1;

mpg=findfueleff(radius, revolutions, gallons);
printf( "Time %d - %d minutes: Your car averaged %.2lf miles per gallon.\n", minutes,minutes+5, mpg);// tell them the mpg for the the 5 minute interval
minutes=minutes+5;//esablishing the five minute intervals
totalgallons=totalgallons+gallons;//establishing the total gallons
totalrev=totalrev+revolutions;//establihing the total revolutions
printf("\n");
}

totalmpg=findfueleff(radius, totalrev, totalgallons);//using another function in this function

printf("your total mpg was %.2lf\n",totalmpg);

printrating(totalmpg); // using a different function in this function

printf("\n");
return totalmpg;
}


void printrating(int MPG)
{
if (MPG >= 40)//MPG >=40
printf("Your car gets exellent gas mileage.\n");
else if (MPG >= 30)//(30 printf("Your car gets good gas mileage.\n");
else if (MPG >= 20)//(20 printf("Your car gets average gas mileage.\n");
else if (MPG >= 0)//(0 printf("Your car gets poor gas mileage.\n");
printf("\n");
}

void work( double traveltime, double timetillwork, double timepast)
{
if ((traveltime+(timepast*2)) <= timetillwork)
{
printf("Go back home and pick up the item.\n");
printf("You will arrive at work with %.0lf minutes to spare.\n", TIME_LEFT);
}
else if ((traveltime+(timepast*2)) >= timetillwork)
{
printf("Just go to work, you'll have to do without the item.\n");
printf("will arrive at work with %.0lf minutes to spare.\n", TIME_LEFT2);
}
printf("\n");
return ;
}

void logo( int outrad, int inrad)
{
int col=0, row=0, distance_2 ;
for(col=-outrad; col<(2*outrad); col++){// declaring my loops to set up a x,y grid using colum and rows.
for(row=-outrad; row<(2*outrad); row++){

distance_2=(pow((row-(outrad)),2)+pow((col-(outrad)),2));//distacne formula to figure out how far away the point is from the cnetter

if(distance_2+1 > pow(outrad,2))//if the distance is longer the the radius print space
printf(" ");

else if(distance_2+1 <> pow(inrad,2))//if the distance is between the outter and inner radius print *
printf("*");

else if(distance_2+1 printf("$");

else
printf("+");//everything else, meaning the axle, print +
}


printf("\n");
}

return;
}


void multgame( int inputturns)
{
int turns, num1, num2, answer, answertry;

int start=time(0);//start timer for how long it takes them to complete problems

for(turns=1;turns<=inputturns;turns++)
{

srand(time(0));//seeding the time random
num1 = 1 + rand()%13;// generating a random number
num2 = 1 + rand()%13;// generating a random number
answer= num1*num2;// getting the answer for the random miliplication table

printf("Answer %dX%d\n",num1, num2);//printing the mulitplication problem
scanf("%d", &answertry);

if(answertry!=answer)
{//if the useer is in corect tell them and ask SAME problem again.
while(answertry!=answer)
{
printf("Incorrect, Try again.\n");
printf("Answer %dX%d\n",num1, num2);
scanf("%d", &answertry);
}
}

else
continue;//if they are right go to the next radome mulitplication set.
}

int end = time(0);// stoping the timer

int timespent =end - start;
printf("Your code took %d seconds.\n", timespent);// telling the user how long they took.
printf("\n");
return ;
}


void printfuelgraph(FILE* ifp)
{

int totalmins, radius, mpgindex, second, lmpg, skippedtime,skippedamount, skips;
double totalturn, totalgas, totalmile, turn, gas, totalmiles, time, skip1, skip2;

//setting up array sizes

double mpg[MPG_ARRAY_SIZE];
char filename[FILE_NAME_SIZE];


// reading the first two values in the file at setting them as total mins radius

fscanf(ifp,"%d %d", &totalmins, &radius);

//figuring out how many lines need to be skipped ti get the last 30 mins
skippedtime=totalmins/5-6;
skippedamount=skippedtime*300;

//excliduing if there is less then thirty minutes in which case nothing
if(skippedtime>=0)
{
for(skips=1;skips<=skippedamount; skips++)
{
fscanf(ifp,"%lf %lf", &skip1, &skip2);
}
}
// saying if there is less the 6 intervals to put 0 in the array for as many places
// that wont be filled before you put in the values.
second=0;
if((totalmins/5) {
for(;second<(MPG_ARRAY_SIZE-(totalmins/5));second++)
{
mpg[second] = 0;
}
}
// reading in the 300 lines to get an average, then taking each mpg and storing
//it to the array for each 5 minute segment for the last 30 minutes
for(;second {
totalgas=0;
totalturn=0;

for(time=1;time<=300;time++)
{
fscanf(ifp,"%lf %lf", &turn, &gas);
totalgas=totalgas_1(totalgas,gas);
totalturn= totalturn_1(totalturn,turn);
}
// saying what happens when you have positive distance with no gas use
//and what happens with no movement and no gass
if(totalgas==0 && totalturn>0){
mpg[second]=100;
}
else if(totalgas==0 && totalturn==0){
mpg[second]=0;
}
else
{
//fix into a called function
mpg[second]=findfueleff(radius, totalturn, totalgas);
}

}
//graphing function to read line by line all the values from the array
for (lmpg = 95; lmpg >= 0; lmpg -= 5)
{

printf("%2d ", lmpg);

for (second=0; second {
if (lmpg >= mpg[second])
{
printf(" ");
}
else
{
printf("***** ");
}
}
printf("\n");
}
printf(" -----------------------------------\n");
printf(" 25-30 20-25 15-20 10-15 05-10 00-05\n");



return ;
printf("\n");
}


double totalgas_1 (double totalgas, double gas){
return totalgas+gas;
//Funtion to total the rev, contrained to work for non zero value due to if statements in code
}
double totalturn_1 (double totalturn, double turn){
return totalturn+turn;
}

No comments:

Post a Comment