Sunteți pe pagina 1din 10

RAGHU INSTITUTE OF TECHNOLOGY

RAGHU INSTITUTE OF TECHNOLOGY


DEPARTMENT OF CSE
College Code : 3J

R13 Syllabus

C PROGRAMMING LAB MANUAL


=========================================================================
Exercise 14
a) Write a C program consisting of Pointer based function to exchange value of two integers using pass
by address.
b) Write a C program to swap two numbers using pointers
========================================================================
SPECIFICATION:
(14)(a) C program consisting of pointer based function to exchange value of two integers using passing
by address.
DESCRIPTION:
When an array is passed to a function as argument, only the address of the first element of the
array is passed, not the actual values of the array elements. When we pass actual parameter addresses to a
functions, the formal parameters receiving the addresses should be of type pointer. The process of calling
a function using pointers to pass the address of variable is known as call by reference.
In C programs function arguments pass their data by value to local variables inside the called
function. This means that the function operates on a copy of the values, not on the original values is in
process in call by value. If you pass pointers to the original values as arguments to the function, then the
called function will operate on the original values.
ALGORITHM:
Step 1 : Start
Step 2 : Initialize two pointer variables *p1,*p2 and three integer variables a,b and temp.
Step 3 : Read two values a and b.
Step 4 : Assign p1 to address of a , p2 to address of b and temp to value of a.
Step 5 : Using sorting method swap the values.
5.1:temp *p1
Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

5.2:*p1*p2
5.3:*p2temp
Step 6 : Display the values of a and b after interchange.
Step 7 : Stop.
FLOWCHART:

START

Initialize Pointer variables *p1,*p2,&

Integer Variables a,b,temp

Read the variables a


and b

p1&a
p2&b

temp*p1
*p1*p2
*p2temp

Display a and b

STOP

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

PROGRAM
/* C Program To Interchange values of two integers using pointer based functions */
Program name:

// wk14a.c

/* Done By : C-Faculty

Dated: 15/10/2013*/

#include<stdio.h>

//stdio.h imports i/p and o/p functions

#include<conio.h>

// It imports i/p and o/p functions on console ( Output Screen )

void main()

// User defined program

// Main Program Begins


int a,b,*p1,*p2,temp; // Variables a,b,*p1,*p1,temp are declared for interchange the values
clrscr();

// Clears the output Screen

printf("Enter any two values:\n");

// Displays the Quoted statement

scanf("%d%d",&a,&b);

// Reads the values of a , b

p1=&a;

// Assign p1 to address of a

p2=&b;

// Assign p2 to address of b

temp=*p1;

// Assign temp to value of a

*p1=*p2;

// Assign a to value of b

*p2=temp;

// Assign b to value of temp

printf("After interchange the values:\n"); // Displays the Quoted statement


printf("%d\t%d",a,b);

// Displays the values of a and b

getch();

// Returns the output statement

// Ends Main Program

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 wk14a.c lcurses -lm
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 wk14a.c -curses o pointerbased

EXPECTED I/P AND O/P:


Output (1)
Enter any two values:

20

After interchange the values:30


Output (2)
Enter any two values:

30
20

286

594

After interchange the values: 594

286

ORIGINAL OUTPUT :
Output (1)
Enter any two values 100

200

After interchange the values 200


Output (2)
Enter any two values 102

100

212

After interchange the values 212

102

VIVA VOCE QUESTIONS:


1. What is a pointer?
Ans: A pointer is a variable that contains the address of a variable.
Example:
int *ptr,a;
ptr=&a;
Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

2. What is a parameter?
Ans:
Parameters are also known as arguments.They are used to communicate data between the
calling function and the called function.
The parameters are of two types:

Formal parameters(dummy parameters)


Actual parameters.

3. What is getch() function?


Ans:
It returns a character just entered from the standard input unit.The entered character is not
echoed on the scree.It reads a single character the moment it is typed without waiting for the Enter key
to be hit.
4. What is pass by reference in functions?
Ans:
In this method, the addresses of actual arguments in the calling function are copied
into formal arguments of the called function. This means that using these addresses, we would have an
access to the actual arguments and hence we would be able to manipulate them. C does not support
Call by reference. But it can be simulated using pointers.
--xXx--

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

SPECIFICATION:
(14)(b) Write a C program to swap two numbers using pointers.
DESCRIPTION:
Pointer is a user defined data type which creates special types of variables which can hold
the address of primitive data type like char, int, float, double or user defined data type like function,
pointer etc. or derived data type like array, structure, union, enum.
ALGORITHM:
Step 1 : Start
Step 2 : Initialize the variables x,y.
Step 3 : Display the x and y values before swapping
Step 4 : Call swap( )
Step 5: Display the x and y values after swapping
Step 6 : Stop
Algorithm for swap() function :
Step 1 : Begin
Step 2: Declare a temporary variable temp of type int.
Step 3 : Assign temp  *a
Step 4: Assign *a *b
Step 5 : Assign *b temp
Step 6 : End
FLOWCHART:

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

START

Function proto type


defined before main()

Initialize the variables x


and y

swap(&x,&y )

Read a third
variable temp

temp*a
*a*b
*btemp

Display the x and y


values before swapping
Return to main program
Call swap(&x,&y );

Display the x and y


values after swapping

STOP

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

PROGRAM
/* C Program To swap two numbers using pointers */
Program name:

// wk14b.c

/* C Program to calculate are of the triangle */


#include<stdio.h>

// stdio.h imports i/p and o/p functions

void swap(int *a,int *b);

// function declaration of swap ( )

void main()

//

User defined program

// Main Program Begins


int x=10,y=20;

// Initialize variables x=10 and y=20 in Integer Datatype

clrscr();

// Clears the output Screen

printf("Before swapping:x=%d y=%d\n\n",x,y);// Display the values of x & y before swapping


swap(&x,&y);

// Calling the function swap( )

printf("After swapping :x=%d y=%d\n\n",x,y); // Display the values of x & y after swapping
getch();

// It returns the ouput

// Main Program Ends

void swap(int *a,int *b)

// function definition of swap ( )

// Sub Program Begins


int temp;

// Initialize a variable temp

temp=*a;

// Assign temp to value of x

*a=*b;

// Assign x to value of y

*b=temp;
}

// Assign y to value of temp


// Main Program Ends

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 wk14b.c lcurses -lm
Step 3: Now go for running the program by using the command
Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

./a.out

Step 4: To create an executing file use the command


cc wk14b.c -curses o swap pointers

EXPECTED I/P AND O/P:


Output (1)
Before exchange:x=10 y=20
After exchange :x=20 y=10
Output (2)
Before exchange:x=100

y=200

After exchange :x=200

y=100

ORIGINAL OUTPUT :
Output (1)
Before exchange: x=10

y=20

After exchange: x=20

y=10

Output (2)
Before exchange: x=15

y=29

After exchange: x=29

y=15

VIVA VOCE QUESTIONS:


1. Define function.
Ans:
A function is a block of statements.Any c program should contain atleast one function,if a
program contains only one function then it is a main() function.A function is a module or block of
program code which deals with a particular task. Making functions is a way of isolating one block of
code from other independent blocks of code.A function can take a number of parameters, do required
processing and then return a value. There may be a function which does not return any value.
Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

2. Distinguish between actual and formal parameters?


Ans:
Actual parameters
1. The arguments
that are passed
in a function
call are called
actual
arguments.
2. These
arguments are
defined in the
calling
function.

Formal parameters
The formal arguments
are the
parameters/arguments
in a function
declaration.

Formal arguments
belong to the called
function.

3. What are the formal parameters?


Ans:
Formal parameters are the identifiers used in the declaration of sub-program or
function.Formal parameters are variables that are declared in the header of function definition.
4. What are the actual parameters?
Ans:
Actual parameters are the expressions in the calling statement. The arguments that are
passed in a function call are called actual arguments. These arguments are defined in the calling
function.
5. What is pass by value in functions?
Ans:
In this method, the value of each of the actual arguments in the calling function is copied
into corresponding formal arguments of the called function. In pass by value, the changes made to
formal arguments in the called function have no effect on the values of actual arguments in the calling
function.
--xXx--

Department of Computer Science & Engg

S-ar putea să vă placă și