Module 3 Notes of Intruduction To C Programming
Module 3 Notes of Intruduction To C Programming
Array
Array is a collection of homogeneous (of same data type) elements.
data_type array_name[array_size];
int i, marks[10];
for (i=0;i<10;i++)
marks[i]=-1;
A[k]=Base_Address(A) + w*(k-lower_bound)
For example,
Given an array int marks[]={99, 67, 78, 56, 88, 90, 34, 85}. Calculate the address of marks[4]
if the base address = 1000.
=1000 + 8 = 1008
For example,
=4–0+1=5
Initializing arrays during declaration:
For example,
For example,
If the number of values provided is less than the number of elements in the array, the un-
assigned elements are filled with zeros.
For example,
int i, marks[10];
for(i=0;i<10;i++)
scanf(“%d”, &marks[i]);
marks[3]=100;
Int i, arr[10];
for (i=0; i<10; i++)
arr[i] = i*2
Operations on Array
1. Traversing an array
2. Inserting an element in an array
3. Deleting an element from an array
4. Merging two arrays
5. Searching an element in an array
6. Sorting an array in ascending or descending order
Traversing an array: Traversing an array means accessing each and every element of the array
for a specific purpose.
#include <stdio.h>
void main()
{
int a[100], i, n;
printf (“Enter the number of elements\n”);
scanf(“%d”, &n);
printf(“Enter the elements:\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Elements of the array are:\n”);
for (i=0;i<n;i++)
printf(“%d\n”,a[i]);
}
#include<stdio.h>
void main()
{
int a[100], i, n, smallest, pos;
printf(“Enter the number of elements\n”);
scanf(“%d”,&n);
printf(“Enter the elements:\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
smallest = a[0];
pos = 0;
for (i=1;i<n;i++)
{
if(a[i]<smallest)
{
smallest = a[i];
pos = i;
}
}
printf(“The position of the smallest element:%d”,pos);
}
Inserting an element in an array: Inserting an element in an array means adding a new data
element to an already existing array.
#include<stdio.h>
void main()
{
int a[100], pos, i,n;
printf(“Enter the number of elements\n”);
scanf(“%d”,&n);
printf(“Enter elements:\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter new element to be inserted\n”);
scanf(“%d”,&num);
Deleting an element from an array: Deleting an element from an array means removing a data
element from an already existing array.
#include<stdio.h>
void main()
{
int a[100], pos, i, n;
printf(“Enter the number of elements\n”);
scanf(“%d”,&n);
printf(“Enter the elements:\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter the position of element to be deleted\n”);
scanf(“%d”,&pos);
printf(“Deleted element:%d”,a[pos]);
for(i=pos;i<n-1;i++)
a[i]=a[i+1];
n--;
printf(“Elements of array after deletion of an element:\n”);
for(i=0;i<n;i++)
printf(“%d”,a[i]);
}
Merging two arrays: Copying two arrays into a new array is known as merging.
#include<stdio.h>
void main()
{
int a[100],b[100], c[100],n1,n2,n3,i,index;
printf(“Enter the number of elements of first array\n”);
scanf(“%d”,&n1);
printf(“Enter the first array elements\n”);
for(i=0;i<n1;i++)
scanf(“%d”,&a[i]);
printf(“Enter the number of elements of second array\n”);
scanf(“%d”,&n2);
printf(“Enter the second array elements\n”);
for(i=0;i<n2;i++)
scanf(“%d”,&b[i]);
index=0;
for(i=0;i<n1;i++)
{
c[index]=a[i];
index++;
}
for(i=0;i<n2;i++)
{
c[index]=b[i];
index++;
}
n=n1+n2;
printf(“The elements of merged array are:\n”);
for(i=0;i<n;i++)
printf(“%d”,c[i]);
}
Searching an element in an array: Find whether a particular value is present in the array or
not.
#include<stdio.h>
void main()
{
int a[100], n, i, found, key;
#include<stdio.h>
void main()
{
int a[100], n, i, found, key;
int low,high,mid
printf(“Enter size of the array\n”);
scanf(“%d”,&n);
printf(“Enter array elements:\n”);
scanf(“%d”,&a[i]);
low=0;
high=n-1;
printf(“Enter key element to be searched\n”);
scanf(“%d”,&key);
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
found=1;
pos=mid;
break;
}
else if(key<a[mid])
high=mid-1;
else
low=mid+1
if(found==1)
printf(“Element found at position %d”,pos);
else
printf(“Element not found\n”);
}
Sorting an array: Arranging elements in an ascending or descending order.
#include<stdio.h>
void main()
{
int a[100], n, i, j;
printf(“Enter the size of array\n”);
scanf(“%d”,&n);
printf(“Enter the array elements\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
Two dimensional array is specified using two subscripts. One subscript denotes row and the
other denotes column.
data_type array_name[row_size][column_size];
#include<stdio.h>
void main()
{
int a[2][2] = {12, 34, 56, 32}
int i, j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“%d”,a[i][j]);
}
printf(“\n”);
}
#include<stdio.h>
void main()
{
int mat[3][3], i, j;
printf(“Enter the elements of a matrix\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf(“%d”,&mat[i][j]);
}
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
#include<stdio.h>
void main()
{
int arr[7][7]={0};
int row=2, col, i, j;
arr[0][0] = arr[1][0] = arr[1][1] = 1;
while(row<7)
{
arr[row][0] = 1;
for(col=1; col<=row; col++)
arr[col][row] = arr[col-1][row-1] + arr[row-1][col];
row++;
}
for(i=0;i<7;i++)
{
for(j=0;j<=i;j++)
printf(“\t %d”, arr[i][j]);
}
}
1. Transpose of a matrix
2. Sum of matrices
3. Difference between matrices
4. Product of matrices
Transpose of matrix:
#include<stdio.h>
void main()
{
int mat[3][3], t_mat[3][3], i, j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
t_mat[j][i] = mat[i][j];
}
}
Sum of matrices:
Example: Write a program to add two matrices and display the result
#include<stdio.h>
void main()
{
int mat1[5][5], mat2[5][5], sum_mat[5][5];
int row1, row2, col1, col2;
for(i=0;i<rows_sum;i++)
{
for(j=0;j<cols_sum;j++)
{
sum_mat[i][j] = mat1[i][j] + mat2[i][j];
}
}
Multiplication of matrices:
#include<stdio.h>
#include<stdlib.h>
int main( )
{
int m,n,p,q,row,col,k,a[3][3],b[3][3],c[3][3];
printf("Enter the order of matrix A\n");
scanf("%d%d",&m,&n);
printf("enter order of matrix B\n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matrix Multiplication is not possible\n");
exit(0);
}
for(row=0;row<p;row++)
{
for(col=0;col<q ;col++)
{
scanf("%d",&b[row][col]);
}
}
for(row=0;row<m;row++)
{
for(col=0;col<q;col++)
{
c[row][col]=0;
for(k=0;k<n;k++)
{
c[row][col]= c[row][col]+a[row][k]*b[k][col];
}
}
}
for(row=0;row<m;row++)
{
for(col=0;col<n;col++)
{
printf("%d",a[row][col]);
}
printf("\n");
}
Multidimensional arrays
#include<stdio.h>
Void main()
{
int array[2][2][2], i, j, k;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
printf(“%d\t”, array[i][j][k]);
}
}
}
Functions
The following code shows that the main() function calls func1()
main() func1()
{ {
............; ..............;
............; }
func1();
............;
............;
}
iii. If all the statements written inside only main() function then it increases the
program size which makes difficulty in maintaining the program because of
limited memory space.
iv. All the libraries in C contain a set of functions which are pre-written and pre-
tested. Programmers can use these functions which speed up program
development.
vi. Like C libraries, programmers can also write their functions and use them in the
main program or any other programs.
Example,
or
The function declaration is global. Therefore, the declared function can be called from any
point in the program.
Example,
#include<stdio.h>
int sum(int a, int b);
int main()
{
int num1, num2, total=0;
Example,
#include<stdio.h>
int check_relation(int a, int b);
void main()
{
int a=3, b=5, res;
res = check_relation(a,b);
if(res = = 0)
printf(“Equal\n”);
if(res = = 1)
printf(“a is greater than b\n”);
if(res = = -1)
printf(“ a is less than b\n”);
When a function is called, the calling function may have to pass values to the called function.
There are two ways in which arguments or parameters can be passed to the called function.
They include,
i. Call by value: Variables are passed by calling function to the called function
ii. Call by reference: Address of variables are passed by the calling function to the
called function.
Call by value:
Example,
#include<stdio.h>
void add(int);
void main()
{
int num = 2;
printf(“The value of num before calling the function = %d\n”, num);
add(num);
void add(int n)
{
n = n+10;
printf(“The value of num in the called function = %d”, n);
}
Output:
Call by reference
#include<stdio.h>
void add(int *n);
void main()
{
Int num = 2;
void add(int *n )
{
*n = *n + 10;
printf(“The value of num in the called function = %d”, *n);
}
Output:
Example,
#include<stdio.h>
void swap_call_by_val(int, int);
void swap_call_by_ref(int *, int *);
void main()
{
int a=1, b=2, c=3, d=4;
printf(“In main function, a=%d and b=%d”, a, b);
swap_call_by_val(a, b);
printf(“In main function after call to swap by value function, a=%d and b=%d”, a, b);
printf(“In main function, c=%d and b=%d”, a, b);
swap_call_by_ref(&c, &d);
printf(“In main function after call to swap by value function, c=%d and d=%d”, c, d);
}
Program 2: Write a program to find the biggest of three integers using functions
#include<stdio.h>
int greater (int a, int b, int c);
void main()
{
int num1, num2, num3, large;
#include<stdio.h>
void swap(int, int);
void main()
{
int x, y;
printf(“Enter the value of x:”);
scanf(“%d”, &x);
printf(“Enter the value of y:”);
scanf(“%d”, &y);
Scope of variables
Scope mean the accessibility and visibility of the variables at different points in the program.
A variable has following four types of scope:
i. Block
ii. Function
iii. Program
iv. File
Block scope:
If a variable is declared within a block then as soon as the control exits that block, the
variable will cease to exist. Such a variable is known as local variable and which is in block
scope.
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.
Example,
return main()
{
.
.
In this example, the label is visible from the beginning to the end of the main() function.
Therefore, there should not be more than one label with the same name inside the main()
function.
Program scope:
Global variables are created at the beginning of program execution and remain existence
throughout the period of execution of the program.
These variables retain their value so that they can be used from every function in the
program.
If we declare local variable that has same as global variables then function use the local
variable and ignore the global.
Example,
#include<stdio.h>
int x=10;
void print();
void main()
{
printf(“The value of x in the main = %d”, x);
int x = 2;
printf(“The value of local variable x in the main = %d”, x);
print();
}
void print()
{
printf(“The value of x in the print = %d”, x );
}
Output:
Storage classes
Storage classes defines the scope (visibility) and lifetime of variables and functions declared
within a C program.
<storage_class_specifier><data_type><variable_name>
● This storage class is used to explicitly declare a variable with automatic storage.
void func1()
{
int a=10; //auto integer local to func1()
printf(“a=%d”, a);
}
void func2()
{
int a=20; //auto integer local to func2()
printf(“a=%d”, a);
}
void main()
{
int a=30; //auto integer local to main()
func1();
func2();
printf(“a=%d”, a);
}
Output:
a=10
b=20
c=30
void main()
{
int a=3, b=5, res;
res = exp(a,b);
printf(“Result = %d”, res);
}
int exp(int a, int b)
{
register int res=1; //register variable
int i;
for(i=1;i<=b;i++)
res=res*a;
return res;
}
● Example,
// FILE1.c
#include<stdio.h>
#include<FILE2.c>
int x;
void print(void);
void main()
{
x=10;
printf(“ x in FILE1 = %d”, x);
print();
}
//FILE2.c
#include<stdio.h>
extern int x;
void print()
{
printf(“x in FILE 2 = %d”, x);
}
main()
{
//statements
}
Output:
x in FILE1 = 10
x in FILE2 = 10
● Example,
void print()
void main()
{
printf(“First call of print());
print();
printf(“Second call of print());
print();
printf(“Third call of print());
print();
}
void print()
{
static int x;
int y = 0;
printf(“Static integer variable, x = %d”, x);
printf(“Integer variable, y=%d”, y);
x++;
y++;
}
Output:
Recursive function
Example programs,
2. Factorial of a number
#include<stdio.h>
int fact(int);
void main()
{
int num, factorial;
printf( “Enter the number: ”);
scanf(“%d”, &num);
factorial = fact(num);
printf(“Factorial of a number = %d”, factorial);
}
int fact(int n)
{
if(n==1)
return 1;
else
return (n*fact(n-1));
}
#include<stdio.h>
int fibonacci(int);
void main()
{
Int n, i=0, res;
printf(“Enter the number of terms\n”);
scanf(“%d”, &n);
printf(“Fibonacci series: \n”)
for(i=0; i<n; i++)
{
res = fibonacci(i);
printf(“%d\t”, res);
}
}
int fibonacci(int n)
{
if(n = =0)
return 0;
else if (n = = 1)
return 1;
else
return (fibonacci(n-1) + fibonacci(n-2));
}