• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Bti1022 lab sheet 7
 

Bti1022 lab sheet 7

on

  • 762 views

 

Statistics

Views

Total Views
762
Views on SlideShare
740
Embed Views
22

Actions

Likes
0
Downloads
47
Comments
0

2 Embeds 22

http://bti1022examprep.blogspot.com 21
http://www.bti1022examprep.blogspot.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Bti1022 lab sheet 7 Bti1022 lab sheet 7 Document Transcript

    • left0UNIVERSITI TUN HUSSEIN ONN MALAYSIA<br />FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING <br />BTI 1022: COMPUTER PROGRAMMING<br />LAB EXERCISE 7 LOOP, GOTO and FUNCTION intro<br />Exercise 1:<br />
      • Type in the following program
      int main()<br />{<br /> double price1, price2 ,price3,price4, price5, price6, price7, totalprice=0;<br /> printf("Program to calculate the average and total price of the 7 itemsnn");<br /> printf("n Enter price of item: RM");<br /> scanf("%lf", &price1);<br /> totalprice +=price1;<br /> printf("n Enter price of item: RM");<br /> scanf("%lf", &price2);<br /> totalprice +=price2;<br /> printf("n Enter price of item: RM");<br /> scanf("%lf", &price3);<br /> totalprice +=price3;<br /> printf("n Enter price of item : RM");<br /> scanf("%lf", &price4);<br /> totalprice +=price4;<br /> printf("n Enter price of item : RM");<br /> scanf("%lf", &price5);<br /> totalprice +=price5;<br /> printf("n Enter price of item : RM");<br /> scanf("%lf", &price6);<br /> totalprice +=price6;<br /> printf("n Enter price of item : RM");<br /> scanf("%lf", &price7);<br /> totalprice +=price7;<br /> printf("n The total price of the item is: RM%0.2lf ", totalprice);<br /> printf("n The average price of the item is: RM%0.2lf ", totalprice/7);<br /> printf("nnEnd of program");<br /> getch();<br /> return 0;<br />}<br />
      • Study program 1.0 carefully, then compile and execute it.
      • What does the program do?
      • Why do you need 7 variables to hold the prices of the items?
      • Why is the variable totalprice initialized to 0?
      • Identify the set of statements that are repeated many times in the program
      • How many times were the set of statements repeated?
      Exercise 2:<br />int main()<br />{<br /> double price, totalprice=0;<br /> printf("Program to calculate the average and total price of itemsnn");<br /> for (int item=0;item<7;item++)<br /> {<br /> printf("n Enter price of item: RM");<br /> scanf("%lf", &price);<br /> totalprice +=price;<br />}<br /> printf("n The total price of the item is: RM%0.2lf ", totalprice);<br /> printf("n The average price of the item is: RM%0.2lf ", totalprice/7);<br /> printf("nnEnd of program");<br /> <br /> getch();<br /> return 0;<br />}<br />2.1 Study program 2.0 carefully, then compile and execute it.<br />a. What does the program do? In what way in this program different from program 1.0?<br />b. Identify the body of the loop, and state how many times it is repeated.<br />c. why do we now only need one variable to hold the prices of the item<br />d. Change the statement for (int item=0; item<7; item++) to the following; and state whether the output of the program remains the same, or if the output changes what are the changes<br />
      • for (int item=0;item<=7;item++)
      • for (int item=0;item<35;item+=5)
      • for (int item=10;item<17;item++)
      • for (int item=7;item>0;item--)
      e. Modify the program such that it will calculate and print the average and the total prices of 20 items.<br />Exercise 3:<br />3.0 Type in the following program<br />int main()<br />{<br /> <br /> double price, totalprice=0;char lagi;<br /> <br /> mula:<br /> printf("Program to calculate the average and total price of itemsnn");<br /> for (int item=0;item<7;item++)<br /> {<br /> printf("n Enter price of item: RM");<br /> scanf("%lf", &price);<br /> totalprice +=price;<br /> }<br /> <br /> printf("n The total price of the item is: RM%0.2lf ", totalprice);<br /> printf("n The average price of the item is: RM%0.2lf ", totalprice/7);<br /> printf("nnEnd of program");<br /> <br /> printf ("nnAgain? (y to continue):");<br /> scanf ("%s",&lagi);<br /> if(lagi=='y')<br /> goto mula;<br /> else<br /> printf("Thank you for using this system"); <br /> <br /> getch();<br /> return 0;<br />}<br />3.1 Study program 3.0 carefully, then compile and execute it.<br />
      • Identify goto
      • From the program above, explain how goto works
      • What type of loop that can be use to replace goto?
      Exercise 4:<br />
      • Type in the following program
      #include<stdio.h><br />#include<conio.h><br />int fnAdd(int iNum1, int iNum2) ;<br />int main(int argc, char **argv) {<br />int iResult,iValue1=8, iValue2=9;<br />iResult = fnAdd(iValue1, iValue2);<br /> printf("Sum of %d and %d is %dn",iValue1, iValue2, iResult);<br />getch();<br /> return 0;<br />}<br />int fnAdd(int iNum1, int iNum2)<br />{<br /> int iSum;<br /> iSum = iNum1 + iNum2; <br /> return (iSum); <br />}<br />4.1 Study program 3.0 carefully, then compile and execute it.<br />
      • What does the program do?
      • Explain how the coding works.
      Exercise 5:<br />
      • Type in the following program
      #include <stdio.h><br />#include <conio.h><br />void fnSum();<br />int main( int argc, char **argv ) {<br />fnSum();<br />getch();<br /> return 0;<br />}<br />void fnSum() {<br />int iNum1,iNum2,iSum;<br />printf("nEnter the two numbers:");<br />scanf("%d%d",&iNum1,&iNum2);<br />iSum = iNum1 + iNum2;<br />printf("nThe sum is %dn",iSum);<br />}<br />5.1 Study program 4.0 carefully, then compile and execute it.<br />
      • What does the program do?
      • Explain how the coding works.
      • What is the different between coding in 4.0 and 5.0?
      GOTO Extras:<br />
      • Type in the following program
      #include<stdio.h><br />#include<conio.h><br />int main()<br />{<br />char choco,macs,chmc,lagi;<br /> printf("tt Testing the goto functionnn");<br />mula: <br /> <br /> printf("n Do you like to eat chocolate(enter y or n): ");<br /> scanf("%s", &choco);<br /> <br /> if(choco=='y')<br /> goto chocs;<br /> else<br /> goto mcd; <br /> <br />chocs:<br /> printf("n Do you love chocolate more than McD? (enter y or n): ");<br /> scanf("%s", &chmc);<br /> if(chmc=='y')<br /> goto chocos;<br /> else<br /> goto mcd; <br /> <br />mcd:<br /> printf("n Do you really love McD? (enter y or n): ");<br /> scanf("%s", &macs);<br /> if(macs=='y')<br /> goto mcdees;<br /> else<br /> goto proud; <br />proud:<br /> printf("n You should be proud. Eat more heathy food");<br /> goto lagi; <br />chocos:<br /> printf("n You should be proud. you help your self by consuming anticancer, brain stimulator, cough preventor and antidiarrhoeal food ");<br /> goto lagi; <br />mcdees: <br /> printf("n You should try to control yourself before it is to late. n STOP eating fatty food"); <br />lagi: <br /> printf ("nn Again? (y to continue): ");<br /> scanf ("%s",&lagi);<br /> if(lagi=='y')<br /> goto mula;<br /> else<br /> printf(" Thank you for using this system"); <br /> <br /> getch();<br /> return 0;<br />}<br />