SlideShare a Scribd company logo
Session 4
Complex Loops with Conditions
Tashreef Muhammad
39th Batch (Prototype 39), Dept. of CSE
Ahsanullah University of Science and Technology
Complex Loops with Conditions
1. Nested Loops
2. Multidimensional Arrays
3. Loop Control
4. Linear search
2
Tashreef Muhammad | CSE 39th Batch | AUST
29 Jan, 2021
Complex Loops with Conditions
● Loops inside loops
○ Allows to do more using loops
○ Many Applications
■ Multidimensional Array traversing
○ General Structure
■ Requires one loop control variable for each loop
■ Inner loop must finish before outer loop
3
Tashreef Muhammad | CSE 39th Batch | AUST
29 Jan, 2021
Nested Loops Multidimensional array Loop Control Linear Search
for(i = 0; i < n; ++i)
{
---
for(j = 0; j < m;
++j)
{
---
---
}
---
}
Complex Loops with Conditions
● Already learnt 1D Array
4
Tashreef Muhammad | CSE 39th Batch | AUST
29 Jan, 2021
Nested Loops Multidimensional Array Loop Control Linear Search
0 1 2 ….. n
X Axis
Complex Loops with Conditions
● Multidimensional Arrays
5
29 Jan, 2021
Nested Loops Multidimensional Array Loop Control Linear Search
0x0 0x1 ….. 0xm
1x0 1x1 ….. 1xm
.
.
.
…..
…..
…..
…..
…..
…..
…..
…..
…..
nx0 nx1 ….. nxm
Tashreef Muhammad | CSE 39th Batch | AUST
X Axis
Y
Axis
Row x Col
Complex Loops with Conditions
● Multidimensional Arrays
6
29 Jan, 2021
Nested Loops Multidimensional Array Loop Control Linear Search
Tashreef Muhammad | CSE 39th Batch | AUST
From Internet
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
7
Val of i:
Val of j:
What Should Be Printed:
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
8
Val of i:
Val of j:
What Should Be Printed:
0
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
9
Val of i:
Val of j:
What Should Be Printed:
0
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
10
Val of i:
Val of j:
What Should Be Printed:
0
0
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
11
Val of i:
Val of j:
What Should Be Printed:
0
0
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
12
Val of i:
Val of j:
What Should Be Printed:
21
0
0
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
13
Val of i:
Val of j:
What Should Be Printed:
21
0
0 1
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
14
Val of i:
Val of j:
What Should Be Printed:
21
0
0 1
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
15
Val of i:
Val of j:
What Should Be Printed:
21 75
0
0 1
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
16
Val of i:
Val of j:
What Should Be Printed:
21 75
0
0 1 2
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
17
Val of i:
Val of j:
What Should Be Printed:
21 75
0
0 1 2
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
18
Val of i:
Val of j:
What Should Be Printed:
21 75 81
0
0 1
0
0 1 2
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
19
Val of i:
Val of j:
What Should Be Printed:
21 75 81
0
0 1
0
0 1 2 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
20
Val of i:
Val of j:
What Should Be Printed:
21 75 81
0
0 1 2 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
21
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
0
0 1
0
0 1 2 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
22
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
0
0 1
0
0 1 2 3 4
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
23
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
0
0 1 2 3 4
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
24
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
0
0 1 2 3 4
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
25
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
0
0 1 2 3 4
1
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
26
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
0
0 1 2 3 4
1
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
27
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
0
0 1 2 3 4
1
0
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
28
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
38 89 14 45
0
0 1 2 3 4
1
0 1 2 3 4
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
29
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
38 89 14 45
0
0 1 2 3 4
1
0 1 2 3 4
2
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
30
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
38 89 14 45
0
0 1 2 3 4
1
0 1 2 3 4
2
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
31
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
38 89 14 45
50 26 77 19
0
0 1 2 3 4
1
0 1 2 3 4
2
0 1 2 3
4
0 1 2 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
32
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
38 89 14 45
50 26 77 19
0
0 1 2 3 4
1
0 1 2 3 4
2 3
4
4
0 1 2 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
33
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
38 89 14 45
50 26 77 19
0
0 1 2 3 4
1
0 1 2 3 4
2 3
4
4
0 1 2 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
34
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
38 89 14 45
50 26 77 19
47 21 27 30
0
0 1 2 3 4
1
0 1 2 3 4
2
0 1 2 3
4
0 1 2 3 4
4
0 1 2 3
3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
35
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
38 89 14 45
50 26 77 19
47 21 27 30
0
0 1 2 3 4
1
0 1 2 3 4
2 3
4
4
0 1 2 3
4
0 1 2 3
4
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
36
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
38 89 14 45
50 26 77 19
47 21 27 30
0
0 1 2 3 4
1
0 1 2 3 4
2 3
4
4
0 1 2 3
4
0 1 2 3
4
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
37
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
38 89 14 45
50 26 77 19
47 21 27 30
33 37 28 51
0
0 1 2 3 4
1
0 1 2 3 4
2
0 1 2 3
4
0 1 2 3 4
4
0 1 2 3
3
4
0 1 2 3
4
4
0 1 2 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
38
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
38 89 14 45
50 26 77 19
47 21 27 30
33 37 28 51
0
0 1 2 3 4
1
0 1 2 3 4
2 3
4
4
0 1 2 3
4
0 1 2 3
4 5
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
4
0 1 2 3
Traverse 2D Array with Nested Loops
#include <stdio.h>
int main(void)
{
int i, j, arr[5][4];
arr[5][4] = { { 21, 75, 81, 13 },
{ 38, 89, 14, 45 },
{ 50, 26, 77, 19 },
{ 47, 21, 27, 30 },
{ 33, 37, 28, 51 } };
for( i = 0; i < 5; ++i)
{
for(j = 0; j < 4; ++j)
{
printf(“%d “, arr[i][j]);
}
printf(“n”);
}
return 0;
}
39
Val of i:
Val of j:
What Should Be Printed:
21 75 81 13
38 89 14 45
50 26 77 19
47 21 27 30
33 37 28 51
0
0 1 2 3 4
1
0 1 2 3 4
2 3
4
4
0 1 2 3
4
0 1 2 3
4 5
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
4
0 1 2 3
Complex Loops with Conditions
● Character Array
○ 1D Array of Characters
○ Why Looking at it Specifically
■ Here comes strings!!!!
■ Strings are consecutive characters
■ Remember ASCII Characters taught in Session 1
40
29 Jan, 2021
Nested Loops Multidimensional Array Loop Control Linear Search
Tashreef Muhammad | CSE 39th Batch | AUST
Complex Loops with Conditions
● Character Array as Strings
○ Strings as character array
■ The null character: ‘0’
■ Turning Character arrays to string
○ <string.h> for Strings
■ Functions to manipulate string
41
29 Jan, 2021
Nested Loops Multidimensional Array Loop Control Linear Search
Tashreef Muhammad | CSE 39th Batch | AUST
Complex Loops with Conditions
● Functions for String Manipulation
○ gets() Taking Inputs
○ puts() Printing Outputs
○ strcmp() String Comparison
○ strlen() Length of String
○ strcat() String Concatenation
42
29 Jan, 2021
Nested Loops Multidimensional Array Loop Control Linear Search
Tashreef Muhammad | CSE 39th Batch | AUST
Complex Loops with Conditions
● Loop Control Statement
○ Continue
○ Break
● Combination of Loops with Conditions
○ Complex Loops
43
29 Jan, 2021
Nested Loops Multidimensional Array Loop Control Linear Search
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse to Find Element in Array
44
Find : 14
Array : 56, 48, 26, 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
int ans = -1, arr[4] = {56, 48, 26, 3};
for(i = 0; i < 4; ++i)
{
if(arr[i] == 14)
{
ans = i;
break;
}
}
if(ans == -1)
printf(“Not Found”);
else
printf(“Found at index %d.”, ans);
56 48 26 3
i
:
ans
:
-1
Traverse to Find Element in Array
45
Find : 14
Array : 56, 48, 26, 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
int ans = -1, arr[4] = {56, 48, 26, 3};
for(i = 0; i < 4; ++i)
{
if(arr[i] == 14)
{
ans = i;
break;
}
}
if(ans == -1)
printf(“Not Found”);
else
printf(“Found at index %d.”, ans);
56 48 26 3
i
:
ans
:
-1
0
Traverse to Find Element in Array
46
Find : 14
Array : 56, 48, 26, 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
int ans = -1, arr[4] = {56, 48, 26, 3};
for(i = 0; i < 4; ++i)
{
if(arr[i] == 14)
{
ans = i;
break;
}
}
if(ans == -1)
printf(“Not Found”);
else
printf(“Found at index %d.”, ans);
56 48 26 3
i
:
ans
:
-1
1
Traverse to Find Element in Array
47
Find : 14
Array : 56, 48, 26, 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
int ans = -1, arr[4] = {56, 48, 26, 3};
for(i = 0; i < 4; ++i)
{
if(arr[i] == 14)
{
ans = i;
break;
}
}
if(ans == -1)
printf(“Not Found”);
else
printf(“Found at index %d.”, ans);
56 48 26 3
i
:
ans
:
-1
2
Traverse to Find Element in Array
48
Find : 14
Array : 56, 48, 26, 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
int ans = -1, arr[4] = {56, 48, 26, 3};
for(i = 0; i < 4; ++i)
{
if(arr[i] == 14)
{
ans = i;
break;
}
}
if(ans == -1)
printf(“Not Found”);
else
printf(“Found at index %d.”, ans);
56 48 26 3
i
:
ans
:
-1
3
Traverse to Find Element in Array
49
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Traverse to Find Element in Array
50
Find : 26
Array : 56, 48, 26, 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
int ans = -1, arr[4] = {56, 48, 26, 3};
for(i = 0; i < 4; ++i)
{
if(arr[i] == 26)
{
ans = i;
break;
}
}
if(ans == -1)
printf(“Not Found”);
else
printf(“Found at index %d.”, ans);
56 48 26 3
i
:
ans
:
-1
Traverse to Find Element in Array
51
Find : 26
Array : 56, 48, 26, 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
int ans = -1, arr[4] = {56, 48, 26, 3};
for(i = 0; i < 4; ++i)
{
if(arr[i] == 26)
{
ans = i;
break;
}
}
if(ans == -1)
printf(“Not Found”);
else
printf(“Found at index %d.”, ans);
56 48 26 3
i
:
ans
:
-1
0
Traverse to Find Element in Array
52
Find : 48
Array : 56, 48, 26, 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
int ans = -1, arr[4] = {56, 48, 26, 3};
for(i = 0; i < 4; ++i)
{
if(arr[i] == 26)
{
ans = i;
break;
}
}
if(ans == -1)
printf(“Not Found”);
else
printf(“Found at index %d.”, ans);
56 48 26 3
i
:
ans
:
-1
1
Traverse to Find Element in Array
53
Find : 26
Array : 56, 48, 26, 3
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
int ans = -1, arr[4] = {56, 48, 26, 3};
for(i = 0; i < 4; ++i)
{
if(arr[i] == 26)
{
ans = i;
break;
}
}
if(ans == -1)
printf(“Not Found”);
else
printf(“Found at index %d.”, ans);
56 48 26 3
i
:
ans
:
2
2
Traverse to Find Element in Array
54
29 Jan, 2021
Tashreef Muhammad | CSE 39th Batch | AUST
Complex Loops with Conditions
● Search Algorithm
○ Linear Search
○ Binary Search
● Linear Search
○ Using loops to search in array
55
29 Jan, 2021
Nested Loops Multidimensional Array Loop Control Linear Search
Tashreef Muhammad | CSE 39th Batch | AUST
Thank You
Welcoming Queries
56

More Related Content

Similar to Complex loops with conditions

Java arrays
Java    arraysJava    arrays
Java arrays
Mohammed Sikander
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
SANTOSH RATH
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
Vishvjeet Yadav
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
Nattawut Phetmak
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
Intro C# Book
 
Os lab upto 1st mid
Os lab upto 1st midOs lab upto 1st mid
Os lab upto 1st mid
Murali Kummitha
 
Os lab upto_1st_mid
Os lab upto_1st_midOs lab upto_1st_mid
Os lab upto_1st_mid
Murali Kummitha
 
Os lab 1st mid
Os lab 1st midOs lab 1st mid
Os lab 1st mid
Murali Kummitha
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
Aroosa Rajput
 
array
arrayarray
array
teach4uin
 
Ansi c
Ansi cAnsi c
L5 array
L5 arrayL5 array
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
MaruMengesha
 
Lec23-CS110 Computational Engineering
Lec23-CS110 Computational EngineeringLec23-CS110 Computational Engineering
Lec23-CS110 Computational Engineering
Sri Harsha Pamu
 
ماترێکس به‌ کوردی ئارام
ماترێکس به‌ کوردی ئارامماترێکس به‌ کوردی ئارام
ماترێکس به‌ کوردی ئارام
Aram Jamal
 
RSA SIGNATURE: BEHIND THE SCENES
RSA SIGNATURE: BEHIND THE SCENESRSA SIGNATURE: BEHIND THE SCENES
RSA SIGNATURE: BEHIND THE SCENES
acijjournal
 
Pnno
PnnoPnno
C programs
C programsC programs
XIIInfo.Pract.PT3277.pdf
XIIInfo.Pract.PT3277.pdfXIIInfo.Pract.PT3277.pdf
XIIInfo.Pract.PT3277.pdf
kajalkhorwal106
 

Similar to Complex loops with conditions (20)

Java arrays
Java    arraysJava    arrays
Java arrays
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
 
Os lab upto 1st mid
Os lab upto 1st midOs lab upto 1st mid
Os lab upto 1st mid
 
Os lab upto_1st_mid
Os lab upto_1st_midOs lab upto_1st_mid
Os lab upto_1st_mid
 
Os lab 1st mid
Os lab 1st midOs lab 1st mid
Os lab 1st mid
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
 
array
arrayarray
array
 
Ansi c
Ansi cAnsi c
Ansi c
 
L5 array
L5 arrayL5 array
L5 array
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
 
Lec23-CS110 Computational Engineering
Lec23-CS110 Computational EngineeringLec23-CS110 Computational Engineering
Lec23-CS110 Computational Engineering
 
ماترێکس به‌ کوردی ئارام
ماترێکس به‌ کوردی ئارامماترێکس به‌ کوردی ئارام
ماترێکس به‌ کوردی ئارام
 
RSA SIGNATURE: BEHIND THE SCENES
RSA SIGNATURE: BEHIND THE SCENESRSA SIGNATURE: BEHIND THE SCENES
RSA SIGNATURE: BEHIND THE SCENES
 
Pnno
PnnoPnno
Pnno
 
C programs
C programsC programs
C programs
 
XIIInfo.Pract.PT3277.pdf
XIIInfo.Pract.PT3277.pdfXIIInfo.Pract.PT3277.pdf
XIIInfo.Pract.PT3277.pdf
 

More from Tashreef Muhammad

Lecture 01 - Discrete Mathematics
Lecture 01 - Discrete MathematicsLecture 01 - Discrete Mathematics
Lecture 01 - Discrete Mathematics
Tashreef Muhammad
 
Lecture 01 - Discrete Mathematics
Lecture 01 - Discrete MathematicsLecture 01 - Discrete Mathematics
Lecture 01 - Discrete Mathematics
Tashreef Muhammad
 
Lecture 2 - Numerical Methods
Lecture 2 - Numerical MethodsLecture 2 - Numerical Methods
Lecture 2 - Numerical Methods
Tashreef Muhammad
 
Lecture 1 - Numerical Methods
Lecture 1 - Numerical MethodsLecture 1 - Numerical Methods
Lecture 1 - Numerical Methods
Tashreef Muhammad
 
Lecture 1 - Numerical Methods
Lecture 1 - Numerical MethodsLecture 1 - Numerical Methods
Lecture 1 - Numerical Methods
Tashreef Muhammad
 
Lecture 2 - Numerical Methods
Lecture 2 - Numerical MethodsLecture 2 - Numerical Methods
Lecture 2 - Numerical Methods
Tashreef Muhammad
 

More from Tashreef Muhammad (6)

Lecture 01 - Discrete Mathematics
Lecture 01 - Discrete MathematicsLecture 01 - Discrete Mathematics
Lecture 01 - Discrete Mathematics
 
Lecture 01 - Discrete Mathematics
Lecture 01 - Discrete MathematicsLecture 01 - Discrete Mathematics
Lecture 01 - Discrete Mathematics
 
Lecture 2 - Numerical Methods
Lecture 2 - Numerical MethodsLecture 2 - Numerical Methods
Lecture 2 - Numerical Methods
 
Lecture 1 - Numerical Methods
Lecture 1 - Numerical MethodsLecture 1 - Numerical Methods
Lecture 1 - Numerical Methods
 
Lecture 1 - Numerical Methods
Lecture 1 - Numerical MethodsLecture 1 - Numerical Methods
Lecture 1 - Numerical Methods
 
Lecture 2 - Numerical Methods
Lecture 2 - Numerical MethodsLecture 2 - Numerical Methods
Lecture 2 - Numerical Methods
 

Recently uploaded

Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Diana Rendina
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 

Recently uploaded (20)

Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 

Complex loops with conditions

  • 1. Session 4 Complex Loops with Conditions Tashreef Muhammad 39th Batch (Prototype 39), Dept. of CSE Ahsanullah University of Science and Technology
  • 2. Complex Loops with Conditions 1. Nested Loops 2. Multidimensional Arrays 3. Loop Control 4. Linear search 2 Tashreef Muhammad | CSE 39th Batch | AUST 29 Jan, 2021
  • 3. Complex Loops with Conditions ● Loops inside loops ○ Allows to do more using loops ○ Many Applications ■ Multidimensional Array traversing ○ General Structure ■ Requires one loop control variable for each loop ■ Inner loop must finish before outer loop 3 Tashreef Muhammad | CSE 39th Batch | AUST 29 Jan, 2021 Nested Loops Multidimensional array Loop Control Linear Search for(i = 0; i < n; ++i) { --- for(j = 0; j < m; ++j) { --- --- } --- }
  • 4. Complex Loops with Conditions ● Already learnt 1D Array 4 Tashreef Muhammad | CSE 39th Batch | AUST 29 Jan, 2021 Nested Loops Multidimensional Array Loop Control Linear Search 0 1 2 ….. n X Axis
  • 5. Complex Loops with Conditions ● Multidimensional Arrays 5 29 Jan, 2021 Nested Loops Multidimensional Array Loop Control Linear Search 0x0 0x1 ….. 0xm 1x0 1x1 ….. 1xm . . . ….. ….. ….. ….. ….. ….. ….. ….. ….. nx0 nx1 ….. nxm Tashreef Muhammad | CSE 39th Batch | AUST X Axis Y Axis Row x Col
  • 6. Complex Loops with Conditions ● Multidimensional Arrays 6 29 Jan, 2021 Nested Loops Multidimensional Array Loop Control Linear Search Tashreef Muhammad | CSE 39th Batch | AUST From Internet
  • 7. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 7 Val of i: Val of j: What Should Be Printed: 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 8. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 8 Val of i: Val of j: What Should Be Printed: 0 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 9. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 9 Val of i: Val of j: What Should Be Printed: 0 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 10. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 10 Val of i: Val of j: What Should Be Printed: 0 0 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 11. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 11 Val of i: Val of j: What Should Be Printed: 0 0 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 12. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 12 Val of i: Val of j: What Should Be Printed: 21 0 0 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 13. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 13 Val of i: Val of j: What Should Be Printed: 21 0 0 1 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 14. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 14 Val of i: Val of j: What Should Be Printed: 21 0 0 1 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 15. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 15 Val of i: Val of j: What Should Be Printed: 21 75 0 0 1 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 16. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 16 Val of i: Val of j: What Should Be Printed: 21 75 0 0 1 2 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 17. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 17 Val of i: Val of j: What Should Be Printed: 21 75 0 0 1 2 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 18. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 18 Val of i: Val of j: What Should Be Printed: 21 75 81 0 0 1 0 0 1 2 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 19. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 19 Val of i: Val of j: What Should Be Printed: 21 75 81 0 0 1 0 0 1 2 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 20. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 20 Val of i: Val of j: What Should Be Printed: 21 75 81 0 0 1 2 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 21. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 21 Val of i: Val of j: What Should Be Printed: 21 75 81 13 0 0 1 0 0 1 2 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 22. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 22 Val of i: Val of j: What Should Be Printed: 21 75 81 13 0 0 1 0 0 1 2 3 4 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 23. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 23 Val of i: Val of j: What Should Be Printed: 21 75 81 13 0 0 1 2 3 4 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 24. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 24 Val of i: Val of j: What Should Be Printed: 21 75 81 13 0 0 1 2 3 4 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 25. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 25 Val of i: Val of j: What Should Be Printed: 21 75 81 13 0 0 1 2 3 4 1 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 26. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 26 Val of i: Val of j: What Should Be Printed: 21 75 81 13 0 0 1 2 3 4 1 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 27. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 27 Val of i: Val of j: What Should Be Printed: 21 75 81 13 0 0 1 2 3 4 1 0 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 28. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 28 Val of i: Val of j: What Should Be Printed: 21 75 81 13 38 89 14 45 0 0 1 2 3 4 1 0 1 2 3 4 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 29. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 29 Val of i: Val of j: What Should Be Printed: 21 75 81 13 38 89 14 45 0 0 1 2 3 4 1 0 1 2 3 4 2 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 30. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 30 Val of i: Val of j: What Should Be Printed: 21 75 81 13 38 89 14 45 0 0 1 2 3 4 1 0 1 2 3 4 2 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 31. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 31 Val of i: Val of j: What Should Be Printed: 21 75 81 13 38 89 14 45 50 26 77 19 0 0 1 2 3 4 1 0 1 2 3 4 2 0 1 2 3 4 0 1 2 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 32. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 32 Val of i: Val of j: What Should Be Printed: 21 75 81 13 38 89 14 45 50 26 77 19 0 0 1 2 3 4 1 0 1 2 3 4 2 3 4 4 0 1 2 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 33. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 33 Val of i: Val of j: What Should Be Printed: 21 75 81 13 38 89 14 45 50 26 77 19 0 0 1 2 3 4 1 0 1 2 3 4 2 3 4 4 0 1 2 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 34. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 34 Val of i: Val of j: What Should Be Printed: 21 75 81 13 38 89 14 45 50 26 77 19 47 21 27 30 0 0 1 2 3 4 1 0 1 2 3 4 2 0 1 2 3 4 0 1 2 3 4 4 0 1 2 3 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 35. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 35 Val of i: Val of j: What Should Be Printed: 21 75 81 13 38 89 14 45 50 26 77 19 47 21 27 30 0 0 1 2 3 4 1 0 1 2 3 4 2 3 4 4 0 1 2 3 4 0 1 2 3 4 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 36. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 36 Val of i: Val of j: What Should Be Printed: 21 75 81 13 38 89 14 45 50 26 77 19 47 21 27 30 0 0 1 2 3 4 1 0 1 2 3 4 2 3 4 4 0 1 2 3 4 0 1 2 3 4 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 37. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 37 Val of i: Val of j: What Should Be Printed: 21 75 81 13 38 89 14 45 50 26 77 19 47 21 27 30 33 37 28 51 0 0 1 2 3 4 1 0 1 2 3 4 2 0 1 2 3 4 0 1 2 3 4 4 0 1 2 3 3 4 0 1 2 3 4 4 0 1 2 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 38. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 38 Val of i: Val of j: What Should Be Printed: 21 75 81 13 38 89 14 45 50 26 77 19 47 21 27 30 33 37 28 51 0 0 1 2 3 4 1 0 1 2 3 4 2 3 4 4 0 1 2 3 4 0 1 2 3 4 5 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST 4 0 1 2 3
  • 39. Traverse 2D Array with Nested Loops #include <stdio.h> int main(void) { int i, j, arr[5][4]; arr[5][4] = { { 21, 75, 81, 13 }, { 38, 89, 14, 45 }, { 50, 26, 77, 19 }, { 47, 21, 27, 30 }, { 33, 37, 28, 51 } }; for( i = 0; i < 5; ++i) { for(j = 0; j < 4; ++j) { printf(“%d “, arr[i][j]); } printf(“n”); } return 0; } 39 Val of i: Val of j: What Should Be Printed: 21 75 81 13 38 89 14 45 50 26 77 19 47 21 27 30 33 37 28 51 0 0 1 2 3 4 1 0 1 2 3 4 2 3 4 4 0 1 2 3 4 0 1 2 3 4 5 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST 4 0 1 2 3
  • 40. Complex Loops with Conditions ● Character Array ○ 1D Array of Characters ○ Why Looking at it Specifically ■ Here comes strings!!!! ■ Strings are consecutive characters ■ Remember ASCII Characters taught in Session 1 40 29 Jan, 2021 Nested Loops Multidimensional Array Loop Control Linear Search Tashreef Muhammad | CSE 39th Batch | AUST
  • 41. Complex Loops with Conditions ● Character Array as Strings ○ Strings as character array ■ The null character: ‘0’ ■ Turning Character arrays to string ○ <string.h> for Strings ■ Functions to manipulate string 41 29 Jan, 2021 Nested Loops Multidimensional Array Loop Control Linear Search Tashreef Muhammad | CSE 39th Batch | AUST
  • 42. Complex Loops with Conditions ● Functions for String Manipulation ○ gets() Taking Inputs ○ puts() Printing Outputs ○ strcmp() String Comparison ○ strlen() Length of String ○ strcat() String Concatenation 42 29 Jan, 2021 Nested Loops Multidimensional Array Loop Control Linear Search Tashreef Muhammad | CSE 39th Batch | AUST
  • 43. Complex Loops with Conditions ● Loop Control Statement ○ Continue ○ Break ● Combination of Loops with Conditions ○ Complex Loops 43 29 Jan, 2021 Nested Loops Multidimensional Array Loop Control Linear Search Tashreef Muhammad | CSE 39th Batch | AUST
  • 44. Traverse to Find Element in Array 44 Find : 14 Array : 56, 48, 26, 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST int ans = -1, arr[4] = {56, 48, 26, 3}; for(i = 0; i < 4; ++i) { if(arr[i] == 14) { ans = i; break; } } if(ans == -1) printf(“Not Found”); else printf(“Found at index %d.”, ans); 56 48 26 3 i : ans : -1
  • 45. Traverse to Find Element in Array 45 Find : 14 Array : 56, 48, 26, 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST int ans = -1, arr[4] = {56, 48, 26, 3}; for(i = 0; i < 4; ++i) { if(arr[i] == 14) { ans = i; break; } } if(ans == -1) printf(“Not Found”); else printf(“Found at index %d.”, ans); 56 48 26 3 i : ans : -1 0
  • 46. Traverse to Find Element in Array 46 Find : 14 Array : 56, 48, 26, 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST int ans = -1, arr[4] = {56, 48, 26, 3}; for(i = 0; i < 4; ++i) { if(arr[i] == 14) { ans = i; break; } } if(ans == -1) printf(“Not Found”); else printf(“Found at index %d.”, ans); 56 48 26 3 i : ans : -1 1
  • 47. Traverse to Find Element in Array 47 Find : 14 Array : 56, 48, 26, 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST int ans = -1, arr[4] = {56, 48, 26, 3}; for(i = 0; i < 4; ++i) { if(arr[i] == 14) { ans = i; break; } } if(ans == -1) printf(“Not Found”); else printf(“Found at index %d.”, ans); 56 48 26 3 i : ans : -1 2
  • 48. Traverse to Find Element in Array 48 Find : 14 Array : 56, 48, 26, 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST int ans = -1, arr[4] = {56, 48, 26, 3}; for(i = 0; i < 4; ++i) { if(arr[i] == 14) { ans = i; break; } } if(ans == -1) printf(“Not Found”); else printf(“Found at index %d.”, ans); 56 48 26 3 i : ans : -1 3
  • 49. Traverse to Find Element in Array 49 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 50. Traverse to Find Element in Array 50 Find : 26 Array : 56, 48, 26, 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST int ans = -1, arr[4] = {56, 48, 26, 3}; for(i = 0; i < 4; ++i) { if(arr[i] == 26) { ans = i; break; } } if(ans == -1) printf(“Not Found”); else printf(“Found at index %d.”, ans); 56 48 26 3 i : ans : -1
  • 51. Traverse to Find Element in Array 51 Find : 26 Array : 56, 48, 26, 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST int ans = -1, arr[4] = {56, 48, 26, 3}; for(i = 0; i < 4; ++i) { if(arr[i] == 26) { ans = i; break; } } if(ans == -1) printf(“Not Found”); else printf(“Found at index %d.”, ans); 56 48 26 3 i : ans : -1 0
  • 52. Traverse to Find Element in Array 52 Find : 48 Array : 56, 48, 26, 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST int ans = -1, arr[4] = {56, 48, 26, 3}; for(i = 0; i < 4; ++i) { if(arr[i] == 26) { ans = i; break; } } if(ans == -1) printf(“Not Found”); else printf(“Found at index %d.”, ans); 56 48 26 3 i : ans : -1 1
  • 53. Traverse to Find Element in Array 53 Find : 26 Array : 56, 48, 26, 3 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST int ans = -1, arr[4] = {56, 48, 26, 3}; for(i = 0; i < 4; ++i) { if(arr[i] == 26) { ans = i; break; } } if(ans == -1) printf(“Not Found”); else printf(“Found at index %d.”, ans); 56 48 26 3 i : ans : 2 2
  • 54. Traverse to Find Element in Array 54 29 Jan, 2021 Tashreef Muhammad | CSE 39th Batch | AUST
  • 55. Complex Loops with Conditions ● Search Algorithm ○ Linear Search ○ Binary Search ● Linear Search ○ Using loops to search in array 55 29 Jan, 2021 Nested Loops Multidimensional Array Loop Control Linear Search Tashreef Muhammad | CSE 39th Batch | AUST