Professional Documents
Culture Documents
1/1
CS1100 – Introduction to Programming
• Programming : From Turtle to C.
• Data Types in C, Operators. Input and the Output.
• Modifying the control flow in Programs if-else,
switch, loops : while, do-while, for. Week 1-6
• Implementing numerical methods using a
C-program.
• (single-dimensional) Arrays in C.
2/1
CS1100 – Introduction to Programming
• Programming : From Turtle to C.
• Data Types in C, Operators. Input and the Output.
• Modifying the control flow in Programs if-else,
switch, loops : while, do-while, for. Week 1-6
• Implementing numerical methods using a
C-program.
• (single-dimensional) Arrays in C.
• Strings, Multi-dimensional Arrays in C. Week 7
2/1
Character arrays
char name[20];
3/1
Character arrays
char name[20];
3/1
What is the output of this program?
#include<stdio.h>
main() {
char name[20] = "AVANI";
int i;
5/1
Example 1 : Finding the length of a given string
6/1
Example 1 : Finding the length of a given string
6/1
Example 1 : Finding the length of a given string
6/1
Example 2 : Compare two strings
Task : Given two strings s1, s2, check if s1 and s2 are the same.
7/1
Example 2 : Compare two strings
Task : Given two strings s1, s2, check if s1 and s2 are the same.
if (s1 == s2) This does not work
7/1
Example 2 : Compare two strings
Task : Given two strings s1, s2, check if s1 and s2 are the same.
if (s1 == s2) This does not work
Pseudo-code :
Find the length ` of the two
strings first. If they are differ-
ent, declare that the strings are
different.
For i ranging from 1 to `
• check if i th characters
are the same. If not,
declare NOT same.
Can we combine the two steps
above?
7/1
Example 2 : Compare two strings
Task : Given two strings s1, s2, check if s1 and s2 are the same.
if (s1 == s2) This does not work
Program Segment:
Pseudo-code :
Find the length ` of the two // strings are in arrays a and b
strings first. If they are differ- int i = 0;
ent, declare that the strings are
different. while (a[i] == b[i]){
if ((a[i] == ’\0’)||(b[i] == ’\0’))
For i ranging from 1 to ` break;
• check if i th characters i++;
}
are the same. If not,
declare NOT same. if (a[i] == ’\0’ && b[i] == ’\0’)
Can we combine the two steps printf("SAME");
else
above? printf("NOT SAME");
7/1
Example 3 : Palindromes
8/1
Example 3 : Palindromes
• malayalam
• neveroddoreven
• dontnod
8/1
Example 3 : Palindromes
• malayalam
• neveroddoreven
• dontnod
8/1
Example 3 : Palindromes
Pseudo-code :
9/1
Example 3 : Palindromes
10/1
Largest element in an array
10/1
Sorting an array in decreasing order
15 8 3 12 30 7 9 17 32 19
11/1
Sorting an array in decreasing order
15 8 3 12 30 7 9 17 32 19
11/1
Selection sort
15 8 3 12 30 7 9 17 32 19
12/1
Selection sort
15 8 3 12 30 7 9 17 32 19
32 8 3 12 30 7 9 17 15 19
12/1
Selection sort
15 8 3 12 30 7 9 17 32 19
32 8 3 12 30 7 9 17 15 19
32 30 3 12 8 7 9 17 15 19
12/1
Selection sort
15 8 3 12 30 7 9 17 32 19
32 8 3 12 30 7 9 17 15 19
32 30 3 12 8 7 9 17 15 19
.. .. .. .. .. .. .. .. .. ..
. . . . . . . . . .
32 30 19 17 15 12 9 8 7 3
Pseudo-code :
• while (i ≤ n )
• maxindex = index of the max element in the part of the array
indexed from i to n. Find maxindex.
12/1
Selection sort
15 8 3 12 30 7 9 17 32 19
32 8 3 12 30 7 9 17 15 19
32 30 3 12 8 7 9 17 15 19
.. .. .. .. .. .. .. .. .. ..
. . . . . . . . . .
32 30 19 17 15 12 9 8 7 3
Pseudo-code :
• while (i ≤ n )
• maxindex = index of the max element in the part of the array
indexed from i to n. Find maxindex. (We have solved this !!)
12/1
Selection sort
15 8 3 12 30 7 9 17 32 19
32 8 3 12 30 7 9 17 15 19
32 30 3 12 8 7 9 17 15 19
.. .. .. .. .. .. .. .. .. ..
. . . . . . . . . .
32 30 19 17 15 12 9 8 7 3
Pseudo-code :
• while (i ≤ n )
• maxindex = index of the max element in the part of the array
indexed from i to n. Find maxindex. (We have solved this !!)
• swap elements array[i] and array[maxindex];
12/1
Selection sort
15 8 3 12 30 7 9 17 32 19
32 8 3 12 30 7 9 17 15 19
32 30 3 12 8 7 9 17 15 19
.. .. .. .. .. .. .. .. .. ..
. . . . . . . . . .
32 30 19 17 15 12 9 8 7 3
Pseudo-code :
• while (i ≤ n )
• maxindex = index of the max element in the part of the array
indexed from i to n. Find maxindex. (We have solved this !!)
• swap elements array[i] and array[maxindex]; (We have solved
this too !!)
12/1
Day 2
13/1
Selection sort - from the pseudocode to the program
Pseudo-code :
for i ranging from 1 to n
• maxindex = the index of
the max element in the
part of the array indexed
from i to n. Find
maxindex.
• swap elements array[i]
and array[maxindex];
14/1
Selection sort - from the pseudocode to the program
Program Segment:
Pseudo-code :
for (i=0; i<n; i++)
for i ranging from 1 to n {
• maxindex = the index of max = i;
for (j=i+1; j<n; j++)
the max element in the {
part of the array indexed if (a[j] > a[max])
from i to n. Find max = j;
maxindex. }
temp = a[i];
• swap elements array[i] a[i] = a[max];
and array[maxindex]; a[max] = temp;
}
14/1
Selection sort – number of comparisons
15/1
Selection sort – number of comparisons
15/1
Selection sort – number of comparisons
15/1
Selection sort – number of comparisons
15/1
Selection sort – number of comparisons
15/1
Multi-dimensional arrays in C
16/1
Multi-dimensional arrays in C
16/1
Multi-dimensional arrays in C
16/1
Multi-dimensional arrays in C
#include<stdio.h>
main() {
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
}
}
16/1
Multi-dimensional arrays in C
17/1
Multi-dimensional arrays in C
17/1
Multi-dimensional arrays in C
17/1
Multi-dimensional arrays in C
17/1
Initializing Multi-dimensional arrays
#include<stdio.h>
main() {
int matrix1[3][4] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12};
int matrix2[][4] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12};
}
18/1
Initializing Multi-dimensional arrays
#include<stdio.h>
main() {
int matrix1[3][4] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12}; • Cannot omit the
column size.
int matrix2[][4] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12};
}
18/1
Initializing Multi-dimensional arrays
What does the program print?
/* Assume N1=3, N2=4 */
main() {
int matrix[N1][N2] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12};
int i;
for (i = 0; i<N1; i++) {
printf("%d\n", matrix[i][2]++);
}
20/1
Reading/Writing matrices at the input
20/1
Reading/Writing matrices at the input
20/1
Matrix Operations : Addition
• Write a program to add two matrices A and B
21/1
Matrix Operations : Addition
• Write a program to add two matrices A and B
#include<stdio.h>
main() {
int A[N1][N2];
int B[N1][N2];
/*initialize A, B suitably */
int C[N1][N2];
int i, j;
22/1
Matrix Operations : Multiplication
• Write a program to multiply matrices A and B
22/1
Matrix Operations : Multiplication
• Write a program to multiply matrices A and B
23/1
Matrix Operations : Multiplication
• Write a program to multiply matrices A and B
main() {
24/1
This week : Take Aways
24/1
This week : Take Aways
24/1
This week : Take Aways
24/1