0% found this document useful (0 votes)
25 views23 pages

Unit 3 PPS Notes

PPS BCS 101 UNIT 3 NOTES

Uploaded by

dwivedialok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views23 pages

Unit 3 PPS Notes

PPS BCS 101 UNIT 3 NOTES

Uploaded by

dwivedialok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

UNIT III

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

Program Loops and Iteration :A loop statement allows us to execute a


statement or group of statements multiple times and following is the general
from of a loop statement in most of the programming languages:
Uses of while loop

A while loop statement in C programming language repeatedly executes a


target statement as long as a given condition is true.

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

(a) Do-while loop

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

(b) For loops


for loop is a repetition control structure that allows you to efficiently write a
loop that needs to execute a specific number of times. Syntax:
for ( initialization ; condition; increment )
{ statement(s);
}
Flow Diagram:
DGI Greater Noida
DGI Greater Noida

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

Loop Control Statements


Loop control statements change execution from its normal sequence.
When execution leaves a scope, all automatic objects that were
created in that scope are destroyed. C supports the following
control statements.
(i) Break statement: Terminates the loop or switch statement and
transfers execution to the statement immediately following the
loop or switch. The break statement in C programming language
has following two usage:
1. When the break statement is encountered inside a loop, the loop is
immediately terminated and program control resumes at the next
DGI Greater Noida

statement following the loop.


2. It can be used to terminate a case in the switch statement.
Syntax:
Break;
Flow Diagram:
DGI Greater Noida

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

(iii)GotoStatement: Transfers control to the labeled statement.


Though it is not advised to use goto statement in your program A
goto statement in C programming language provides an
unconditional jump from the goto to a labeled statement in the
same function. Syntax:
goto label;
label:
statement;

Downloaded from : uptukhabar.net


DGI Greater Noida
Flow Diagram:

Example:
#include
<stdio.h>
main ()
{ int a =
10;
LOOP:
do
{ if( a == 15)
{ a = a + 1;
goto LOOP;
}

Downloaded from : uptukhabar.net


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
Topic: Arrays- Array notation and representation, manipulating
array elements, using multi dimensional arrays,
passing arrays to functions.
Introduction
 A data structure is the way data is stored in the machine and the functions used to access
that data. An easy way to think of a data structure is a collection of related data items.
 An array is a data structure that is a collection of variables of one type that are accessed
through a common name.
 Each element of an array is given a number by which we can access that element which is
called an index. It solves the problem of storing a large number of values and manipulating
them.
 In an Array values of same type are stored. An array is a group of memory locations related
by the fact that they all have the same name and same type. To refer to a particular location
or element in the array we specify the name to the array and position number of particular
element in the array.
One Dimensional
Array Declaration:
Before using the array in the program it must be declared
Syntax:
data_type array_name[size];

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:

int a[5]={9,8,7,6,5}; int b[5];


b=a; //not valid
we have to copy all the elements by using for loop.
for(a=i; i<5; i++) b[i]=a[i];

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.

TWO DIMENSIONAL ARRAYS


Arrays that we have considered up to now are one dimensional array, a single line of elements. Often
data come naturally in the form of a table, e.g. spreadsheet, which need a two-dimensional array.
Declaration:
The syntax is same as for 1-D array but here 2 subscripts are used.
Syntax:
data_type array_name[rowsize][columnsize];
Rowsize specifies the no.of rows
Columnsize specifies the no.of columns.

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]);

Examples:1.Matrix addition in c language


#include<stdio.h>
int main()
{ int a[3][3],b[3][3],c[3][3],i,j;
printf("Enter the First matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++) scanf("%d",&a[i]
[j]);
printf("\nEnter the Second matrix->"); for(i=0;i<3;i+
+)
for(j=0;j<3;j++) scanf("%d",&b[i]
[j]);
printf("\nThe First matrix is\n");
for(i=0;i<3;i++)
{ printf("\n");
for(j=0;j<3;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;
}

Algorithm:Addition of two matrixes:

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:

2.C code for matrix multiplication


#include<stdio.h>
int main()
{ int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
printf("\nEnter the row and column of first matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the row and column of second matrix");
scanf("%d %d",&o,&p);
if(n!=o)
{ printf("Matrix mutiplication is not possible");
printf("\nColumn of first matrix must be same as row of second matrix");
}

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;
}

Algorithm:Multiplication of two matrixes:

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:

3. Sum of diagonal elements of a matrix in c


#include<stdio.h>
int main()
{ int a[10][10],i,j,sum=0,m,n;
printf("\nEnter the row and column of matrix: ");
scanf("%d %d",&m,&n);
printf("\nEnter the elements of matrix: ");
for(i=0;i<m;i++)
for(j=0;j<n;j++) scanf("%d",&a[i]
[j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++)
{ printf("\n");
for(j=0;j<m;j++)
{ printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ if(i==j)
sum=sum+a[i][j];
}
}
printf("\n\nSum of the diagonal elements of a matrix is: %d",sum);
return 0;
}
Sample output:
Enter the row and column of matrix: 3 3
Enter the elements of matrix: 2

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

Algorithm:Sum of diagonal element of matrix:

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

4. C program to find the largest element in an array


#include<stdio.h>
int main()
{ int a[50],size,i,big;
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ‖, size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++)
{ if(big<a[i])
big=a[i];
}
printf("\nBiggest: %d",big);
return 0;
}

20
DGI Greater Noida

1-d arrays using functions

Passing individual array elements to a function

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

Passing whole 1-D array to a function

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

You might also like