Friday, September 4, 2009

2009 Engineering Computing Assignment Ver1

Question:
Write a program that will ask the user to choose among these three choices/modules:
1) Weather balloons
2) Compass Heading
3) Drag Force

Each choice/module is described below:

Choice 1: Weather balloons
Weather balloons are used to gather temperature and pressure data at various altitudes in the atmosphere. The balloon rises because the density of the helium inside the balloon is less than the density of the surrounding air outside the balloon. As the balloon rises, the surrounding air becomes less dense, and thus the balloon’s ascent slows until it reaches a point of equilibrium.

During the day, sunlight warms the helium trapped inside the balloon, which causes the helium to expand and become less dense; thus, the balloon will rise higher. During the night, however, the helium in the balloon cools and becomes more dense; thus, the balloon will descend to a lower altitude.

The next day, the sun heats the helium again and the balloon rises. Over time, this process generates a set of altitude measurements that can be approximated with a polynomial equation. Assume that the following polynomial represents the altitude or height in meters during the first 48 hours following the launch of a weather balloon.

h(t) = -0.12t4 + 12t3 – 380t2 + 4100t + 220

where the units of t are in hours. The corresponding polynomial model for the velocity in meters per hour of the weather balloon is

v(t) = -0.48t3 + 36t2 - 760t + 4100


Write a program that will print a table of the time, altitude and the velocity for this weather balloon using units of meters and meters per second. Let the user enter the start time, the increment in time between lines of the tables and the ending time, where all the values must be less than 48 hours. Print a message to the user that specifies an upper bound of 48 hours. Make sure that your program checks the user input stays within the proper bounds. If the are errors, ask the user to reenter the complete set of report information.

After your table is printed out, also print the peak altitude and its corresponding time. Your program should also check that the final time is greater than the initial time. If it is not, ask the user to reenter the complete set of report information.


Choice 2: Compass Heading
While spending the summer as a surveyor’s assistant, you have been given a task to write a program that will transforms compass heading in degrees (0 to 360) to compass bearings. A compass bearing consists of three items: the direction you face (north or south), an angle between 0 and 90 degrees, and the direction you turn before walking (east or west).

For example, to get the bearing for a compass heading of 110.0 degrees, you would first face due south (180 degrees) and then turn 70.0 degrees east (180.0 – 70.0 is 110.0). Therefore, the bearing is South 70.0 Degrees East.

For the cardinal directions, the output should be the following:

Direction Heading Output
North 0.0 Compass Bearing: North 0.0 Degrees East
East 90.0 Compass Bearing: North 90.0 Degrees East
South 180.0 Compass Bearing: South 0.0 Degrees West
West 270.0 Compass Bearing: North 90.0 Degrees West


Choice 3: Drag Force
When an automobile is moving through the atmosphere, it must overcome a force (or wind resistance) called drag that works against the motion of the vehicle. The drag force can be expressed as:

F = ½ Cd * A * ρ * V2

where F is the force(in Newton), Cd is the drag coefficient, A is the projected area of the vehicle perpendicular to the velocity vector (in m2), ρ is the density of the gas or fluid through which the body is traveling (1.23 kg/m3), and V is the body’s velocity.

Write a program that allows a user to input A and Cd interactively to compute the drag force giving the range of velocities from 0 m/s to 40 m/s and display a table showing the drag force for the input shape given.




Answer:

/************************************************************************
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX *
* XXXXXXX ENGINEERING COMPUTING *
* 3E XXXX *
* Assignment 1 *
* Group members (by alpha order) : *
* *
* *
* Due Date: xxxxxxxx *
* *
* Lecturer's name: Ms. xxxxxxxxx *
* *
* In the main function, switch case was used to select user option. *
* User input was checked if it is numerical else, it will return *
* invalid input to the screen and allow user to retry. *
* *
* In the balloon function, prompt for start and ending time. *
* altitute, velocity and time was calculated using equations provided.*
* Nested do loop was used to loop the program when the user entered *
* invalid input. Characters are not supported. Table was *
* printed using for loop. *
* *
* In compass function, it allow user to enter campass reading and *
* convert it to bearing reading. Characters is not supported. *
* Strcpy() function was used to assign string to a variable and later *
* to print desired string to screen using printf. *
* *
* In drag function, prompt area and drag coefficient. Drag force was *
* then calculated using equation given. Characters input is *
* unsupported. The program will loop if invalid input was given and if*
* user decide to continue using the function. *
************************************************************************/

//preprocessors directives
#include
#include
#include
#include
#include
//define constants
#define MAX_LINE_BUFFER 256
#define DENSITY 1.23

//functions declaration
void balloon();
void compass();
void drag();

float to_hour(float h,float m);
int round(float x);




void main()
{
//local variables
int choice,confirm;
char temp[MAX_LINE_BUFFER];
char str,test;

do{//post test loop to repeat program

system("cls");
printf("==================Main Menu==================\n");
printf("\t1\tWeather balloons\n");
printf("\t2\tCompass Heading\n");
printf("\t3\tDrag Force\n");
printf("\t4\tQuit\n");
printf("=============================================\n");
printf("Please insert your choice: ");
fgets (temp, MAX_LINE_BUFFER, stdin); //store user input to temp, max 256bytes
sscanf(temp,"%c",&test);//scan 1st char in user input

if(!isdigit(test))//if 1st char is not num, invalid input
choice=0;
else
sscanf(temp,"%d%c",&choice,&str);//scanf for first int and char from temp

if(!isspace(str))//if str is not whitespace, make choice invalid
choice=0;

flushall();//clear remaining input in stream i.e. char after str


switch(choice)
{
case 1 : balloon(); //call balloon function
break;
case 2 : compass(); //call compass function
break;
case 3 : drag(); //call drag function
break;
case 4 : printf("Ended!\n");
system("pause");
break;
default: system("cls");
printf("You have entered an invalid choice..!\n");
printf("Please enter you choice again.\n");
system("pause");
break;
}
}while(choice!=4);//stop looping when choice==4

}




//======================================== start of module 1 ==================================
void balloon()
{
int error=0,maxhour,hour,minute,maxminute,confirm;
float starthour,startminute,total_increment_hour,endhour,endminute,totalendhour,ihour,iminute;
float totalstarthour,height,velocity,maxheight=0,time,t;

do{
do{
do{
do{
system("cls");
//get start time
printf("Enter start time (less than 48hours)\n");
printf("Hour: ");//prompt for start hour
scanf("%f",&starthour);
flushall();
printf("Minute: ");//prompt for start minute
scanf("%f",&startminute);
flushall();
totalstarthour=to_hour(starthour,startminute); //call func to convert to hour only

if(starthour<0 || startminute<0 || totalstarthour>48)
{
printf("Invalid time input!\n");
printf("Please try again from the beginning.\n");
system("pause");
}
}while(totalstarthour>48 || starthour<0 || startminute<0);//start over if invalid input



//get end time
printf("\nEnter end time (less than 48 hours)\n");
printf("Hour: ");//prompt for end hour
scanf("%f",&endhour);
flushall();
printf("Minute: ");//prompt for end minute
scanf("%f",&endminute);
flushall();

totalendhour=to_hour(endhour,endminute); //call func to convert to hour only

if( endhour<0 || endminute<0 || totalendhour>48 || totalendhour<=totalstarthour)
{
printf("Invalid time input!\n");
printf("Please try again from the beginning.\n");
system("pause");
}

}while(totalendhour>48 || endhour<0 || endminute<0 || totalendhour<=totalstarthour);
//start over if invalid input


//get time increment
printf("\nEnter each line increment:\n");
printf("Hour: ");
scanf("%f",&ihour);
flushall();
printf("Minute: ");
scanf("%f",&iminute);
flushall();

total_increment_hour=to_hour(ihour,iminute);//convert to hour only

//check for increment value error
error=0;
if(ihour>(endhour-starthour) || ihour==(endhour-starthour) && iminute>(endminute-startminute))
{
printf("The increment must be less than the different of starting and ending time.\n");
error=1;
}
else if(total_increment_hour<0)
{
printf("Invalid value!\n");
error=1;
}
else if(total_increment_hour==0)
{
printf("The increment cannot be zero!\n");
error=1;
}
if(error==1)
{
printf("Please try again.\n");
system("pause");
}

}while(error==1);//start over if invalid input



//print table
printf("%c======================================================%c\n",213,184);
printf("| Time | Altitude | Velocity |\n");
printf("| | (meter) | (meter/hour) |\n");
printf("|-----------------+----------------+-------------------|\n");

do{
//convert hour to hour and minute
hour = floor(totalstarthour);
minute=round((totalstarthour-floor(totalstarthour))*60);
if(minute==60)
{
hour+=1;
minute=0;
}

t=totalstarthour;
height= (-.12*pow(t,4))+(12*pow(t,3))-(380*t*t)+(4100*t)+220;//calc altitute
velocity= (-0.48*pow(t,3))+(36*t*t)-(760*t)+4100;//calc velocity

//print content of table
printf("| %02d:%02d ", hour,minute);
printf("| %10.3f ", height);
printf("| %10.3f |\n",velocity);

if(height>maxheight)//to find greatest altitute
{
maxheight=height;
maxhour=hour;
maxminute=minute;
}

totalstarthour+=total_increment_hour;

}while(totalstarthour<=totalendhour);

printf("%c======================================================%c\n",212,190);

//printf peak altitute and coresponding time
printf("\nThe peak altitude is %.3fm, at %02d:%02d.\n\n",maxheight,maxhour,maxminute);
maxheight=0;//reset maxheight for user to continue in second loop
system("pause");

//prompt to continue?
do{
printf("Continue ???\n");
printf("1\tyes\n2\tno\n>");
scanf("%d",&confirm);
flushall();
printf("%d\n",confirm);
}while(confirm!=1 && confirm!=2);

}while(confirm!=2);//check if to continue
}


float to_hour(float h,float m)//convert hour and minute to hour in floating point
{
h=h+m/60;
return h;
}

int round(float x)//round up minute to fix trucation error of floating number
{
if((x-(int)x)>=0.5)
x=(int)x+1;
return x;
}



//==================================== start of module 2 ===========================
void compass()
{
//local variables
int confirm;
float bearing,dir;
char NorS[6],EorW[5];
do{
do{
bearing=-1;//initialize bearing to invalid value
system("cls");
printf("Bearing:\n ");//ask user to enter bearing
scanf("%f",&bearing);
flushall();
if(bearing<0 || bearing>360)
{
printf("Invalid bearing!\nPlease try again.\n");
system("pause");
}
}while(bearing<0 || bearing>360);//to start over if invalid input

//calc direction based on bearing quadrant
if(bearing>0&&bearing<90)
dir=bearing;
else if(bearing>=90&&bearing<=180)
dir=180-bearing;
else if(bearing>180&&bearing<270)
dir=bearing-180;
else if(bearing>=270&&bearing<=360)
dir=360-bearing;
else if(bearing==0)
dir=0;


//ternary conditional operators to decide which direction to print
(bearing<=90||bearing>=270? strcpy(NorS,"North"):strcpy(NorS,"South"));
(bearing<180? strcpy(EorW,"East"):strcpy(EorW,"West"));
printf("Compass Bearing: %s %.1f degree %s\n\n",NorS,dir,EorW);

//prompt to continue?
do{
printf("Continue ???\n");
printf("1\tyes\n2\tno\n>");
scanf("%d",&confirm);
flushall();
}while(confirm!=1 && confirm!=2);//prompt again if invalid input

}while(confirm!=2);//check if to continue
}



//============================================= start of module 3 =========================================
void drag()
{
float A,Cd,F;
int v,confirm,error=0;

do{
do{
do{
A=-1;//initialize area to invalid value
Cd=-1;//initialize coefficient to invalid value
system("cls");
printf("Please enter the value of projected area(in m%c)>>>\n",253);
scanf("%f",&A);//prompt for area
flushall();
if(A<0)//to check if area is valid
{
printf("Invalid area!\n");
printf("Please try again from the beginning.\n");
system("pause");
}
}while(A<0);//to start over if invalid input



printf("Please enter the value of drag coefficient>>>\n");
scanf("%f",&Cd);
flushall();
if (Cd<0)//to check if drag coefficient is valid
{
printf("Invalid coefficient!\n");
printf("Please try again from the beginning.\n");
system("pause");
}
}while(Cd<0);//to start over if invalid input


//print table
printf("%c=================================%c\n",213,184);
printf("| Velocity | Drag force |\n");
printf("| (m/s) | (N) |\n");
printf("|---------------+-----------------|\n");


for(v=0;v<=40;v++)//do for every v in between 0 and 40
{
F=(float)1/2*Cd*A*DENSITY*v*v;//calculate drag force
printf("| %02d ",v);
printf("| %10.3f |\n",F);
}

printf("%c=================================%c\n\n",212,190);

//prompt to continue?
do{
printf("Continue ???\n");
printf("1\tyes\n2\tno\n>");
scanf("%d",&confirm);
flushall();
}while(confirm!=1 && confirm!=2);//prompt again if invalid input

}while(confirm!=2);//check if to continue
}

No comments:

Post a Comment