Bti1022 lab sheet 7

6
UNIVERSITI TUN HUSSEIN ONN MALAYSIA FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING BTI 1022: COMPUTER PROGRAMMING Exercise 1: 1.0 Type in the following program int main() { double price1, price2 ,price3,price4, price5, price6, price7, totalprice=0; printf("Program to calculate the average and total price of the 7 items\n\n"); printf("\n Enter price of item: RM"); scanf("%lf", &price1); totalprice +=price1; printf("\n Enter price of item: RM"); scanf("%lf", &price2); totalprice +=price2; printf("\n Enter price of item: RM"); scanf("%lf", &price3); totalprice +=price3; printf("\n Enter price of item : RM"); scanf("%lf", &price4); totalprice +=price4; printf("\n Enter price of item : RM"); scanf("%lf", &price5); totalprice +=price5; printf("\n Enter price of item : RM"); scanf("%lf", &price6); totalprice +=price6; printf("\n Enter price of item : RM"); scanf("%lf", &price7); totalprice +=price7; printf("\n The total price of the item is: RM%0.2lf ", totalprice); printf("\n The average price of the item is: RM%0.2lf ", totalprice/7); printf("\n\nEnd of program"); getch(); return 0; } 1.1 Study program 1.0 carefully, then compile and execute it. a. What does the program do? b. Why do you need 7 variables to hold the prices of the items? LAB EXERCISE 7 LOOP, GOTO and FUNCTION intro

description

 

Transcript of Bti1022 lab sheet 7

Page 1: Bti1022 lab sheet 7

UNIVERSITI TUN HUSSEIN ONN MALAYSIAFACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING BTI 1022: COMPUTER PROGRAMMING

Exercise 1:

1.0 Type in the following program

int main(){ double price1, price2 ,price3,price4, price5, price6, price7, totalprice=0; printf("Program to calculate the average and total price of the 7 items\n\n"); printf("\n Enter price of item: RM"); scanf("%lf", &price1); totalprice +=price1; printf("\n Enter price of item: RM"); scanf("%lf", &price2); totalprice +=price2; printf("\n Enter price of item: RM"); scanf("%lf", &price3); totalprice +=price3; printf("\n Enter price of item : RM"); scanf("%lf", &price4); totalprice +=price4; printf("\n Enter price of item : RM"); scanf("%lf", &price5); totalprice +=price5; printf("\n Enter price of item : RM"); scanf("%lf", &price6); totalprice +=price6; printf("\n Enter price of item : RM"); scanf("%lf", &price7); totalprice +=price7; printf("\n The total price of the item is: RM%0.2lf ", totalprice); printf("\n The average price of the item is: RM%0.2lf ", totalprice/7); printf("\n\nEnd of program"); getch(); return 0;}

1.1 Study program 1.0 carefully, then compile and execute it.

a. What does the program do?b. Why do you need 7 variables to hold the prices of the items?c. Why is the variable totalprice initialized to 0?d. Identify the set of statements that are repeated many times in the programe. How many times were the set of statements repeated?

LAB EXERCISE 7 LOOP, GOTO and FUNCTION intro

Page 2: Bti1022 lab sheet 7

Exercise 2:

int main(){ double price, totalprice=0; printf("Program to calculate the average and total price of items\n\n"); for (int item=0;item<7;item++) { printf("\n Enter price of item: RM"); scanf("%lf", &price); totalprice +=price;} printf("\n The total price of the item is: RM%0.2lf ", totalprice); printf("\n The average price of the item is: RM%0.2lf ", totalprice/7); printf("\n\nEnd of program"); getch(); return 0;}

2.1 Study program 2.0 carefully, then compile and execute it.

a. What does the program do? In what way in this program different from program 1.0?b. Identify the body of the loop, and state how many times it is repeated.c. why do we now only need one variable to hold the prices of the itemd. 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

i. for (int item=0;item<=7;item++)ii. for (int item=0;item<35;item+=5)

iii. for (int item=10;item<17;item++)iv. 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.

Exercise 3:

3.0 Type in the following program

int main(){ double price, totalprice=0;char lagi; mula: printf("Program to calculate the average and total price of items\n\n"); for (int item=0;item<7;item++) { printf("\n Enter price of item: RM"); scanf("%lf", &price); totalprice +=price; } printf("\n The total price of the item is: RM%0.2lf ", totalprice);

Page 3: Bti1022 lab sheet 7

printf("\n The average price of the item is: RM%0.2lf ", totalprice/7); printf("\n\nEnd of program"); printf ("\n\nAgain? (y to continue):"); scanf ("%s",&lagi);

if(lagi=='y') goto mula; else printf("Thank you for using this system"); getch(); return 0;}

3.1 Study program 3.0 carefully, then compile and execute it.a. Identify gotob. From the program above, explain how goto worksc. What type of loop that can be use to replace goto?

Exercise 4:

4.0 Type in the following program

#include<stdio.h>#include<conio.h>

int fnAdd(int iNum1, int iNum2) ;int main(int argc, char **argv) {

int iResult,iValue1=8, iValue2=9;

iResult = fnAdd(iValue1, iValue2); printf("Sum of %d and %d is %d\n",iValue1, iValue2, iResult);

getch(); return 0;}

int fnAdd(int iNum1, int iNum2){

int iSum; iSum = iNum1 + iNum2; return (iSum);

}4.1 Study program 3.0 carefully, then compile and execute it.

a. What does the program do?b. Explain how the coding works.

Page 4: Bti1022 lab sheet 7

Exercise 5:

5.0 Type in the following program

#include <stdio.h>#include <conio.h>

void fnSum();int main( int argc, char **argv ) {

fnSum();getch();

return 0;}

void fnSum() {int iNum1,iNum2,iSum;printf("\nEnter the two numbers:");scanf("%d%d",&iNum1,&iNum2);iSum = iNum1 + iNum2;printf("\nThe sum is %d\n",iSum);

}

5.1 Study program 4.0 carefully, then compile and execute it.

a. What does the program do?b. Explain how the coding works.c. What is the different between coding in 4.0 and 5.0?

GOTO Extras:

6.0 Type in the following program

#include<stdio.h>#include<conio.h>int main(){

char choco,macs,chmc,lagi; printf("\t\t Testing the goto function\n\n");mula: printf("\n Do you like to eat chocolate(enter y or n): "); scanf("%s", &choco); if(choco=='y') goto chocs; else goto mcd; chocs: printf("\n Do you love chocolate more than McD? (enter y or n): ");

Page 5: Bti1022 lab sheet 7

scanf("%s", &chmc); if(chmc=='y') goto chocos; else goto mcd;

mcd: printf("\n Do you really love McD? (enter y or n): "); scanf("%s", &macs); if(macs=='y') goto mcdees; else goto proud; proud:

printf("\n You should be proud. Eat more heathy food"); goto lagi; chocos: printf("\n You should be proud. you help your self by consuming anticancer, brain stimulator, cough preventor and antidiarrhoeal food "); goto lagi; mcdees: printf("\n You should try to control yourself before it is to late. \n STOP eating fatty food");

lagi: printf ("\n\n Again? (y to continue): "); scanf ("%s",&lagi);

if(lagi=='y') goto mula; else printf(" Thank you for using this system"); getch(); return 0;}