Professional Documents
Culture Documents
C-PROGRAMMING
MATRIX MULTIPLICATION
#include<stdio.h> for (i=0;i<3; i++)
#include<math.h> {
main() for (j=0;j<3; j++)
{ c[i][j]=0;
int i,j,k; }
int a[3][3],b[3][3],c[3][3];
printf("Enter 1st matrix: \n");
for (i=0;i<3;i++)
for (i=0;i<3;i++) {
{ for (j=0;j<3;j++)
printf("\nEnter #%d row: ",(i+1)); {
for (j=0;j<3; j++) for (k=0;k<3;k++)
scanf("%d",&a[i][j]); c[i][j] = c[i][j] + (a[i][k])*(b[k][j]);
}
printf("\n\n");
}
}
printf("Enter 2nd matrix: \n"); printf("The resultant matrix is:\n\n");
for (i=0;i<3; i++) for (i=0;i<3;++i)
{ {
printf("\nEnter #%d row: ",(i+1)); for (j=0;j<3;++j)
for (j=0;j<3;j++) printf("%4d",c[i][j]);
scanf("%d",&b[i][j]);
printf("\n\n");
}
printf("\n\n"); }
}
OUTPUT
FUNCTIONS
Divide and conquer
Construct a program from smaller pieces or components
These smaller pieces are called modules
Each piece more manageable than the original program
FUNCTIONS
Functions
Modularize a program
All variables declared inside functions are local variables
Known only in function defined
Parameters
Communicate information between functions
Local variables
Benefits of functions
Divide and conquer
Manageable program development
Software reusability
Use existing functions as building blocks for new programs
Abstraction - hide internal details (library functions)
Returning control
If nothing returned
return;
If something returned
return expression;
1 /* Finding the maximum of three integers */
2
3 #include <stdio.h>
4
5 int maximum( int, int, int ); /* function prototype */
6
7 int main()
8 {
9 int a, b, c;
10
11 printf( "Enter three integers: " );
12 scanf( "%d%d%d", &a, &b, &c );
13 printf( "Maximum is: %d\n", maximum( a, b, c ) );
14
15 return 0;
16 }
17
18 /* Function maximum definition */
19 int maximum( int x, int y, int z )
20 {
21 int max = x;
22
23 if ( y > max )
24 max = y;
25
26 if ( z > max )
27 max = z;
28
29 return max;
30 }
Returns an int
Scaling
To get a random number between 1 and n
1 + ( rand() % n )
rand() % n returns a number between 0 and n - 1
1 + ( rand() % 6)
number between 1 and 6
RANDOM NUMBER GENERATION
srand function
<stdlib.h>
Takes an integer seed and jumps to that location in its
"random" sequence
srand( seed );
srand( time( NULL ) ); //load <time.h>
time( NULL )
Returns the time at which the program was compiled in seconds
global x is 1 on entering c
global x is 10 on exiting c
global x is 10 on entering c
global x is 100 on exiting c
local x in main is 5
RECURSION
Recursive functions
Functions that call themselves
The function written in terms of itself is a recursive case
The recursive case usually calls the function with a decreasing n
The function launches a new copy of itself (recursion step) to solve what it
cannot do
Eventually base case gets solved
Gets plugged in, works its way up and solves whole problem
EXAMPLE
/* Program to find factorial of a
number */
#include <stdio.h>
int factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
main()
{
printf(“%d”, factorial(5));
}
RECURSION
Example: factorials
5! = 5 * 4 * 3 * 2 * 1
Notice that
5! = 5 * 4!
4! = 4 * 3! ...
Compute 5!
L16 29
RECURSIVE ALGORITHMS COMPUTER
IMPLEMENTATION
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
f(5)=
5·f(4)
L16 30
RECURSIVE ALGORITHMS COMPUTER
IMPLEMENTATION
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
f(4)= f(5)=
4·f(3) 5·f(4)
L16 31
RECURSIVE ALGORITHMS COMPUTER
IMPLEMENTATION
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
L16 32
RECURSIVE ALGORITHMS COMPUTER
IMPLEMENTATION
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
L16 33
RECURSIVE ALGORITHMS COMPUTER
IMPLEMENTATION
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
L16 34
RECURSIVE ALGORITHMS COMPUTER
IMPLEMENTATION
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
L16 35
RECURSIVE ALGORITHMS COMPUTER
IMPLEMENTATION
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
L16 36
RECURSIVE ALGORITHMS COMPUTER
IMPLEMENTATION
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
L16 37
RECURSIVE ALGORITHMS COMPUTER
IMPLEMENTATION
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
L16 38
RECURSIVE ALGORITHMS COMPUTER
IMPLEMENTATION
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
4·6= f(5)=
24 5·f(4)
L16 39
RECURSIVE ALGORITHMS COMPUTER
IMPLEMENTATION
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
5·24=
120
L16 40
EXAMPLE USING RECURSION:
THE FIBONACCI SERIES
Fibonacci series: 0, 1, 1, 2, 3, 5, 8...
Each number is the sum of the previous two
Can be solved recursively:
fib( n ) = fib( n - 1 ) + fib( n – 2 )
Code for the fibaonacci function
long fibonacci( long n )
{
if (n == 0 || n == 1) // base case
return n;
else
return fibonacci( n - 1) +
fibonacci( n – 2 );
}
EXAMPLE USING RECURSION:
THE FIBONACCI SERIES
Set of recursive calls to function fibonacci
f( 3 )
return f( 2 ) + f( 1 )
return f( 1 ) + f( 0 ) return 1
return 1 return 0
1 /* Recursive fibonacci function */
2
3 #include <stdio.h>
4
5 long fibonacci( long );
6
7 int main()
8 {
9 long result, number;
10
11 printf( "Enter an integer: " );
12 scanf( "%ld", &number );
13 result = fibonacci( number );
14 printf( "Fibonacci( %ld ) = %ld\n", number, result );
15 return 0;
16 }
17
18 /* Recursive definition of function fibonacci */
19 long fibonacci( long n )
20 {
21 if ( n == 0 || n == 1 )
22 return n;
23 else
24 return fibonacci( n - 1 ) + fibonacci( n - 2 );
25 }
Enter an integer: 0 Program Output
Fibonacci(0) = 0
Enter an integer: 1
Fibonacci(1) = 1
Enter an integer: 2
Fibonacci(2) = 1
Enter an integer: 3
Fibonacci(3) = 2 Program Output
Enter an integer: 4
Fibonacci(4) = 3
Enter an integer: 5
Fibonacci(5) = 5
Enter an integer: 6
Fibonacci(6) = 8
Enter an integer: 10
Fibonacci(10) = 55
Enter an integer: 20
Fibonacci(20) = 6765
Enter an integer: 30
Fibonacci(30) = 832040
Enter an integer: 35
Fibonacci(35) = 9227465
RECURSION VS. ITERATION
Repetition
Iteration:
explicit loop
Recursion: repeated function calls
Termination
Iteration:
loop condition fails
Recursion: base case recognized