2011 Name: MOHD FARID FAUZI
[PROGRAMMING FOR ENGINEERS] ENGINEERS] A program to h elp the user to determine the displacement and accele acceleration ration at certain time from a given data.
CONTENTS
1. OBJECTIVE
2
2. ALGORITHM ALGORITHM
2
3. FLOWCH FLOWCHA ART
3-5
4. PSEUDOCODE PSEUDOCODES S
6
5. SOURCE CODE
7-9
6. CONCL CONCLUTION
10
7. REF REFERENCE
10
1 e g a
P
OBJECTIVE The objective objective of this of this r epor epor t is to pro provide vide a program program C to help the user ser dete determ rmine ine the displa displacem cement and accelera ccelerati tio on at cer cer t ain tim ti me per per iod based based from a given given data of ti of tim me and velo velocity of an an object object and also lso inclu includes the f low char char t of the of the p program. rogram.
ALGORITHM
1. The displa displacem cement of the of the object object at sta stated/cer ted/cer tain tim ti me per per iod. v = u + at 2 s = ut + ½ at at
s1-s0 s1
= = = =
(v1 -v0) 2 t t v0t + ½ (v1 -v0) t v1t vt v0t + - 0 2 2 v0t vt + 1 2 2 ½ (v (v0t + v1t) ½ (v (v0 + v1 ) t ½ (v (v0 + v1 ) (t (t1-t0) ½ (v (v0 + v1 ) (t (t1-t0) + s0
Sf
=
0.5*(Tf-Ti)*(Vi+Vf)+Si
s = v0t + ½ = = =
Com pu puter ter form
2. The averag veragee accelera ccelerati tio on at tha that tim time v = u + at at = v - u at = v1 ± v ± v0 (v1 -v0) a = t (v1 -v0) a = (t1-t0) Ac=(Vf-Vi)/(Tf-Ti)
puter ter Com pu
form
2 e g a
P
FLOWCHARTS START
DISPLAY MAIN MENU
USER INPUT: MENU_SELECT 1,2,3,4,5
o ptio ption1 DISPL AY GIVEN DAT A FILE
o ptio ption2
o ptio ption3
FIND THE DISPLACEMENT ACCELER ATION
SAVED DATA TO
o ptio ption4 ABOUT
ME
o ptio ption5 EXIT
FILE
NO USER_INPUT
== '5'
YES
END
3 e g a
P
OPTION 1 OPTION1
READ DATA FR M FILE
DISPLAY READ DATA FROM FILE
RETURN
OPTION 2 OPTION2
ENTER FILENAME
READ DATA FROM FILE
Sf=0.5*(Tf-Ti)*(Vi+Vf)+Si; Ac=(Vf-Vi)/(Tf-Ti);
DISPLAY DISPLACEMENT
AND
ACCELER ATION
RETURN
4 e g a
P
OPTION 3 OPTION3
ENTER FILENAME
READ DATA FROM FILE
Sf=0.5*(Tf-Ti)*(Vi+Vf)+Si; Ac=(Vf-Vi)/(Tf-Ti);
SAVED DISPLACEMENT
AND
ACCELER ATION
RETURN
OPTION 4
OPTION4
DISPLAY ABOUT ME
RETURN
5 e g a
P
PSEUDOCODES 1. Star t main main Star t loo ping ping {Displa Display menu enu Select o ptio ption o ptio ption1,o ptio ption2,o pti 2,o ptio on3,o pti 3,o ptio on4,o ptio ption5 }end loo ping ping y
2. if o pti o ptio on1 o pen data f ile ile ³moti ³motio on.dat´ r ead data from f ile ile pr pr int int it to command ommand p prom rom pt pt((cmd) clear clear sc scr r een een clo close data f ile ile r etur etur n to main main 3. if o pti o ptio on2 o pen data f ile ile ³moti ³motio on.dat´ r ead data from f ile ile pro process cess and formul formu la pr pr int int it to command ommand p prom rom pt pt((cmd) clear clear sc scr r een een clo close data f ile ile ³moti ³motio on.dat´ r etur etur n to main main 4. if o pti o ptio on3 o pen data f ile ile ³moti ³motio on.dat´ ask t sk to enter enter f ilenam ilenamee o pen data to wr ite/sa ite/save r ead data from f ile ile pro process cess and formul formu la pr pr int int it to f ile( ile(to save) ve) clear clear sc scr r een een clo close data f ile ile ³moti ³motio on.dat´ clo close data f ile ile ³f ilenam ilename´ e´ r etur etur n to main main 5. if o pti o ptio on4 r prof rof ile displa display programm programmeer p ile r etur etur n to main main 6. if o pti o ptio on5 exit
6 e g a
P
SOUR CE CODE #include//librar #include//library y call #include// #include// for atof and system used void option1()//display function { int i; char kod[4],kod3[4],kod2[4]; kod[4],kod3[4],kod2[4]; FILE *mama; mama=fopen("motion.dat"," mama=fopen("motion.dat","r");//open r");//open file motion.dat for(i=0;i<=1;i++) { printf("\n"); fscanf(mama,"%s %s\n",kod,kod2);//read from file printf("%s\t%s",kod,kod2); }printf("\r");system("PAUSE"); system("CLS");//clear system("CLS");//clear sreen fclose(mama); } void option2()//displacem option2()//displacement ent n acceleration function { int i; char kod[4],kod3[4],kod2[4]; kod[4],kod3[4],kod2[4]; double Si,Sf,Vi,Vf,Ac,Tf,Ti; Si,Sf,Vi,Vf,Ac,Tf,Ti; Si=Sf=Vi=Vf=Ac=Tf=Ti=0; FILE *mama; mama=fopen("motion.dat"," mama=fopen("motion.dat","r");//open r");//open file motion.dat fscanf(mama,"%s %s\n",kod,kod2);//read 1st line... fscanf(mama,"%s fscanf(mama,"%s %s\n",kod,kod2);//read %s\n",kod,kod2);//read second line printf(" t\t s\t a\n"); Vi=atof(kod2);//changing Vi=atof(kod2);//changing from string to double Ti=atof(kod); printf("%.2f\t%.2f\t%5 printf("%.2f\t%.2f\t%5.2f",Ti,0,0); .2f",Ti,0,0); for(i=0;i<=1;i++) { printf("\n"); fscanf(mama,"%s %s\n",kod,kod2);//read data from file Vf=atof(kod2); Tf=atof(kod); Sf=0.5*(Tf-Ti)*(Vi+Vf)+S Sf=0.5*(Tf-Ti)*(Vi+Vf)+Si;//displace i;//displacement ment formula Ac=(Vf-Vi)/(Tf-Ti);//Acc Ac=(Vf-Vi)/(Tf-Ti);//Acceleration eleration formula printf("%.2f\t%.2f\t%5.2f" printf("%.2f\t%.2f\t%5.2f",Tf,Sf,Ac);//p ,Tf,Sf,Ac);//print rint data to cmd Vi=atof(kod2); Ti=atof(kod); }printf("\r");system("PAU }printf("\r");system("PAUSE");system("C SE");system("CLS"); LS"); fclose(mama);//close fclose(mama);//close file motion.dat }
7 e g a
P
void option3()//saved to file function { int i; char kod[4],kod3[4],kod2[4],filen kod[4],kod3[4],kod2[4],filename[20]; ame[20]; double Si,Sf,Vi,Vf,Ac,Tf,Ti; Si,Sf,Vi,Vf,Ac,Tf,Ti; Si=Sf=Vi=Vf=Ac=Tf=Ti=0; FILE *mama; mama=fopen("motion.dat"," mama=fopen("motion.dat","r");//open r");//open file to read printf("Please printf("Please insert filename to save. Including extension.(example: extension.(example: ANSWER.txt)\n"); scanf("%s",&filename);//N scanf("%s",&filename);//Name ame of the file to be saved FILE *papa; papa=fopen(filename,"w"); papa=fopen(filename,"w");//open //open file to write on(to save) fscanf(mama,"%s %s\n",kod,kod2); fscanf(mama,"%s %s\n",kod,kod2);//scan dta from file(2nd line) fprintf(papa," fprintf(papa," t\t s\t a\n");//print data to selected file Vi=atof(kod2); Ti=atof(kod); fprintf(papa,"%.2f\t%. fprintf(papa,"%.2f\t%.2f\t%5.2f",Ti, 2f\t%5.2f",Ti,0,0); 0,0); for(i=0;i<=1;i++) { fprintf(papa,"\n"); fscanf(mama,"%s %s\n",kod,kod2); Vf=atof(kod2); Tf=atof(kod); Sf=0.5*(Tf-Ti)*(Vi+Vf)+Si; Ac=(Vf-Vi)/(Tf-Ti); fprintf(papa,"%.2f\t%.2f\t fprintf(papa,"%.2f\t%.2f\t%5.2f",Tf,Sf,A %5.2f",Tf,Sf,Ac); c); Vi=atof(kod2); Ti=atof(kod); } fclose(mama);//close fclose(mama);//close reading file fclose(papa);//close fclose(papa);//close writing file(saved data) printf("File printf("File being saved...\n\nOpen saved...\n\nOpen cpp directory to read it\n"); printf("File printf("File can be opened u sing notepad..\n\n"); notepad..\n\n"); system("PAUSE");system("CLS"); } void option4() { //personal info printf(">>>>>>>>>>>>>>>>> printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>><<<<< >>>>>>>>><<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<< << <<<<<<<<<<<<<<<<<<<"); printf(">>>>>>>>>>>>>>>>> printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>> > about me <<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); <<<<<<<<<"); printf(">>>>>>>>>>>>>>>>> printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>><<<<< >>>>>>>>><<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<< << <<<<<<<<<<<<<<<<<<<"); printf(">>> FACULTY OF MECHANICAL MECHANICAL ENGINEERING,UNIVER ENGINEERING,UNIVERSITI SITI TEKNOLOGI TEKNOLOGI MALAYSIA SKUDAI <<<"); printf(">>> RAZMAN MURAD <<<");//name
8 e g a
P
printf(">>>
SX101183 MMJ03
<<<"); printf(">>>>>>>>>>>>>>>>> printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>><<<<< >>>>>>>>><<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<< << <<<<<<<<<<<<<<<<<<<"); system("PAUSE");system("CLS"); } void main() { for(;;)//looping for(;;)//looping it till you want to end it { char select; // User Interface 4 this program printf("::::::::::::::::: printf("::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::: :::::::::::::::::::::::::::: :::::::::::::::\ :\ n"); printf("::::^^^^^^^^^^^^^ printf("::::^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^::: ^^^^^^^^^^^::::\ :\ n"); printf(":::: printf(":::: PROGRAM TO CALCULATE CALCULATE DISPLACEMENT DISPLACEMENT & ACCELERATION ::::\n"); printf("::::>>>>>>>>>>>>> printf("::::>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>><<<<<<<<<<<<< ><<<<<<<<<<<<<<<<<<<<<<<<::: <<<<<<<<<<<::::\ :\ n"); printf(":::: printf(":::: 1. Display data on file. ::::\n"); printf(":::: printf(":::: 2. Calculate displacement and acceleration. acceleration. ::::\n"); printf(":::: printf(":::: 3. Save data to file. ::::\n"); printf(":::: printf(":::: 4. About me. ::::\n"); printf(":::: printf(":::: 5. Exit. ::::\n"); printf(":::: ::::\n"); printf(":::::: printf(":::::: PLEASE ENTER A CHOICE TO PROCEED ::::::___:::::::::::"); printf("\b\b\b\b\b\b\b\b\ printf("\b\b\b\b\b\b\b\b\b\b\b\b\b") b\b\b\b\b");//backspace ;//backspace scanf("%s",&select); printf("\n\n\n"); switch(select) { case '1' : option1();//function option1();//function call break; case '2' : option2();//function option2();//function call break; case '3' : option3();//function option3();//function call break; case '4' : option4();//function option4();//function call break; case '5' : printf("Program printf("Program is exiting....\n\n"); exiting....\n\n"); break; default : printf("Access printf("Access denied..\nSorry denied..\nSorry you enter the wrong input..\n"); system("PAUSE");system("CLS"); break; }if (select=='5') break; } }
9 e g a
P
CONCLUSION
Af ter ter f inished inished this r epor epor t, I
lear le ar n how to cr eate and unde nder r sta stand the p programm rogrammin ing g
fund fu ndam ament entaals such as by cr eatin ting g algor ithm, ithm, f low owch char ar t and then the p program rogram itsel itselff usin sing g var iou ouss type of of v var iab ables les,, Ope Opera rattor s, st staate tem ments ents,, and a nd exp expr r essio essions ns..
REFERENCES 1.
www
.fkm. com/mohamed/
2.
BYRON
S. GOTTFRIED, PH.D. SCH AUM¶S OUTLINES OF PROGR AMMING
WITH C SECOND EDITION M cGR AW-HILL 1996
0 1 11
e g a
P