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

Dti2143 lab sheet 7

on

  • 609 views

 

Statistics

Views

Total Views
609
Views on SlideShare
591
Embed Views
18

Actions

Likes
0
Downloads
29
Comments
0

1 Embed 18

http://dti2143.blogspot.com 18

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 7 Dti2143 lab sheet 7 Document Transcript

  • 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 />
    • Write the program output (one input is sufficient)
    • Explain how the coding works
    • How many arguments passed from function to main program?
    Exercise 2.0:<br />Write a program to calculate student’s mark. Section the program with function. <br />
    • Examples:main(), getio(), calc(), disp()
    • the output should be similar as:
    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:<br />
    • Type in the following program
    #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 />