Unit 3 PPS Notes
Unit 3 PPS Notes
SYALLBUS
Iteration and Loops: Use of While, do While and for Loops, Multiple
Loop
Variables, Use of Break , Goto and Continue Statements.
Arrays: Array Notation and Representation, Manipulating Array
Elements, using
Multi-Dimensional Arrays. Character Arrays and Strings, Structure,
union, Enumerated Data types, Array of Structures, Passing Arrays to
Functions.
Iteration and loops: use of while, do while and for loops, multiple loop
variables,
use of break, Goto and Continue statement
Syntax:
while(condition)
{
statement(s);
}
Here statement(s) may be a single statement or a block of statements. The
condition may be any expression, and true is any nonzero value. The loop
iterates while the condition is true.When the condition becomes false,
program control passes to the line immediately following the loop. Flow
Diagram:
Example:
#include
<stdio.h>
main ()
{ int a =
10; while(
a < 20 )
{
printf("value of a: %d\
n", a); a++;
}
}
When the above code is compiled and executed, it
produces following result: value of a: 10
value of
a: 11
value of
a: 12
value of
a: 13
value of
a: 14
value of
a: 15
value of
a: 16
value of
a: 17
value of
a: 18
value of
a: 19
Unlike for and while loops, which test the loop condition at the top of the loop,
the do...while loop in C programming language checks its condition at the
bottom of the loop. A do...while loop is similar to a while loop, except that a
do...while loop is guaranteed to execute at least one time.
Syntax:
do
{ Statement;
} while ( condition );
If the condition is true, the flow of control jumps back up to do, and the
statement(s) in the loop execute again. This process repeats until the given
condition becomes false.
Flow Diagram:
DGI Greater Noida
Example:
#include
<stdio.h>
main ()
{ int a =
10; do
{ printf("value of a:
%d\n", a); a = a + 1;
} while ( a < 20 );
}
When the above code is compiled and executed, it produces
following result: value of a: 10
value of
a: 11
value of
a: 12
value of
a: 13
value of
a: 14
value of
a: 15
value of
a: 16
value of
a: 17
value of
a: 18
value of
a: 19
Example:
#include
<stdio.h>
main ()
{ for( int a = 10; a < 20; a = a + 1 )
{ printf("value of a: %d\n", a);
}
}
When the above code is compiled and executed, it produces
following result: value of a: 10
value of
a: 11
value of
a: 12
value of
a: 13
value of
a: 14
value of
a: 15
value of
a: 16
value of
a: 17
value of
a: 18
value of
a: 19
Jump statements
Example:
#include
<stdio.h>
main ()
{ int a = 10;
while( a <
20 )
{ printf("value of a:
%d\n", a); a++;
if( a > 15)
{ break;
}
}
}
When the above code is compiled and executed, it
produces following result: value of a: 10
value of
a: 11
value of
a: 12
value of
a: 13
value of
a: 14
value of
a: 15
(ii) Continue statement: Causes the loop to skip the remainder of its
body and immediately retest its condition prior to reiterating. The
continue statement in C programming language works somewhat
like the break statement. Instead of forcing termination, however,
continue forces the next iteration of the loop to take place, skipping
any code in between. Syntax: continue;
DGI Greater Noida
Flow Diagram:
Example:
#include
<stdio.h>
main ()
{ int a =
10; do
{ if( a == 15)
{ a = a + 1;
continue;
DGI Greater Noida
}
printf("value of a: %d\
n", a); a++;
} while( a < 20 );
}
When the above code is compiled and executed, it
produces following result: value of a: 10
value of
a: 11
value of
a: 12
value of
a: 13
value of
a: 14
value of
a: 16
value of
a: 17
value of
a: 18
value of
a: 19
Example:
#include
<stdio.h>
main ()
{ int a =
10;
LOOP:
do
{ if( a == 15)
{ a = a + 1;
goto LOOP;
}
data_type represents the type of elements present in the array. array_name represents the name of the
array.
Size represents the number of elements that can be stored in the array.
Example:
int age[100];
float sal[15];
char grade[20];
Here age is an integer type array, which can store 100 elements of integer type. The array sal is
floating type array of size 15, can hold float values. Grade is a character type array which holds 20
characters.
Initialization:
We can explicitly initialize arrays at the time of declaration. Syntax:
data_type array_name[size]={value1, value2,..........valueN};
Value1, value2, valueN are the constant values known as initializers, which are assigned to the array
elements one after another.
Example:
int marks[5]={10,2,0,23,4};
The values of the array elements after this initialization are: marks[0]=10, marks[1]=2, marks[2]=0,
marks[3]=23, marks[4]=4
DGI Greater Noida
NOTE:
1. In 1-D arrays it is optional to specify the size of the array. If size is omitted during
initialization then the compiler assumes the size of array equal to the number of initializers.
Example:int marks[]={10,2,0,23,4};Here the size of array marks is initialized to 5.
2. We can‘t copy the elements of one array to another array by simply assigning it.Example:
Processing:
For processing arrays we mostly use for loop. The total no. of passes is equal to the no. of elements
present in the array and in each pass one element is processed.
Example:
#include<stdio.h>
main()
{
int a[3],i;
for(i=0;i<=2;i++) //Reading the array values
{
printf(―enter the elements‖); scanf(―
%d‖,&a[i]);
}
for(i=0;i<=2;i++) //display the array values
{
printf(―%d‖,a[i]); printf(―\
n‖);
}
}
This program reads and displays 3 elements of integer type.
Example:
int a[4][5];
This is a 2-D array of 4 rows and 5 columns. Here the first element of the array is a[0][0] and last
element of the array is a[3][4] and total no.of elements is 4*5=20.
15
DGI Greater Noida
Initialization:
2-D arrays can be initialized in a way similar to 1-D arrays.
Example:
int m[4][3]={1,2,3,4,5,6,7,8,9,10,11,12};
The values are assigned as follows:
The initialization of group of elements as follows:
int m[4][3]={{11},{12,13},{14,15,16},{17}};
The values are assigned as:
Note:
In 2-D arrays it is optional to specify the first dimension but the second dimension should always be
present.
Example: int m[][3]={
{1,10},
{2,20,200},
{3}, {4,40,400} };
Here the first dimension is taken 4 since there are 4 roes in the initialization list. A 2-D array is
known as matrix.
Processing:
For processing of 2-D arrays we need two nested for loops. The outer loop indicates the rows and the
inner loop indicates the columns.
Example:
int a[4][5];
Reading values in
a for(i=0;i<4;i++)
for(j=0;j<5;j++) scanf(―
%d‖,&a[i][j]);
Displaying values of a
for(i=0;i<4;i++)
for(j=0;j<5;j++) printf(―
%d‖,a[i][j]);
16
DGI Greater Noida
printf("%d\t",a[i][j]);
}
printf("\nThe Second matrix is\n");
for(i=0;i<3;i++)
{ printf("\n");
for(j=0;j<3;j++)
printf("%d\t",b[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nThe Addition of two matrix is\n");
for(i=0;i<3;i++)
{ printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
return 0;
}
Rule: Addition of two matrixes is only possible if both matrixes are of same size.
Suppose two matrixes A and B is of same size m X n
Sum of two marixes is defined as
(A + B)ij = Aij + Bij
Where 1 ≤ i ≤ m and 1 ≤ j ≤ n
For example:
Suppose two matrixes A and B of size of 2 X 3 is as follow:
17
DGI Greater Noida
else{
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++) scanf("%d",&a[i]
[j]);
printf("\nEnter the Second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++) scanf("%d",&b[i]
[j]);
printf("\nThe First matrix is\n");
for(i=0;i<m;i++)
{ printf("\n");
for(j=0;j<n;j++)
{ printf("%d\t",a[i][j]);
}
}
printf("\nThe Second matrix is\n");
for(i=0;i<o;i++)
{ printf("\n");
for(j=0;j<p;j++)
{ printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++)
{ //row of first matrix
for(j=0;j<p;j++)
{ //column of second matrix
sum=0; for(k=0;k<n;k+
+)
sum=sum+a[i][k]*b[k][j]; c[i]
[j]=sum;
}
}
}
printf("\nThe multiplication of two matrix is\n");
for(i=0;i<m;i++)
{ printf("\n");
for(j=0;j<p;j++)
{ printf("%d\t",c[i][j]);
}
}
return 0;
}
18
DGI Greater Noida
Rule: Multiplication of two matrixes is only possible if first matrix has size m X n and other
matrix has size n x r. Where m, n and r are any positive integer. Multiplication of two
matrixes is defined as
Where 1 ≤ i ≤ m and 1 ≤ j ≤ n
For example:
Suppose two matrixes A and B of size of 2 x 2 and 2 x 3 respectively:
19
DGI Greater Noida
3
5
6
7
9
2
6
7
The matrix is
2 3 5
6 7 9
2 6 7
Sum of the diagonal elements of a matrix is: 16
Diagonal elements have been shown in the bold letter.We can observer the properties any
element Aij will diagonal element if and only if i = j
20
DGI Greater Noida
We can pass individual array elements as arguments to a function like other simple variables.
Example:
#include<stdio.h> void check(int); void main()
{
int a[10],i; clrscr();
printf(―\n enter the array elements:‖); for(i=0;i<10;i++)
{
scanf(―%d‖,&a[i]);
check(a[i]);
}
void check(int num)
{
if(num%2==0)
printf(―%d is even\n‖,num); else
printf(―%d is odd\n‖,num);
}
Output:
enter the array elements:
1 2 3 4 5 6 7 8 9 10
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
Example:
C program to pass a single element of an array to
function #include <stdio.h> void display(int a)
{
printf("%d",a);
}
int main()
{
int c[]={2,3,4};
display(c[2]); //Passing array element c[2] only. return 0;
}
Output
234
21
DGI Greater Noida
We can pass whole array as an actual argument to a function the corresponding formal arguments
should be declared as an array variable of the same type.
Example:
#include<stdio.h>
main()
{
int i, a[6]={1,2,3,4,5,6};
func(a);
printf(―contents of array:‖);
for(i=0;i<6;i++) printf(―
%d‖,a[i]); printf(‖\n‖);
}
func(int val[])
{
int sum=0,i;
for(i=0;i<6;i++)
{
val[i]=val[i]*val[i];
sum+=val[i];
}
printf(―the sum of squares:%d‖, sum);
}
Output
contents of array: 1 2 3 4 5 6
the sum of squares: 91
Example.2:
Write a C program to pass an array containing age of person to a function. This function should find
average age and display the average age in main function.
#include <stdio.h>
float average(float
a[]); int main()
{
float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18};
avg=average(c); /* Only name of array is passed as argument.
*/ printf("Average age=%.2f",avg);
return 0;
}
float average(float a[])
{
int i;
float avg, sum=0.0; for(i=0;i<6;+
+i)
22
DGI Greater Noida
{
sum+=a[i];
}
a
v
g
=
(
s
u
m
/
6
)
;
r
e
t
u
r
n
a
v
g
;
}
Output
Average age= 27.08
Example: 2
C Program to Print the
Alternate Elements in an
Array #include <stdio.h>
void main()
{
int array[10]; int i, j, temp;
printf("enter the element of an array \
n"); for (i = 0; i < 10; i++)
scanf("%d", &array[i]);
printf("Alternate elements of a given array \n");
for (i = 0; i < 10; i += 2) printf( "%d\n",
array[i]) ;
}
23