Lab 10 sem ii_12_13

285 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
285
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lab 10 sem ii_12_13

  1. 1. UNIVERSITI TUN HUSSEIN ONN MALAYSIA FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING BTI 10202: COMPUTER PROGRAMMING LAB 10 : Functions (cont) NAME : _____________________________________ MATRICS NO.: _______________ DATE : ____________ MARKExercise 1: Type the coding below and answer the following questions: 1 #include<stdio.h> 2 #include<conio.h> 3 void swapByValue(int a, int b ); 4 void swapByAddress(int *a, int *b ); 5 6 void swapByValue(int a, int b ){ 7 int temp; 8 Temp = a; 9 a = b; 10 b = temp; 11 12 } 13 void swapByAddress(int *a, int *b ){ 14 int temp; 15 temp = *a; 16 *a = *b; 17 *b = temp; 18 19 } 20 int main(void){ 21 int a, b; 22 a = 8, b = 5; 23 24 printf("nIntially a=%3d and b=%3d ", a, b); 25 swapByValue(a, b); 26 printf("nAfter swap by value a=%3d and b=%3d", a, b); 27 swapByAddress(&a, &b); 28 printf("nAfter swap by address a=%3d and b=%3d", a, b); 29 getch(); 30 return 0; 31} i. What is the output of the coding? Output:
  2. 2. ii. Explain the difference between passing by value and address By value: ________________________________________________________ ________________________________________________________________ ________________________________________________________________ By Address: ______________________________________________________ ________________________________________________________________ ________________________________________________________________iii. Add printf("nnIn swap by value function a=%3d and b=%3d", a, b); at line 11 Add printf("nnIn swap by address a=%3d and b=%3d", *a, *b); at line 18 What is the output of the coding? Explain: By value: ________________________________________________________ ________________________________________________________________ ________________________________________________________________ By Address: ______________________________________________________ ________________________________________________________________ ________________________________________________________________
  3. 3. Exercise 2 : Type the coding below and answer the following questions: #include <stdio.h> #include <conio.h> main () { int n; long faktorial ( int ); printf("Enter n (1-15): "); scanf ("%d",&n); printf ("%d! = %dn",n, faktorial ( n ) ); getch(); } long faktorial ( int n ) { if ( n == 0 ) return 1; else return (n * faktorial ( n -1 )); } i. What is the output of the coding? Output: ii. Explain how the coding work (input: 3) : ________________________________________________________________ ________________________________________________________________ ________________________________________________________________ ________________________________________________________________ ________________________________________________________________ ________________________________________________________________ ________________________________________________________________ ________________________________________________________________ ________________________________________________________________ ________________________________________________________________

×