1 / 15
π¦ Session 11: Arrays
Storing Many Values
Store 100 numbers in one variable? Yes, with arrays!
Marks of 50 students? A list of scores? One name, many boxes.
One array β Many values β¨
Today's Array Mission
π¦
Declare an array
Type, name, size
π’
Index (0-based)
First element is 0
π
Loops + arrays
Fill, print, sum
π₯
Read & write
scanf/printf with arr[i]
π
Simple programs
Sum, max, marks
π¨
Common traps
Bounds, off-by-one
π€ Why Arrays? Without vs With
β Without array (5 marks)
int m1, m2, m3, m4, m5;
scanf("%d", &m1);
scanf("%d", &m2);
Too many variables!
β
With array
int marks[50];
for (i=0; i<50; i++)
scanf("%d", &marks[i]);
One name, 50 boxes. π
Same type β’ One name β’ Use a loop
π¦ Declaring an Array
Type, name, and size in square brackets. Size is fixed.
int marks[5];
float scores[10];
int arr[100];
π Fixed size
You choose size when you write the program (e.g. 5, 10, 50).
π Same type
All elements are the same type (all int or all float).
π’ Index: Start from 0!
First element is index 0, not 1. Last element is size - 1.
int arr[5];
[0]
10
[1]
20
[2]
30
[3]
40
[4]
50
Valid indices: 0, 1, 2, 3, 4. Never use 5 (out of bounds!).
π₯ Reading & Writing Array Elements
arr[0] = 10;
arr[1] = 20;
printf("%d", arr[0]);
scanf("%d", &arr[2]);
Use & when reading into arr[i] with scanf: &arr[i]
π Loop: Fill and Print Array
int arr[5], i;
for (i = 0; i < 5; i++) {
printf("Enter value %d: ", i + 1);
scanf("%d", &arr[i]);
}
for (i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
Always use i < 5 (not i <= 5). Last index is 4.
β Sum of All Elements
int arr[5] = {10, 20, 30, 40, 50};
int sum = 0, i;
for (i = 0; i < 5; i++) {
sum = sum + arr[i];
}
printf("Sum = %d\n", sum);
Initializer: int arr[5] = {10, 20, 30, 40, 50};
π Find the Maximum
int arr[5], i, max;
max = arr[0];
for (i = 1; i < 5; i++) {
if (arr[i] > max)
max = arr[i];
}
printf("Max = %d\n", max);
Start from index 1; compare each with max.
π Marks of 50 Students (as in Session 10!)
int marks[50], i, sum = 0;
for (i = 0; i < 50; i++) {
printf("Mark for student %d: ", i + 1);
scanf("%d", &marks[i]);
sum = sum + marks[i];
}
printf("Average = %f\n", sum / 50.0);
One array, one loop: store and add. Then average!
π¨ Array Traps
β Out of bounds
arr[5] when size is 5 β valid indices are 0 to 4 only!
β Wrong loop condition
i <= 5 for arr[5] β last index is 4. Use i < 5.
β Forgetting & in scanf
scanf("%d", arr[i]); β must be &arr[i] for integers.
β Index 1 to N
Use 0 to N-1. First element is arr[0], not arr[1].
π§ Array Quiz!
In int arr[10], valid indices are 0 to 9.
First element of an array is at index 1.
We use a loop to fill or print many elements easily.
arr[10] for int arr[10] is valid (last element).
π Array Mastery Unlocked!
You've earned these badges:
π¦ Array Declarer
π’ Index Master (0-based)
π Loop + Array Pro
π Sum & Max
π 50 Students Ready
You can store many values in one variableβlike a pro! π
π Your Coding Mission!
π₯ Read 5 numbers
Store in array, then print them
β Sum & average
Array of 10, find sum and average
π Max and min
Find largest and smallest in array
π Marks
Store marks of N students, print average
π What's Coming Next!
Strings: Working with Text
Store names and sentences? Strings are arrays of characters!
π€ Think About This Week:
How would you store a person's name in C?