Upcoming SlideShare
×

# Dti2143 lab sheet 7

605 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
605
On SlideShare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
31
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Dti2143 lab sheet 7

1. 1. left0UNIVERSITI TUN HUSSEIN ONN MALAYSIA<br />FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING <br />DTI 2143: COMPUTER PROGRAMMING<br />LAB EXERCISE 7 FUNCTION AND GOTO<br />Exercise 1.0:<br />#include<stdio.h><br />long fibonacci(long);<br />int main()<br />{<br />int proceed=1;<br />long result, number;<br />while (proceed==1)<br />{<br />printf("nEnter an interger:");<br />scanf("%ld",&number);<br />result=fibonacci(number);<br />printf("Fibonacci(%ld)= %ldn",number,result);<br />printf("nAnymore item? Enter 1 for yes or else for no :");<br />scanf("%d",&proceed);<br />}<br />return 0;<br />}<br />long fibonacci(long n)<br />{<br />if (n==0||n==1)<br />return n;<br />else<br />return fibonacci(n-1)+fibonacci(n-2);<br />}<br />Study program 1.0 carefully, then compile and execute it.<br /><ul><li>Write the program output (one input is sufficient)
2. 2. Explain how the coding works
3. 3. How many arguments passed from function to main program?</li></ul>Exercise 2.0:<br />Write a program to calculate student’s mark. Section the program with function. <br /><ul><li>Examples:main(), getio(), calc(), disp()
4. 4. the output should be similar as:</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?
5. 5. What does the program do?
6. 6. Explain how the coding works.
7. 7. How many arguments passed from function to main program?</li></ul>Exercise 4.0:<br /><ul><li>Type in the following program</li></ul>#include<stdio.h><br />#include<conio.h><br />int main()<br />{<br />char choco,macs,chmc,lagi;<br /> printf("tt Testing the goto functionnn");<br />mula: <br /> <br /> printf("n Do you like to eat chocolate(enter y or n): ");<br /> scanf("%s", &choco);<br /> <br /> if(choco=='y')<br /> goto chocs;<br /> else<br /> goto mcd; <br /> <br />chocs:<br /> printf("n Do you love chocolate more than McD? (enter y or n): ");<br /> scanf("%s", &chmc);<br /> if(chmc=='y')<br /> goto chocos;<br /> else<br /> goto mcd; <br /> <br />mcd:<br /> printf("n Do you really love McD? (enter y or n): ");<br /> scanf("%s", &macs);<br /> if(macs=='y')<br /> goto mcdees;<br /> else<br /> goto proud; <br />proud:<br /> printf("n You should be proud. Eat more heathy food");<br /> goto lagi; <br />chocos:<br /> printf("n You should be proud. you help your self by consuming anticancer, brain stimulator, cough preventor and antidiarrhoeal food ");<br /> goto lagi; <br />mcdees: <br /> printf("n You should try to control yourself before it is to late. n STOP eating fatty food"); <br />lagi: <br /> printf ("nn Again? (y to continue): ");<br /> scanf ("%s",&lagi);<br /> if(lagi=='y')<br /> goto mula;<br /> else<br /> printf(" Thank you for using this system"); <br /> <br /> getch();<br /> return 0;<br />}<br />Study program 4.0 carefully, then compile and execute it.<br />4.1 Rewrite this program to example GOTO function based on your own understanding.<br />