Chapter 8: Arrays
1. What is an Array?
- Array is a collection of elements of the same type
- All elements are stored in contiguous (next to each other) memory locations
- Each element has an index starting from 0
2. 1D Arrays (One-Dimensional Arrays)
Syntax for Declaration:
|
0 1 2 3 4 5 6 |
data_type array_name[size]; |
Examples:
|
0 1 2 3 4 5 6 7 8 |
int marks[5]; // Array of 5 integers float prices[10]; // Array of 10 floats char vowels[5]; // Array of 5 characters |
Important Rule: Size must be known at compile time (or use dynamic allocation – we’ll learn later).
3. Initializing Arrays
Way 1: Declare and Initialize at the same time
|
0 1 2 3 4 5 6 |
int marks[5] = {85, 92, 78, 90, 88}; |
Way 2: Initialize partially (remaining get 0)
|
0 1 2 3 4 5 6 |
int numbers[5] = {10, 20, 30}; // numbers = {10, 20, 30, 0, 0} |
Way 3: Let compiler decide size
|
0 1 2 3 4 5 6 |
int ages[] = {25, 30, 22, 28}; // Size becomes 4 automatically |
4. Accessing Array Elements
Use index inside square brackets [] Index starts from 0 to size-1
|
0 1 2 3 4 5 6 7 8 |
marks[0] = 85; // First element marks[1] = 92; // Second element marks[4] = 88; // Last element |
Example: Full Program – Storing and Printing Marks
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int main() { int marks[5] = {85, 92, 78, 90, 88}; int i; printf("Student Marks:\n"); for (i = 0; i < 5; i++) { printf("Roll %d: %d\n", i+1, marks[i]); } return 0; } |
Output:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
Student Marks: Roll 1: 85 Roll 2: 92 Roll 3: 78 Roll 4: 90 Roll 5: 88 |
Danger: Never access marks[5] – it is out of bounds → undefined behavior (crash or garbage value)
5. Taking Input in Array
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <stdio.h> int main() { int numbers[5]; int i; printf("Enter 5 numbers:\n"); for (i = 0; i < 5; i++) { printf("Number %d: ", i+1); scanf("%d", &numbers[i]); } printf("\nYou entered:\n"); for (i = 0; i < 5; i++) { printf("%d ", numbers[i]); } return 0; } |
6. Array Operations
A. Sum and Average of Array Elements
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <stdio.h> int main() { int marks[5] = {85, 92, 78, 90, 88}; int sum = 0; float average; int i; for (i = 0; i < 5; i++) { sum += marks[i]; } average = (float)sum / 5; printf("Sum = %d\n", sum); printf("Average = %.2f\n", average); return 0; } |
Output:
|
0 1 2 3 4 5 6 7 |
Sum = 433 Average = 86.60 |
B. Linear Search in Array
Find if a number exists in the array.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h> int main() { int numbers[5] = {23, 45, 12, 67, 89}; int search, i, found = 0; printf("Enter number to search: "); scanf("%d", &search); for (i = 0; i < 5; i++) { if (numbers[i] == search) { printf("%d found at position %d\n", search, i+1); found = 1; break; } } if (found == 0) { printf("%d not found in the array.\n", search); } return 0; } |
C. Sorting Array (Bubble Sort – Simple Method)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <stdio.h> int main() { int arr[5] = {64, 34, 25, 12, 22}; int i, j, temp; printf("Unsorted array: "); for (i = 0; i < 5; i++) { printf("%d ", arr[i]); } // Bubble Sort for (i = 0; i < 4; i++) { for (j = 0; j < 4 - i; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } printf("\nSorted array: "); for (i = 0; i < 5; i++) { printf("%d ", arr[i]); } return 0; } |
Output:
|
0 1 2 3 4 5 6 7 |
Unsorted array: 64 34 25 12 22 Sorted array: 12 22 25 34 64 |
7. Multidimensional Arrays (2D Arrays)
2D array is like a table (rows and columns).
Declaration:
|
0 1 2 3 4 5 6 |
int matrix[3][4]; // 3 rows, 4 columns |
Initialization:
|
0 1 2 3 4 5 6 7 8 9 10 |
int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; |
Accessing:
|
0 1 2 3 4 5 6 7 |
matrix[0][0] = 1; // First row, first column matrix[2][2] = 9; // Last element |
Example: Print 2D Array (Matrix)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> int main() { int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int i, j; printf("Matrix:\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } return 0; } |
Output:
|
0 1 2 3 4 5 6 7 8 9 |
Matrix: 1 2 3 4 5 6 7 8 9 |
Today’s Homework
- Create an array of 10 integers. Take input from user and print them in reverse order.
- Write a program to find the largest and smallest number in an array.
- Create a 2D array (3×3) and calculate the sum of all elements.
- Write a program that takes 5 numbers in an array and checks if a particular number is present (search).
- Try to print this pattern using nested loops and arrays:
text012345678911 21 2 31 2 3 4
