Module 3 Notes C Programming
Module 3 Notes C Programming
22POP13
MODULE-3
INTRODUCTION
Types of C Function
1. Library function
Library functions are the in-build function in C compiler.
For example:
main() //the execution of every C program starts from this main() function
printf() //printf() is used for displaying output in C
sqrt(x); // finds the square root of x
scanf() //scanf() is used for taking input in C
2. User defined function
C allows programmer to define their own function according to their
requirement. These types of functions are known as user-defined functions.
Advantages
• User defined functions helps to decompose the large program into small
segments which makes programmer easy to understand, maintain and debug.
• If repeated code occurs in a program. Function can be used to include those
code and execute when needed by calling that function.
• Programmer working on large project can divide the workload by making
different functions.
#include<stdio.h>
global declarations;
void function_name(parameter list); //function declaration or function prototype
void main()
{
local declarations;
Statements;
Function_name(parameter list); //function call
...........
..........
}
void function_name(parameter list) //function definition
{
local declarations;
Statements;
}
• Where:
– return_type is the data type that the function returns.
– Function_name refers to the name of the function.
– The formal parameters are a comma separated list of variables that
receive the values from the main program when a function is called.
– The return statement returns the results of the function.
Program to add two integers. Marks a function add integers and display sum
in main() function.
#include<stdio.h>
int add(int a,int b); // function prototype
void main()
{
int a, b, sum;
printf(“enter two numbers\n”);
scanf(“%d%d”,&a,&b);
sum=add(a,b); //actual arguments
printf(“\n sum=%d”,sum);
}
int add(int a,int b) //formal arguments
{
int sum; //local declaration
sum=a+b;
return sum;
}
2. Function with no arguments and with return value:
– In this , there is no data transfer from the calling function to the called
function. But there is a data transfer from called function to the calling
function.
– When the function definition returns a value the calling function
receives one value from the called function.
• The calling and called function need to communicate with each other to
exchange the data.
• The data flow between the calling and called functions can be divided into
– Call-by-value
– Call-by-reference
• Call by value:
– It is a one-way communication. The calling function can send data to
the called function, but the called function cannot send any data to the
calling function.
– The called function may change the values passed, but the original
values in the calling function remain unchanged.
• Call by reference:
– We may often come across situations where we need to modify the
values of actual parameter in the called function.
Scope of variable:
Variable have a defined scope. by scope we mean the accessibility and
visibility of the variables at different points in the program.
There are 4 types:
1. Block scope
2. Function scope
3. Program scope
4. File scope
1. Block scope
Statements block is a group of statements enclosed within
opening and closing curly brackets{}.
If a a variables is declared within a statement block then as soon
as the control exists that block, the variable will stop to exist.
Such a variable is also known as local variable is said to have a
block scope.
Blocks of statements may be placed one after the other in a
program, such blocks that are placed at the same level are
known as parallel blocks.
For example: a program may also contain a nested block, like a
while loop inside main(). If an integer x is declared and
initialized in main() and then it is re-declared and re-initialized
in a while loop, then the integer variable x will occupy different
memory slots and will be considered as different variables
#include<stdio.h>
Void main()
{
int x=10;
int i=0;
printf(“\n the value of x outside the while loop is %d”,x);
while(i<3)
{
int x=I;
printf(\n the value of x inside the while loop is
%d”,x);
i++;
}
printf(“\n the value of x outside the while loop is %d”,x);
}
2. Function scope
Function scope indicates that a variable is active and visible
from the beginning to the end of a function.
In C, only the goto label has function scope.( it is applicable to
goto statements)
This means that the programmer cannot have the same label
names inside a function.
For example: int main()
{
Loop: /*a goto label has function scope*/
.
.
.
.
Goto Loop;
}
3. Program scope
Local variable are automatically created when they are declared
in the function and are usable only within that function.
The local variables are unknown to other functions in the
programs
Such variable stops to exists after the function in which they are
declared is exited and have to be re-created each time the
function is called.
To avoid these, we can declare the variables outside any
function blocks called as global variable where are accessed
from any point in the program.
Global variable are created at the beginning of program
execution and remain in existence throughout period of
execution of the program.
If we have a variable declared in a function that has some name
as that of the global variable, then the function will use the local
variable declared within it and ignore the global variable
Example:
#include<stdio.h>
int x=10;
void print();
void main()
{
printf(“\n the value of x in the main()=%d”,x);
int x=2;
printf(“\n the value of local variable x in the
main()=%d”,x);
print();
}
void print()
{
printf(“the value of x in the print(0=%d”,x);
}
4. File scope
When a global variable is accessible until the end of the file, the
variable is said to have file scope.
To allow variable to have file scope, declare that variable with
the static keyword before specifying its data type
static int x=10;
A global static variable can be used anywhere from the file in
which it is declared but it is not accessible by any other file.
Storage class
It defines the scope (visibility) and lifetime of variable/functions declared in
a program. Storage class tells the information about variable/functions
The storage class of a function or a variable determines the part
of memory where storage space will be allocated for that
variable or function.
It specifies how long the storage allocation will continue to
exist.
It specifies the scope of the variable or function i.e which
variable name is visible or accessible.
It specifies whether the variable or function has internal,
external or no linkage.
It specifies whether the variable/ function will be automatically
initialized to zero or to any undetermined value.
There are 4 types
o Automatic
o External
o Register
o Static
#include<stdio.h>
extern int x;
void print()
{
printf(“\n x in file 2-=%d”,x);
}
main()
{
//statements
}
Recursion
• A function that calls itself is known as recursive function.
• The rule to be followed for recursive function is
– Establish boundary conditions that terminate the recursive calls
– Implement the recursive calls so that each call brings one steps closer
to the solution.
• Every recursive function must be provided with a way to end the recursion.
In this example when, n is equal to 0, there is no recursive call and recursion
ends.
Types of Recursion
1. Direct Recursion
A function is said to be directly recursive if it explicitly calls itself.
int func(int n)
{
if(n==0)
return 1;
else
return (func(n-1));
}
2. Indirect Recursion
It refers to a process where a function calls another function and that
function calls back the original function. The process of indirect
recursion takes place as illustrated below:
int func1(int n)
{
if(n==0)
return 1;
else
return (func2(n));
}
int func2(int x)
{
Return func1(x-1);
}
These two functions are indirectly recursive as they both call each
other.
3. Tail Recursion
A recursion function is said to be tail recursive if no operations
are pending to be performed when the recursive function
returns to its caller.
Tail recursive functions are highly desirable because they are
much more efficient to use as the amount of information that
has to be stored on the system stack is independent of the no.of
recursive calls.
Ex1: the factorial function that we have written is a non-tail
recursive function in the below code:
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}
1. Traversing an array
Traversing an array means accessing each and every element of
the array for a specific purpose.
If A is an array of homogeneous data elements, then traversing
the data elements can include printing every element, counting
the total no.of elements, or performing any process on these
elements.
Since array is linear data structure, traversing its element is very
simple and straightforward.
The algorithm for array traversal
Step1: [initialization] set i=lower_bound
Step2: repeat steps 3 to 4 while i<=upper_bound
Step3: apply process to a[i]
Step4: set i=i+1 [end of loop]
Step5: exit
Ex: write a program to print the position of the smallest of n
numbers using arrays.
Write a program to find the second largest number in an given
array.
Algorithm
Step1:start
Step2: [initialize] set pos=-1
Step3: [initialize] set i=1
Step4: repeat step4 while I<=N
Step5: if A[I]=val
Set pos=I
Print pos
Goto step 7
[end of loop]
Step6:if pos=-1
Print” value is not present in the array”
[end of loop]
Step7: exit
Write the program to implement linear search
2. Binary search
Algorithm
Step1: start
Step2: [initialize] set beg=lower_bound, end=upper_bound,pos=-1
Step3: repeat step4 and 5 while beg<=end
Step4: set mid=(beg+end)/2
Step5: if a[mid=val, then pos=mid
Print pos
Goto step7
Else if a[mid]>val, then set end= mid-1
Else
Set beg=mid+1
[end of if]
[end of loop]
Step6: if pos=-1,then
Print “ value is not present in the array”
[end of if]
Step7: exit
Bubble Sort
#include<stdio.h>
void main()
{
int a[20],n,.temp,i,j;
printf(“enter the number of elements\n”):
scanf(“%d”,&n);
printf(“enter the unsorted array elements\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
If( a[ J ] > a[ J+1 ] )
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf(“the sorted elements arer\n”);
for(i=0;i<n;i++)
printf(“ %d\t”, a[i]);
}
Selection Sort
Passing arrays to function
Two dimensional arrays
Write a c program to read & print 2d array as a Array.
#include<stdio.h>
void main()
{
int m,n,i,j,a[3][3];
printf(“enter number of rows and columns\n”);
scanf(“%d %d”,&m,&n);
printf(“eneter array elements\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“array elements are\n”); for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d”,a[i][j]);
}
printf(“\n”);
}
}
Write a program to find sum of each row and sum of each column
#include<stdio.h>
void main()
{
int m,n,i,j,rsum,csum,a[3][3];
printf(“enter number of rows and columns\n”);
scanf(“%d %d”,&m,&n);
printf(“enter array elements\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<m;i++)
{
rsum=0;
for(j=0;j<n;j++)
{
rsum=rsum+a[i][j];
}
printf(“sum is %d”,rsum);
}
for(i=0;i<m;i++)
{
csum=0;
for(j=0;j<n;j++)
{
csum=csum+a[i][j];
}
printf(“sum is %d”,csum);
}
}
Write a C program to implement Matrix Multiplication
#include<stdio.h>
void main()
{
int m,n,i,j,sum,p,q,k,a[3][3],b[3][3],c[3][3];
printf(“enter number of rows and columns of matrix a \n”);
scanf(“%d %d”,&m,&n);
printf(“enter number of rows and columns of matrix b \n”);
scanf(“%d %d”,&p,&q);
if(n!=p)
{
printf(“multiplication not possible\n”);
exit(0);
}
printf(“enter matrix a elements\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“enter array b elements\n”);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]= c[i][j]+a[i][k]*b[k][j];
}
}
}
printf(“resultant matrix A is \n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
printf(“resultant matrix B is \n”);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf(“%d\t”,b[i][j]);
}
printf(“\n”);
}
printf(“resultant matrix a is \n”);
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
}
Multidimensional array
• The multidimensional array can have three, four or more dimensions.
• The first dimension is called as plane, which consists of rows and
columns.
• The c language considers the three dimensional array i.e an array of
two-dimensional arrays
Declaration:
Syntax: type array_name[planes][rows][columns]
Ex: int table[3][2][3];
Initialization:
Ex: int table[3][2][3] ={
{ {0,1,2},{3,4,5}},
{{6,7,8},{9,10,11}},
{{12,13,14},{15,16,17}}
};
Write a program to read and display a 2*2*2 array
Applications of array
• Arrays are widely used to implement mathematical vectors, matrices,
and other kinds of rectangular tables.
• Many databases include one-dimensional array whose elements are
records.
• Arrays are also used to implement other data structures such as
strings, stacks, queues, heaps, and tables.
• Arrays can be used for sorting elements in ascending or descending
order.