Dam31303 dti2143 lab sheet 7
Upcoming SlideShare
Loading in...5
×
 

Dam31303 dti2143 lab sheet 7

on

  • 684 views

 

Statistics

Views

Total Views
684
Views on SlideShare
684
Embed Views
0

Actions

Likes
0
Downloads
27
Comments
0

0 Embeds 0

No embeds

Accessibility

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

Dam31303 dti2143 lab sheet 7 Dam31303 dti2143 lab sheet 7 Document Transcript

  • 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 />
    • What does the program do?
    • Explain how the coding works.
    • How many arguments passed from function to main program?
    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 />
    • What does the program do?
    • Explain how the coding works.
    • How many arguments passed from function to main program?
    • What are the differences between coding in exe 1 and exe 2?
    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 />
    • Write the program output?
    • What does the program do?
    • Explain how the coding works.
    • How many arguments passed from function to main program?
    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 />
    • Complete the coding
    • Modify the switch case to if else selection
    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 />