RAGHU INSTITUTE OF TECHNOLOGY
RAGHU INSTITUTE OF TECHNOLOGY
DEPARTMENT OF CSE
College Code: 3J
R13 Syllabus
C PROGRAMMING LAB MANUAL
===================================================================
Exercise 11
a) Write a C functions to find both the largest and smallest number of an array of integers.
b) Write C programs illustrating call by value and call by reference cncepts.
====================================================================
SPECIFICATION:( 11)(a).Write a c function to find both the largest and smallest number of an array of integers.
ALGORITHM:Step 1:-Start
Step 2:- Declare globally largest( ),smallest( )and integer variables size,a[50]
Step 3:- Read size and a[i]
Step 4:-Call the function largest( )
4.1:Declare big,a[]
4.2:Initialize big
a[0]
4.3: Check the iteration loop where i is initialize as zero i<size
4.3.1: if it is true then it enters then it checks
big
a[i] and it runs the loop until the loop fails
4.3.2:if the loop fails then it comes out of the loop
4.5: Display the largest element.
4.6: End the largest function and return to the main function
Step 5:-Call the function smallest( )
5.1:Declare small,a[]
5.2:Initialize small
Department of Computer Science & Engg
a[0]
RAGHU INSTITUTE OF TECHNOLOGY
5.3:Check the iteration where i is initialized as 1 do where
i<size
5.3.1:if the condition is true then it enter into loop and small<a[i] is checked
5.3.2:if the condition is false then it comes out of loop
5.4:Display smallest element
5.5:End the smallest function and return to the main function
FLOW CHART:START
Globally declare the functions largest( ) and smallest( )
Declare the variables size,a[50]
Read size and a[i]
Call the function largest( )
Call the functions smallest( )
STOP
Department of Computer Science & Engg
RAGHU INSTITUTE OF TECHNOLOGY
Largest( ) begin
Declare big,a[ ]
FALSE
For i in steps of 1 do where
i<size
TRUE
FALSE
big < a[i]
big
a[i]
The largest element
Largest ( ) - end
Department of Computer Science & Engg
RAGHU INSTITUTE OF TECHNOLOGY
Smallest( ) begin
Declare small,a[ ]
Small
a[0]
FALSE
For i in steps of 1 do
where i<size
TRUE
FALSE
Small<a[i]
Small
a[i]
Smallest element
Smallest( )- end
Department of Computer Science & Engg
RAGHU INSTITUTE OF TECHNOLOGY
PROGRAM
/*C Program to find largest and smallest numbers of an array using pointers*/
Program name:
/* Done By : C-Faculty
#include<stdio.h>
#include<curses.h>
void largest( );
void smallest( );
int main( )
int size,a[50];
{
printf(Enter the size of an array);
scanf(%d,&size);
printf (Enter elements into array);
for(i=0;i<size;i++)
{
scanf(%d&a[i]);
}
largest( )
smallest( )
return(0);
}
void largest ( )
{
int big , a[i];
Department of Computer Science & Engg
// wk11a.c
Dated: 15/10/2013*/
RAGHU INSTITUTE OF TECHNOLOGY
big =a[0];
for(i=1;i < size ;i++)
{
if(big < a[i])
}
}
printf(the largest elements is %d;big);
}
void smallest( )
{
int small, a[i];
small=a[0];
for(i=1; i<size;i++)
{
if( small > a[i])
{
small=a[i];
}
}
printf(smallest elements is %d;small);
}
PROCEDURE FOR EXECUTING THE PROGRAM:
Step 1: After typing the program, press ESC button+shift+: and then type wq(to save the
program and quit)
Step 2: Now compile the program by using the following command
cc wk11a.c lcurses
Department of Computer Science & Engg
RAGHU INSTITUTE OF TECHNOLOGY
Step 3: Now go for running the program by using the command
./a.out
Step 4: To create an executing file use the command
cc wk11a.c -curses o swappointeers
EXPECTED I/P AND O/P
OUTPUT:
Enter the size of an array:3
Enter the elements into array:25
69 98
Largest element is 98
Smallest element is 25
ORIGINAL OUTPUT:
Enter the size of an array:4
Enter the elements into the array:38 44 21 48
Largest element is 48
Smallest element is 21
--xXx--
Department of Computer Science & Engg
RAGHU INSTITUTE OF TECHNOLOGY
SPECIFICATION:(11)(b) Write a c program to illustrate call by value.
ALGORITHM:Step 1:Start
Step 2:Declare integer variables x, y, avg,sum function
Step 3:- Function called
x
sum(a,b,c)
step 4:-Compute
avg
x/3
step 5:-Display avg
step 6:- Call the function
sum(p,q,r)
step 7: Declare integer variable d
step 8:-Read integer variables p,q,r
step 9:- Compute
d
p+q+r
step 10:-Return to the main function
step 11:-Stop
FLOWCHART:
Department of Computer Science & Engg
RAGHU INSTITUTE OF TECHNOLOGY
start
Declare x,y,avg
Call function X=sum(a,b,c)
avg=x/3
Display avg
stop
Stop
Sum-begin
Declare integer
variable d
Read p,q,r
d=p+q+r
Sum-end
Department of Computer Science & Engg
RAGHU INSTITUTE OF TECHNOLOGY
PROGRAM
/* C Program for implementing call by value */
Program name:
/* Done By : C-Faculty
#include<stdio.h>
#include<curses.h>
int sum(int ,int ,int)
int main( )
{
int x,y,avg;
clear( );
x=sum (a ,b ,c);
avg=x/3;
printf(%d,avg);
return(0);
}
int sum ( p,q,r )
{
int d;
printf(enter the values);
scanf(%d%d%d, &p,&q,&r);
d=p+q+r;
return(d);
}
Department of Computer Science & Engg
// wk11b.c
Dated: 15/10/2013*/
RAGHU INSTITUTE OF TECHNOLOGY
PROCEDURE FOR EXECUTING THE PROGRAM:
Step 1: After typing the program, press ESC button+shift+: and then type wq(to save the
program and quit)
Step 2: Now compile the program by using the following command
cc wk11b.c lcurses
Step 3: Now go for running the program by using the command
./a.out
Step 4: To create an executing file use the command
cc wk11b.c -curses o callbyvalue
EXPECTED I/P AND O/P:
1)Enter the values:1 2 3
avg=2
2)Enter the values:2 6 4
avg=4
ORIGINAL OUTPUT:
1)Enter the values:1 2 3
avg=2
2)Enter the values:2 6 4
avg=4
--xXx--
Department of Computer Science & Engg
RAGHU INSTITUTE OF TECHNOLOGY
SPECIFICATION:(11)(c) Write a C program illustrating call by reference
ALGORITHM:
step 1:- Start
step 2:- Declare two pointers *p and *q.
step 3:- Declare two integer variables a and b.
step 4:- Read integer variable a and b.
step 5:- Call the function add
add(&a,&b)
5.1: c
*p + *q
step 6:-Display elements
step 7:-Stop
FLOW CHART:-
Department of Computer Science & Engg
RAGHU INSTITUTE OF TECHNOLOGY
start
Globally declare add function
Declare integer variables a and b
Call the
functionAdd(&a,&b)
Display elements
Stop
Add function
Declare integer variable c
C= *p+*q
Add end
Department of Computer Science & Engg
RAGHU INSTITUTE OF TECHNOLOGY
PROGRAM
/* C Program to illustrate call by reference */
Program name:
/* Done By : C-Faculty
// wk11c.c
Dated: 15/10/2013*/
#include <stdio.h>
#include <curses.h>
void add (int *, int*);
int main ( )
{
int a, b;
clear ( );
printf (enter values);
scanf (%d%d, &a,&b);
add(&a,&b);
printf(Elements after adding are %d,c)
return(0);
}
void add( int *p, int *q )
{
int c;
c=*p+*q;
}
PROCEDURE FOR EXECUTING THE PROGRAM:
Step 1: After typing the program, press ESC button+shift+: and then type wq(to save the
program and quit)
Department of Computer Science & Engg
RAGHU INSTITUTE OF TECHNOLOGY
Step 2: Now compile the program by using the following command
cc wk11c.c lcurses
Step 3: Now go for running the program by using the command
./a.out
Step 4: To create an executing file use the command
cc wk11c.c -curses o callbyrefernce
EXPECTED I/P AND O/P:
1)Enter the values 4 5
C=9
2)Enter the values 1 2
C=3
ORIGINAL OUTPUT
1)Enter the values 4 5
C=9
2)Enter the values 1 2
C=3
VIVA VOCE QUESTIONS:
1. How call by reference is implemented?
Ans: Reference is implemented indirectly by passing the address of the variable.
--xXx--
Department of Computer Science & Engg