Dti2143 lab sheet 6
Upcoming SlideShare
Loading in...5
×
 

Dti2143 lab sheet 6

on

  • 784 views

 

Statistics

Views

Total Views
784
Views on SlideShare
784
Embed Views
0

Actions

Likes
0
Downloads
30
Comments
0

0 Embeds 0

No embeds

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

Dti2143 lab sheet 6 Dti2143 lab sheet 6 Document Transcript

  • left0UNIVERSITI TUN HUSSEIN ONN MALAYSIA<br />FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING <br />DAM31303/DTI 2143: COMPUTER PROGRAMMING<br />LAB EXERCISE 6: SELECTION & LOOP <br />Exercise 1:<br />
    • Type in the following program
    #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 />
    • 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: Write a C program to draw a symmetrical triangle by using for-loop. The program should be able to produce output as follows: <br />Exercise 4:<br />Write a C programme for Holiday Villa Subang Jaya Business Lounge internet charges that calculates the total charges for internet usage. Operation time: 08:00-22:00 and the charges are as follows:<br />HoursRateFirstRM 3.00Second to FifthRM 2.00Subsequence RM 1.00<br />Allow the user to repeat calculations as many times as needed and enter ‘n’ to stop. The output should similar as follow:<br />