Upcoming SlideShare
×

Dti2143 lab sheet 6

667 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
667
On SlideShare
0
From Embeds
0
Number of Embeds
25
Actions
Shares
0
43
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dti2143 lab sheet 6

1. 1. left0UNIVERSITI TUN HUSSEIN ONN MALAYSIA<br />FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING <br />DTI 2143: COMPUTER PROGRAMMING<br />LAB EXERCISE 6 LOOP AND FUNCTION (INTRO)<br />Exercise 1:<br /><ul><li>Type in the following program</li></ul>#include<stdio.h><br />#include<conio.h><br />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 /> <br /> getch();<br /> return 0;<br />}<br />1.1 Study program 1.0 carefully, then compile and execute it.<br /><ul><li>What does the program do?
2. 2. Why do you need 7 variables to hold the prices of the items?
3. 3. Why is the variable totalprice initialized to 0?
4. 4. Identify the set of statements that are repeated many times in the program
5. 5. How many times were the set of statements repeated?</li></ul>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 /><ul><li>for (int item=0;item<=7;item++)
6. 6. for (int item=0;item<35;item+=5)
7. 7. for (int item=10;item<17;item++)
8. 8. for (int item=7;item>0;item--)</li></ul>e. Modify the program such that it will calculate and print the average and the total prices of 20 items.<br />Exercise 3:<br />#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 />3.1 Study program 3.0 carefully, then compile and execute it.<br /><ul><li>What does the program do?
9. 9. Explain how the coding work.</li></ul>Exercise 4:<br />#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 />4.1 Study program 4.0 carefully, then compile and execute it.<br /><ul><li>What does the program do?
10. 10. Explain how the coding works.
11. 11. What is the different between coding in 3.0 and 4.0.</li></ul>Exercise 5:<br />#include<stdio.h><br />#include<conio.h><br />void sum(int a, int b, int *cp);<br />int main(void)<br />{<br /> <br />int x,y,z;<br />x=7;y=2;<br />printf(" X Y Zn");<br />printf("------------n");<br />sum(x,y,&z);<br />printf("%4d%4d%4dn",x,y,z);<br />sum(y,x,&z);<br />printf("%4d%4d%4dn",x,y,z);<br />sum(z,y,&x);<br />printf("%4d%4d%4dn",x,y,z);<br />sum(z,z,&x);<br />printf("%4d%4d%4dn",x,y,z);<br />getch();<br />return(0);<br />}<br />void sum(int a,int b, int *cp)<br />{<br /> *cp=a+b;<br />}<br />5.1 Study program 5.0 carefully, then compile and execute it.<br /><ul><li>What does the program do?
12. 12. Explain the calculation.