Dam31303 dti2143 lab sheet 7

610 views
562 views

Published on

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
610
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dam31303 dti2143 lab sheet 7

  1. 1. left0UNIVERSITI TUN HUSSEIN ONN MALAYSIA<br />FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING <br />DAM 31303/DTI 2143: COMPUTER PROGRAMMING<br />LAB EXERCISE 7 FUNCTION INTRO AND SWITCH CASE RECAPLAB EXERCISE 7 FUNCTION INTRO AND SWITCH CASE RECAP<br />Exercise 1.0: Type in the following program<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 />int fnAdd(int iNum1, int iNum2)<br />{<br /> int iSum;<br /> iSum = iNum1 + iNum2; <br /> return (iSum);}<br />1.1 Study program 3.0 carefully, then compile and execute it.<br /><ul><li>What does the program do?
  2. 2. Explain how the coding works.
  3. 3. How many arguments passed from function to main program?</li></ul>Exercise 2.0: Type in the following program<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 />2.1 Study program 4.0 carefully, then compile and execute it.<br /><ul><li>What does the program do?
  4. 4. Explain how the coding works.
  5. 5. How many arguments passed from function to main program?
  6. 6. What are the differences between coding in exe 1 and exe 2?</li></ul>Exercise 3.0:<br />#include<stdio.h><br />int maximum(int,int,int);<br />int main()<br />{<br />int a,b,c;<br />printf("Enter three(3) intergers:");<br />scanf("%d%d%d",&a,&b,&c);<br />printf("Maximum is %dn",maximum(a,b,c));<br />return 0;<br />}<br />int maximum(int x, int y, int z)<br />{<br />int max=x;<br />if (y>max)<br />max = y;<br />if (z>max)<br />max = z;<br />return max;<br />}<br />Study program 3.0 carefully, then compile and execute it.<br /><ul><li>Write the program output?
  7. 7. What does the program do?
  8. 8. Explain how the coding works.
  9. 9. How many arguments passed from function to main program?</li></ul>Exercise 4.0: Compile and run the program below<br />printf("Enter an integer to choose a color(red=1,green=2,blue=3):n");scanf("%d",&color);switch(color){case 1: printf("you chose red colorn"); break;case 2:printf("you chose green colorn"); break;case 3:printf("you chose blue colorn"); break;default:printf("you did not choose any colorn");}<br /><ul><li>Complete the coding
  10. 10. Modify the switch case to if else selection</li></ul>Exercise 5.0: Write a C program to compute bill payment of electrical consumption for different users. In order to promote saving, Tenaga National Berhad (TNB) charge a lower rate to users with lower consumption as shown in table below. Hint:Break down to basic: Input; Switch; If-Else; calculations; While or Do-while loop. The program calculation and output similar as follows:<br />User CodeUser typeFirst RateSecond rate1HouseholdRM0.10 for first 500 unitRM 0.25 for the following units2OfficeRM 0.25 for first 1000 unitRM0.50 for the following units3FactoryRM 0.50 for first 1500 unitRM 0.75 for the following units <br />

×