of 43
7/28/2019 TA ZC142-L5
1/43
TA ZC142 Computer ProgrammingLecture 5Date: 23/01/2013
7/28/2019 TA ZC142-L5
2/43
Last Lecture
Switch case statementRepetition Control Structure for statement while statement do-while statement
7/28/2019 TA ZC142-L5
3/43
Todays Agenda
Continue loops Do-While Loop
Arrays Derived Data Types 1-D Array 2-D Array Memory organization Programming Exercises
7/28/2019 TA ZC142-L5
4/43
Control Structure
Sequential Control Structure It s a sequential flow of execution of
instructions.Selection Control Structure
This control structure chooses amongalternative program statements.
Repetition Control Structure This control structure repeats a group of
program statements till it satisfies somecondition.
7/28/2019 TA ZC142-L5
5/43
Basics of loops1. Initialization of a condition variable.2. Execution of the loop statements.3. Test for a specified value of the condition
variable.
4. Updating the condition variable.5. C provides three constructs for loop
operations
6. The for statement7. The while statement8. The do while statement
7/28/2019 TA ZC142-L5
6/43
While
while (test){
loop_body;}
test
loop_body
T
F
Loop body can contain block of statements.Executes loop body as long as test evaluates to TRUE (non-zero).
Note: Test is evaluated b e f o r e executing loop body .
7/28/2019 TA ZC142-L5
7/43
Introducing while loop
sum = 0;for (i = 1; i
7/28/2019 TA ZC142-L5
8/43
While loop example
x = 0;while (x < 10){
printf(%d , x); x = x + 1;
}
char c = Y ;
while( c == Y )
{
printf(Inside Loop \n);
printf(Do you want to continue inside loop? (Y/N));
c = getchar();
}
7/28/2019 TA ZC142-L5
9/43
Infinite Loops
The following loop will never terminate:Why??? x = 0;
while (x < 10)
printf(%d , x);
Loop body does not change condition, so
test never fails.
This is a common programming error thatcan be difficult to find.
7/28/2019 TA ZC142-L5
10/43
Nested LoopsThe statement of for is again a for statementLoop body can (of course) be another loop.
/* print a multiplication table */for (m1 = 1; m1
7/28/2019 TA ZC142-L5
11/43
int main()
{int n = 100; // number int i, prime = TRUE;
for(j=2;j
7/28/2019 TA ZC142-L5
12/43
ArraysDerived Data Type
7/28/2019 TA ZC142-L5
13/43
Arrays
An array is fixed size sequenced collection of elements of the same data type addressableby index or subscript.
It can be used to represent a list of homogenous values
Examples:List of marks of studentsList of employee id no.s in an organization
7/28/2019 TA ZC142-L5
14/43
One Dimensional Arrays
List of items given one variable name using
only one subscriptDeclaration: type variable_name[size];
Here size indicates the maximum number of elementsThe subscript value start from 0 to s ize-1 Example:
int number[10];float number[10];char grade[10];
7/28/2019 TA ZC142-L5
15/43
Compile time initializationint num[5]={0,0,0,0,0};
float num[5]={0.0,10.5,11,-13.25};int num[]={1,2,3,4,5}; is it correct?
Consider the following initializationint num[4]={10,20,30,40,50};
Initialization
Invalid Statement
7/28/2019 TA ZC142-L5
16/43
Run time initialization
for(i=0; i
7/28/2019 TA ZC142-L5
17/43
#include#include#define N 10
int main() {float marks[N],sum,ave;int count,no;printf("enter size of array\n");scanf("%d",&no);if(no > N) {printf("size of array entered by you is exceeding the maximum
size\n");exit(0);
}for(sum=0.0,count=0;count
7/28/2019 TA ZC142-L5
18/43
/* Calculate and display the deviation */ for(i=0;i
7/28/2019 TA ZC142-L5
19/43
Problems with arrays!!!- No Bound Checking!!!
There is no check in C compiler to see if the subscript used for an array exceedsthe size of the array.Data entered with a subscript exceedingthe array size will
simply be placed in memory outside thearray limit and
lead to unpredictable results. It s a programmers responsibility to take care.
7/28/2019 TA ZC142-L5
20/43
Exercise on Arrays
Write a program to find a number in aninteger array. Also find the number of timesit occurs in the array.
Write a program to find maximum in anarray. Also print the index at which it occurs.
Write a program copy the elements of onearray into another array in reverse order.
7/28/2019 TA ZC142-L5
21/43
Exercise on Arrays
Write a program to count the number of students belonging to each of the following
groups of marks: 0-9,10-19,20- 29,..,90 -91,100. Assume there are 50 students.Declare an array to store the frequency of
marks in each group.
7/28/2019 TA ZC142-L5
22/43
Do while loop
7/28/2019 TA ZC142-L5
23/43
Do-While
do{
loop_body;}while (test);
loop_body
testT
F
Loop body can contain block of statements.Executes loop body as long as test evaluates to TRUE (non-zero).
Note: Test is evaluated af ter executing loop body.
7/28/2019 TA ZC142-L5
24/43
int i = 1,sum = 0;
do{
sum = sum + i;i = i+2;printf(%d %d \ n,i,sum);
} while(sum < 40 || i < 10);printf(%d %d \ n,i,sum);
OUTPUT
i sum
3 1
5 47 9
9 16
1125
11 25
7/28/2019 TA ZC142-L5
25/43
Write a program to find total marks in5 subjects for 50 students. Find the
average marks. Find the number of students above average ,belowaverage and at average. Find the
maximum total marks. Use arrays.
7/28/2019 TA ZC142-L5
26/43
Write a program to copy the elements of onearray into another array in reverse order.
Write a program to count the number of students belonging to each of the followinggroups of marks: 0-9,10-19,20- 29,..,90 -91,100. Assume there are 50 students.Declare an array to store the frequency of marks in each group.
7/28/2019 TA ZC142-L5
27/43
Write a multiplication tablefrom (1 X 1) to (10 X 10) usingdo-while loop.
7/28/2019 TA ZC142-L5
28/43
int main(){ int m1,m2,product;
m2 = 1;do {
m1 = 1;do {
product = m1 * m2; printf("%d X %d = %d\n",m2,m1,product); m1 = m1 + 1;} while(m1
7/28/2019 TA ZC142-L5
29/43
Continue statement
continue;sum = 0;for(i=0;i
7/28/2019 TA ZC142-L5
30/43
Two Dimension Array
7/28/2019 TA ZC142-L5
31/43
Two dimensional arrays also called as matrixDeclaration :
type variable_name[size1][size2];
size1 >Number of rows in matrix
size2 > Number of columns in matrix Examples :
int number[4][3]; /* 12 elements */float number[3][2]; /* 6 elements */char name[10][20]; /* 200 chars */
7/28/2019 TA ZC142-L5
32/43
Initialization of a 2-D Arrayint a[2][3]={1,2,3,4,5,6};int a[2][3]={{1,2,3}, {4,5,6}};int a[][3]={{1,2,3}, {4,5,6}} ;
How values will be assigned in each case??Following initializations are not allowedint a[3][]={2,4,6,8,10,12};int a[][]={2,4,6,8,10,12};
Note: If the first bracket pair is empty, thencompiler take the size from the number of inner
brace pairs
7/28/2019 TA ZC142-L5
33/43
Memory Map for 2-D Arrays
Kept in memory as a linear sequence of variables.
Two methods for storing- Row major Column major Example: int a[3][3];Row major storage:a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2],a[2][0], a[2][1], a[2][2]
Column major storage:a[0][0], a[1][0], a[2][0], a[0][1], a[1][1], a[2][1],a[0][2], a[1][2], a[2][2]
7/28/2019 TA ZC142-L5
34/43
Accessing Two Dimensional Array
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};for(i=0;i
7/28/2019 TA ZC142-L5
35/43
Working with two dimensional Arrays
Matrix Addition and Subtraction
Let A[m][n] and B[p][q] are two matrices.Precondition : m equals to p and n equals to q
7/28/2019 TA ZC142-L5
36/43
Matrix Addition
Algorithm Steps: 1. Read two matrices A and B and
initialize C matrix to zero2. Repeat (3) for i=0 to m-13. Repeat (3.a) for j=0 to n-1
3.a) C[i][j] = A[i][j] +B[i][j]4. Display C matrix.
7/28/2019 TA ZC142-L5
37/43
Write a program to find total marks of 5students in 3 subjects and store the totalmarks of each student in a onedimensional array. Also find the highest
marks in each subject and store in anotherarray.
7/28/2019 TA ZC142-L5
38/43
Matrix Multiplication:
M1xM2
7/28/2019 TA ZC142-L5
39/43
Pre condition: Number of columns in M1 should beequal to number of rows in M2
Algorithm Steps: 1. Read two matrices M1[m][n] and M2[n][r] andinitialize another matrix M3[m][r] for storing result.
2. Repeat for i=0 to m-13. Repeat for j=0 to r-14. M3[i][j] = 0 5. Repeat for k=0 to n-16. M3[i][j] += M1[i][k] * M2[k][j]7. 3. Print matrix M3.
7/28/2019 TA ZC142-L5
40/43
Write a program to read a matrix of characters. Dimensions are taken fromthe user.(a) Convert a lower case alphabet intoupper case alphabet.if there is a character other thanalphabet replace that character with
$ . Display the final matrix with all theupper case alphabets and $.
7/28/2019 TA ZC142-L5
41/43
(b) Find the vowels inthe resultant
matrix. Printnumber of vowelsin each row. Alsoprint the vowel
along with its index.
a f # T @
i s ! o m
V 5 u Q i
7/28/2019 TA ZC142-L5
42/43
7/28/2019 TA ZC142-L5
43/43
Any Doubts orQuestions?