You are on page 1of 71

CLO 2:To use appropriate data types in simple data

processing applications. To create programs using the


concept of arrays.
CLO 3 : To create string processing applications with single
and multi-dimensional arrays.
Decision Making / Conditional
Branching / Selection Statement
 Decision making statements are used to skip or to execute
a group of statements based on the result of some
condition.
 The decision making statements are,
 simple if statement
 if…else statement
 else … if ladder
 nested if
 switch statement
 These statements are also called Conditional Branching or
Selection Statement statements
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 2
Simple if statement
Syntax:
if(condition)
{
Statements; False
if(condition)
}
True (Bypass)

Statements;

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 3


Simple if - Example
//Convert the negative number to
positive number Output:
# include <stdio.h>
void main () Enter a negative number: -50
{ The absolute value is 50
int number;

printf(“Enter a negative number:");


scanf("%d",&number);
if (number<0)
number= -number;
printf ("The absolute value is %d", number);
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 4
if - else statement
Syntax:

if(condi False
if(condition) tion)
{
True
True block statements;
} True Block False Block
Statement Statements
else
{
False block statements;
}

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 5


if – else Example
//Check whether the given number is
positive negative Output:
# include<stdio.h> Enter a non-zero number 50
void main ()
The number is positive
{
int number;
printf (“Enter a non-zero number:");
scanf ("%d", &number);
if (number < 0)
printf(“The number is negative”);
else
printf(“The number is positive”);
}

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 6


if – else Example
//Check whether the given number
is odd or even
#include<stdio.h> Output:
void main() Enter a number : 125
{ The number is ODD
int number;
printf ("Enter a number:");
scanf ("%d",&number);
if ((number%2)==0)
printf ("The number is EVEN.\n");
else
printf ("The number is ODD.\n");
}

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 7


Else - if Ladder Statement
Syntax:
if (condition1)
statement block 1;
else if (condition2)
statement block 2;
else if (condition3)
statement block 3;
:
:
else if (condition)
statement block n;
else
default statement;
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 8
Else - if Ladder Statement
True
If(condition1)
False
True
Else if(condition2)
False Statements1;
True
Else if(condition3)

False Statements2;

Default Statements;
Statements3;

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 9


Else - if Ladder Example
//Find the given number is positive /
negative / equal to zero Output:
#include <stdio.h> Enter a number: 75
void main () Positive Number
{
int number; Output:
printf ("Enter a number:"); Enter a number: -75
scanf ("%d", &number); Negative Number
if (number>0)
printf ("\n Positive Number"); Output:
else if (number<0) Enter a number: 0
printf("\n Negative Number"); Number is equal to zero
else
printf ("\n Number is equal to zero");
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 10
Else - if Ladder Example
//Find the grade of a student
#include <stdio.h>
void main () Output:
{ Enter mark: 75
int mark; Distinction
printf ("Enter mark:");
scanf ("%d", &mark);
if (mark <= 100 && mark >= 70)
printf ("\n Distinction");
else if (mark >= 60)
printf("\n First class");
else if (mark >= 50)
printf ("\n Second class");
else
printf ("Fail");
}

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 11


Nested if Statement
 if
statement may itself can contain another if statement is
known as nested if statement.
Syntax:
if(condition1)
{
if(condition2)
{
True block statement of condition1 & 2;
}
else
{
False block statement of condition2;
}
}
else
{
False block statements of condition1;
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 12
Nested if Statement
False
condition1
True
False
if(condition2) False Block Statements
True of condition 1;
True Block Statements
of condition 1 & 2; False Block Statements
of condition 2;

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 13


Nested if Example
//Check whether the given number is positive (or) negative
(or) equal to zero
# include <stdio.h>
void main()
{ Output:
int number;
printf (“Enter a number:"); Enter a number: 25
scanf ("%d", &number); 25 is positive
if (number!=0)
{ number
if(number>0)
printf(“%d is positive number”, number”);
else
printf(“%d is negative number”, number”);
}
else
{
printf(“\nThe given number is equal to zero”);;
}
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 14
Nested if Example
//Print the greatest of 3 numbers
# include <stdio.h>
void main()
{
int n1,n2,n3,max; Output:
printf (“Enter 3 numbers:");
scanf ("%d %d %d", &n1,&n2,&n3); Enter 3 numbers:10 25 20
if (n1 > n2) The largest number is: 25
{
if(n1 > n3)
max = n1;
else
max = n3;
}
else if(n2 > n3)
max = n2;
else
max = n3;
printf(“The largest number is: %d”,max);
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 15
Switch Statement
 If there are many conditions, then the switch
statement is used
 If any one of the condition is matched, then the
statements under that case will be executed
 If none of the conditions match, then the default case
will be executed
 At the end of every case break is used
 The expression of switch statement is an integer value
or a character

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 16


Switch
Syntax:
Statement
switch ( expression )
{
case value1:
program statement;
......
break;
case value2:
program statement;
.......
break;
…….
…….
case valuen:
program statement;
.......
break;
default:
program statement;
.......
break;
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 17
Switch Statement
Switch (Expression)

Case 1 Statements

Case 2 Statements

Case 3 Statements

Case 4 Statements

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 18


Switch Statement Example
//Calculator Program break;
#include <stdio.h> case '/':
void main ()
if (num2 != 0)
{
int num1, num2, result; result = num1 / num2;
char operator; break;
printf ("Enter two numbers:"); default:
scanf ("%d %d", &num1, &num2); printf ("\n unknown operator");
printf ("Enter an operator:"); break;
scanf ("%c", &operator); }
switch (operator) printf (“Result=%d", result);
{ }
case '+':
result = num1 + num2;
break; Output:
case '-': Enter two numbers:10 20
result = num1 - num2; Enter an operator: *
break; Result= 200
case '*':
result = num1 * num2;
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 19
Switch Statement Example
//Counting the vowels case 'i': count++;
#include<stdio.h> break;
void main() case 'o': count++;
{ break;
char ch; case 'u': count++;
int i,count=0; break;
printf(“\nEnter 10 characters”); }
for(i=0;i<10;i++) }
{ printf("\n Number of vowels present in the
Scanf(“%d”,&ch); entered characters is: %d",count);
switch(ch) getch();
{ }
case 'a': count++;
break; Output:
case 'e': count++; Enter 10 Characters: a b c d e f g h i j
break; Number of vowels present in the entered
characters is 3
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 20
Looping statements / Iteration
 The test may be either to determine whether the i has
repeated the specified number of times or to determine
whether the particular condition has been met.
 Type of Looping Statements are
 while statement

 do-while statement

 for statement

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 21


while statement
Syntax:
While
while (test condition) False
(test
{ condition)

body of the loop; True


}
Body of the loop;

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 22


while statement example
//Sum of n numbers Output:
#include<stdio.h> Enter a number: 5
void main() Sum of 5 numbers is 15
{
int n,x=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(x<=n)
{
sum=sum+x;
x++;
}
printf("Sum of %d numbers is %d",n, sum);
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 23
while statement example
//Sum of digits of a number Output
#include<stdio.h> Enter a number: 275
void main() Sum of digits of a number=14
{
int n,remainder,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(n>0)
{
remainder=n%10;
sum=sum+remainder;
n=n/10;
}
printf("Sum of digits of a number=%d",sum);
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 24
while statement example
//Armstrong if(sum==temp)
Number printf("%d is an Armstrong number“
#include<stdio.h> ,temp);
void main() else
{ printf("%d is not an Armstrong number“
int n, r, sum=0,temp; ,temp);
printf("Enter a number: "); }
scanf("%d",&n);
temp=n;
Output
while(n!=0)
Enter a number: 15
{
r=n%10; 15 is not an Armstrong number
sum=sum+(r*r*r);
n=n/10; Enter a number: 153
} 153 is an Armstrong number
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 25
do..while statement
 Since the body of the i is executed first and then the i
condition is checked we can be assured that the body of
the i is executed at least once.
Syntax: do
do Body of the loop
{
body of the loop;
} While
True
while (test condition); (test
condition)

False
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 26
do..while statement example
//Sum of n numbers Output:
#include<stdio.h> Enter a number: 5
void main() Sum of 5 numbers is 15
{
int n,x=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
do
{
sum=sum+x;
x++;
} while(x<=n);
printf("Sum of %d numbers is %d",n, sum);
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 27
do..while statement example
//Find the reverse of a given number Output
#include<stdio.h> Enter the number:275
void main() Reversed number is 572
{
int n, rem, reverse=0;
printf(“Enter the number:”);
scanf(“%d”,&n);
do
{
rem=n%10;
reverse=reverse*10+rem;
n=n/10;
} while(n>0);
printf(“\nReversed number is %d”,reverse);
}

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 28


while and do..while comparison
While Do…while
1) Syntax: 1) Syntax:
while(condition) do
{ {
Body of the loop Body of the loop
} }while(condition);
2) This is decision making and 2) This is also -decision making
looping statement and looping statement
3) This is the top tested loop 3) This is the bottom tested loop
4) Loop will be executed atleast
4)Loop will not be executed if the
once even though the condition is
condition is false in first check
false in first check
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 29
for statement
■ The for loop is most commonly and popularly used looping statement
in C. The for loop allows us to specify three things about the loop
control variable i in a single line. They are,
Initialization

■ Test condition to determine whether the loop should continue or


not
■ Increment or decrement
Syntax:
for(initialization ; test condition ; increment/decrement)
{
body of the loop;
}

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 30


for statement
Initialization;

Increment/Decrement;

Body of the loop

True test
condition

False

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 31


for statement example
//Sum of n numbers Output:
#include<stdio.h> Enter a number: 5
void main() Sum of 5 numbers is 15
{
int n,i,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("Sum of %d numbers is %d",n, sum);
}

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 32


for statement example
//Sum of n numbers Output:
#include<stdio.h> Enter a number: 5
void main() Sum of 5 numbers is 15
{
int n,i=1,sum=0; //initialization
printf("Enter a number: ");
scanf("%d",&n);
for(;i<=n;) //test condition
{
sum=sum+i;
i++; //increment
}
printf("Sum of %d numbers is %d",n, sum);
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 33
for statement example
//12+22+32+…. n2

#include<stdio.h> //<math.h> Output


void main()
{ Enter the number:5
int n, i,sum=0;
Sum of series=55
printf(“Enter the number:”);
scanf(“%d”, &n);
for(i=1;i <= n;i++)
{
sum = sum + i*i; //pow(i,2)

}
printf(“Sum of series=%d”,sum);
}

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 34


for statement example
// Number 1 to 10 divisible by 2 but
not divisible by 3 and 5
Output
#include<stdio.h>
void main() 2
{ 4
int i;
8
for(i=1;i<=10;i++)
{
if(i%2==0 && i%3!=0 && i%5!=0)
printf("%d\n",i);
}
}

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 35


Unconditional Branching / Jump
Statement
 A Jump statement transfers the control from one point
to another without checking any condition (ie)
unconditionally
 goto
 break
 continue

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 36


goto statement
The goto statement used to transfer the program control
unconditionally from one statement to another statement.
The general usage is as follows:
goto label; Label:
………… Statement;
.............. …………
Label: Statement; goto label;
…………
The goto requires a label in order to identify the place where the
branch is to be made.
A label is a valid variable name followed by a colon.
Forward Jump – goto statement before the label
Backward Jump – goto statement after the label

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 37


goto statement example
//Sum of n numbers Output:
#include <stdio.h> Enter a number:5
void main () 1+2+3+…+5=15
{
int n, sum = 0, i = 0;
printf ("Enter a number:");
scanf ("%d", &n);
inc: i++;
sum += i;
if (i < n)
goto inc;
printf ("\n 1+2+3+…+%d =
%d",n,sum) ;
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 38
break Statement
 A break statement can be used with in switch
statement or looping statement
 It terminates the execution of the nearest enclosing
switch or the nearest enclosing loop

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 39


break statement - Example
//Sum of n numbers Output:
#include<stdio.h> Enter a number: 5
void main() The sum of 5 numbers is 15
{
int i=1,n,sum=0;
printf(“\nEnter a number:”);
scanf(“%d”,&n);
while(1)
{
sum=sum+i;
i++;
if(i>n)
break;
}
printf(“\nThe sum of %d numbers is
%d”,n,sum);
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 40
continue statement
 During loop operations it may be necessary to skip a part of
the body of the loop under certain conditions.

 Like the break statement C supports similar statement called


continue statement.

 The continue statement causes the loop to be continued with


the next iteration after skipping any statement in between.

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 41


continue statement
//Sum of the entered numbers Output
#include < stdio.h > Enter the integer:11 22 33 -1
void main()
{ You have entered a negative number
int i, num, sum=0; 44
printf(“Enter the numbers:”); Sum of positive numbers entered =110
for (i = 1; i < =5; i++)
{
scanf(“%d”, &num);
if(num < 0)
{
printf(“You have entered a negative number\n”);
continue;
}
sum+=num;
}
printf(“Sum of positive numbers entered = %d”,sum);
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 42
break and continue comparison
Break Continue

1) Syntax: 1) Syntax:
break; continue;
2) Takes the control to outside of 2) Takes the control to beginning of
the loop the loop
3) It is used in switch statement, 3) It is used in for statement, while
for statement, while and do..while and do..while statement but not in
statement switch statement
4) Example: 4) Example:
for(i=0;i<n;i++) for(i=0;i<n;i++)
{ {
if(i==3) if(i<=3)
break; continue;
} }
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 43
Array
 Array is a collection of similar data items, which are stored
under a common name
 A value in array is identified by index or subscript enclosed
in square brackets with array number
 Eg: a[n]
 a – Name of the array
 n – no of elements in the array
 The array value starts from the index 0 (ie) a[0]
 Array elements are stored in contiguous memory location
 Arrays can be classified into 2 types
 1-dimensional array
 2-dimensional array
 Multi-dimensional array
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 44
Characteristics of Array
 Instead of declaring n variables for n values, only one
variable is enough using array
Ex: int a[3];
 All the elements of an array share the same name
 Element number in an array plays major role for
accessing each element
Ex: int a[3]={1,2,3};
a[0]=1
a[1]=2
a[2]=3

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 45


Characteristics of Array
 Any particular element of an array can be modified
separately without disturbing other element
Ex: int a[3]={1,2,3};
Replace 2 by 10 in above statement is done by writing
a[1]=10;
 Any array element a[] can be assigned to another ordinary
variable or array variable of its type
Ex: b=a[2];
a[1]=a[2];

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 46


One Dimensional Array
 The collection of data items can be stored under one variable
name using only one subscript, such an variable is called one-
dimensional array
Declaration of Array
dataType variableName[size];
 dataType – specifies the data type of the variable(int, float, char)
 variableName – specifies the name of the array
 size – specifies the number of elements that can be stored in the
array
 Eg: int mark[6];

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 47


One Dimensional Array
Initialiazing the Array:
dataType variableName[size] = { list of values separated by comma }
 Eg: int mark[6]={67, 89, 90, 78, 80, 70};
mark[0]=67 mark[3]=78
mark[1]=89 mark[4]=80
mark[2]=90 mark[5]=70

 We can also declare the above statement by


int mark[] = {67, 89, 90, 78, 80, 70};
 This approach works fine as long as we initialize every element
in the array.

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 48


One Dimensional Array
//C program to print the TOTAL
of 6 subject marks
#include<stdio.h> Output:
int main( ) Enter n value: 6
{ 10 20 30 40 50 60
int i, n, total=0, mark[ 10]; Total = 210
printf(“\nEnter n value:”);
scanf(“%d”,&n);
for(i=0; i<n; i++)
{
scanf(“%d”, &mark[i]);
}
for(i=0; i<n; i++)
{
total=total+mark[i];
}
printf(“\nTotal = %d”, total);
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 49
One Dimensional Array
//Count the number of positive &
negative numbers in the array printf(“\nPositive Numbers = %d”,pos);
#include<stdio.h> printf(“\nNegative Numbers = %d”, neg);
int main( ) }
{
int i, n, pos=0, neg=0, num[ 10];
printf(“\nEnter n value:”); Output:
scanf(“%d”,&n);
Enter n value: 5
for(i=0; i<n; i++)
{ scanf(“%d”, &num[i]); 12 -5 -8 0 34
} Positive Numbers = 2
for(i=0; i<n; i++)
{ if(num[i]>0) Negative Numbers = 2
pos++;
else if(num[i]<0)
neg++;
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 50
One Dimensional Array
//Searching the element in the array if(i<n)
#include<stdio.h> printf(“\nThe element %d is found in
int main( ) the position %d”, ele, i);
{ else
int i, n, pos=0, neg=0, num[10 ],ele; printf(“\nThe element is not found.”);
printf(“\nEnter n value:”); }
scanf(“%d”,&n);
printf(“\nEnter the elements of array:”); Output:
for(i=0; i<n; i++) Enter n value: 4
scanf(“%d”, &num[i]); Enter the elements of array: 10 20 30 40
printf(“\nEnter the element to be Enter the element to be searched: 50
searched:”); The element is not found.
scanf(“%d”,&ele);
for(i=0; i<n; i++)
{ if(num[i]= = ele)
break;
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 51
One Dimensional Array
//Sort the elements of array in Ascending Order
#include<stdio.h> printf(“\nThe sorted elements in the array
int main( ) are:”);
{ for(i=0; i<n; i++)
int i,j, a, n, number[10 ]; printf(“%d\t”, number[i]);
printf(“\nEnter n value:”); }
scanf(“%d”,&n);
printf(“\nEnter the elements of array:”); Output:
for(i=0; i<n; i++) Enter n value: 4
scanf(“%d”, &number[i]); Enter the elements of array: 20 40 30 10
for (i=0; i<n; ++i) The sorted elements in the array are:
{ 10 20 30 40
for (j=i+1; j<n; ++j)
{
if (number[i] < number[j])
{
temp=number[i];
number[i] = number[j];
number[j] = temp;
}
}
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 52
Two Dimensional Array
 2-Dim ensional array is used to store tables and matrices
 2-D array has two subscripts:
row_size : number of elements in each row

 column_size : number of columns in the array
 Declaration of 2-D array:
dataType variableName[row_size][column_size];
Eg: int marks[6][3];
 marks is a 2-D array with 6 rows and 3 elements in each row
 The initialization to the array can be done as below
int marks[6][3] = {75,60,80, 65,48,72, 54,76,69, 76,81,60,
42,53,70, 80,35,79};
or
int marks[6][3] = {{75,60,80},{65,48,72}, {54,76,69},
{76,81,60},{42,53,70},{80,35,79}};
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 53
2-Dimensional Array
Eg: int marks[6][3];
In memory 12 consecutive storage locations are reserved for the
variable marks as shown below:

marks[0][0] marks[0][1] marks[0][2] 75 60 80

marks[1][0] marks[1][1] marks[1][2] 65 48 72

marks[2][0] marks[2][1] marks[2][2] 54 76 69

marks[3][0] marks[3][1] marks[3][2] 76 81 60

marks[4][0] marks[4][1] marks[4][2] 42 53 70

marks[5][0] marks[5][1] marks[5][2] 80 35 79

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 54


2-Dimensional Array
//Print the elements in matrix a for(i=0;i<r; i++)
#include<stdio.h> {
void main() for(j=0;j<c; j++)
{ {
int r, c, i, j, a[10][10]; printff(“%d\t”, a[i][j]);
printf(“\nEnter row size and column size”); }
scanf(“%d %d”, &r, &c); printf(“\n”);
printf(“\nEnter the elements:”); }
for(i=0;i<r; i++) }
{ Output:
for(j=0;j<c; j++) Enter row size and column size: 2 3
{ Enter the elements: 1 2 3 4 5 6
scanf(“%d”, &a[i][j]); Matrix a :
} 1 2 3
} 4 5 6
printf(“\nMatrix a:\n”);

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 55


2-Dimensional Array
//Sum of two matrices if((r1!=r2) || (c1!=c2))
#include<stdio.h> printf(“\nMatrix cannot be added”);
void main() else
{ {
int r1, c1, r2, c2, i, j, a[5][5],b[5][5],d[5][5]; for(i=0;i<r1; i++)
printf(“\nEnter row and column size for Matrix a:”); {
scanf(“%d %d”, &r1, &c1); for(j=0;j<c1; j++)
printf(“\nEnter row and column size for Matrix a:”); {
scanf(“%d %d”, &r2, &c2); d[i][j]=a[i][j]+b[i][j];
printf(“\nEnter the elements of matrix a:”); }
for(i=0;i<r1; i++) }
{ for(j=0;j<c1; j++) }
{ printf(“\nSum of Matrix a & b:\n);
scanf(“%d”, &a[i][j]); for(i=0;i<r1; i++)
} {
} for(j=0;j<c1; j++)
printf(“\nEnter the elements of matrix b:”); {
for(i=0;i<r2; i++) printf(“%d\t”, d[i][j]);
{ for(j=0;j<c2; j++) }
{ printf(“\n”);
scanf(“%d”, &b[i][j]); }
}
}
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 56
2-Dimensional Array
//Sum of two matrices
Output:
Enter row and column size for matrix a: 2 3
Enter row and column size for matrix b: 2 3
Enter the elements of matrix a: 1 2 3 4 5 6
Enter the elements of matrix b: 1 2 3 4 5 6
Sum of Matrix a & b:
2 4 6
8 10 12

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 57


2-Dimensional Array
//Multiplication of two matrices if(c1!=r2)
#include<stdio.h> printf(“\nMatrix cannot be multiplied”);
void main() else
{ { for(i=0;i<r1; i++)
int r1, c1, r2, c2. i, j,k,a[5][5],b[5][5],d[5][5]; { for(j=0;j<c2; j++)
printf(“\nEnter row and column size for Matrix a:”); {
scanf(“%d %d”, &r1, &c1); d[i][j]=0;
printf(“\nEnter row and column size for Matrix a:”); for(k=0;k<r2;k++)
scanf(“%d %d”, &r2, &c2); {
printf(“\nEnter the elements of matrix a:”); d[i][j]=d[i][j]+a[i][k]*b[k][j];
for(i=0;i<r1; i++) }
{ for(j=0;j<c1; j++) }
{ }
scanf(“%d”, &a[i][j]); } printf(“\nMultiplication of Matrix a & b:\n);
} for(i=0;i<r1; i++)
} {
printf(“\nEnter the elements of matrix b:”); for(j=0;j<c2; j++)
for(i=0;i<r2; i++) {
{ for(j=0;j<c2; j++) printf(“%d\t”, d[i][j]);
{ }
scanf(“%d”, &b[i][j]); printf(“\n”);
} }
} }
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 58
2-Dimensional Array
//Multiplication of two matrices
Output:
Enter row and column size for matrix a: 2 3
Enter row and column size for matrix b: 3 2
Enter the elements of matrix a:
123
456
Enter the elements of matrix b:
12
34
56
Multiplication of Matrix a & b:
22 28
49 64
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 59
Pointer
 Pointer stores the address of another variable
 It refers to a memory location

Features of Pointer
 Pointers save memory space
 Execution time of using pointer is faster
 Pointers are used with data structure
 Pointers are used in file handling

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 60


Pointer Declaration
Pointer Declaration:
 The general form is
data_type *variableName;
• * : Indirection operator / Dereference operator

 Examples
int *iptr; //iptr is a pointer to an integer variable
float *fptr; //fptr is a pointer to a floating point variable
char *cptr; //cptr is a pointer to a character
const int *ptric; //ptric is a pointer to an integer constant

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 61


Pointer
Assigning Values to pointer using ‘&’ operator:
 Declare a pointer
 Assign the address of a variable to a pointer
 Example
1000
int *ptr, a=10; 1001
10 a

ptr = &a; 1002

 a = 10 1003

 ptr = address of a (1000) 1005


1000 ptr
 *ptr = 10 (value stored in 1000) 1006 *ptr
1007

1008

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 62


Pointer- Example
#include<stdio.h>
void main()
ptr address of i
{
int i = 5;
int *ptr; i 5
ptr = &i;
Output:
printf(“i = %d\n”, i); value of ptr =
i = 5
printf(“*ptr = %d\n”, *ptr); address of ‘i’
*ptr = 5
printf(“ptr = %p\n”, ptr); in memory
ptr = effff5e0
}

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 63


Pointer – Example
include< stdio.h >
void main()
{ Output:
int a, *iptr; a = 123 is stored at address 1002
float b, *fptr; b = 12.34 stored at address 1005
char ch, *cptr; Character a stored at address 1004
a=123;
b=12.34;
ch=’a’;
iptr=&a;
fptr=&b;
cptr=&ch;
printf(“a = %d is stored at address %p\n”, *iptr, iptr);
printf(“b = %f is stored at address %p\n”, *fptr, fptr);
printf(“Character %c is stored at address %p\n”, *cptr, cptr);
}

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 64


Pointer Example

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 65


Void Pointer
 It is a generic pointer which points to the address of any variable of
any data type(char, int, float)
 Syntax:
void *ptr;
 After assigning the address of a variable to a void pointer, we should
typecast the void * to (datatype) *
 Example
void *p;
char ch=‘A’;
p=&ch;
printf(“*p=%c”,p); //Not valid because p is void pointer
printf(“*p=%c”,*((char*)p) ); //valid because void is typecase
to char datatype

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 66


Void Pointer
Example: Output:
#include<stdio.h> *ptr=10
void main() *ptr=10.230000
{
void *ptr;
int i=10;
float j=10.23;
ptr=&i; //Assigning address of integer to void pointer
printf(“*ptr=%d”,*((int*)ptr));
ptr=&j;
printf(“*ptr=%d”,*((float*)ptr));
getch();
}

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 67


Null Pointer
 Null pointer is a special pointer which does not point to
anywhere
 It does not hold the address of a variable or function
 It has value 0 or NULL
 Syntax:
int *ptr=NULL;
(or)
int *ptr=0;
(or)
int *ptr;
ptr=NULL;
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 68
Pointer to an Array
 An array name itself is an address or pointer. It points to
the address of the first element of an array
Declaration
int *ptr, a[10];
ptr=a;

 One Dimensional Array


a[i]  *(p+i)

 Two Dimensional Array


a[i][j]  *(*(p+i)+j)

Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 69


Pointer to an array
Example: Output:
#include<stdio.h> Enter the size 3
void main() Enter the array elements
{ int i,n ,a[10]; 1
int *ptr; 2
printf("Enter the size\n"); 3
scanf("%d",&n); Array elements are : 1 2 3
printf("Enter the array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
ptr=a;
printf(“Array elements are : ");
for(i=0;i<n;i++)
{
printf(“%d “,*(ptr+i));
}
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 70
Array of Pointers
Example: Output:
#include<stdio.h> Address of 1 is 0028FEF4
void main() Address of 2 is
{ 0028FEF8
int *arrp[3]; Address of 3 is
int a[3]={1,2,3}; 0028FEFC
for(i=0;i<3;i++)
arrp[i]=&a[i];
for(i=0;i<3;i++)
printf(“Address of %d is
%u\n“,*arrp[i],arrp[i]);
}
Prepared by Kiruthika S.S., AP/ CSE/ SRMIST, Ramapuram 71

You might also like