Arrays & Strings
Arrays
Store many values of one type under one name: declaring, indexing, looping, and the sum, max, search and reverse programs.
An array stores many values of the same type in one contiguous block of memory, under one name. Instead of mark1, mark2, mark3… you write marks[0], marks[1], marks[2].
#include <stdio.h>
int main(void) {
int marks[5] = {78, 92, 65, 88, 71};
printf("First mark: %d\n", marks[0]);
printf("Last mark: %d\n", marks[4]);
marks[2] = 70; // change one element
for (int i = 0; i < 5; i++)
printf("marks[%d] = %d\n", i, marks[i]);
return 0;
}First mark: 78 Last mark: 71 marks[0] = 78 marks[1] = 92 marks[2] = 70 marks[3] = 88 marks[4] = 71
int marks[5] — five ints side by side in memory. Indexes run from 0 to size − 1.Declaring and initialising
int a[5]; // 5 ints, values are garbage (if local)
int b[5] = {1, 2, 3, 4, 5}; // fully initialised
int c[5] = {1, 2}; // rest become 0 -> {1, 2, 0, 0, 0}
int d[5] = {0}; // all zeros
int e[] = {4, 8, 15, 16, 23}; // size worked out from the list: 5The number of elements is sizeof(arr) / sizeof(arr[0]) — handy when you initialise without writing the size.
Reading an array and finding sum, average, max and min
#include <stdio.h>
int main(void) {
int n, a[100];
printf("How many numbers? ");
scanf("%d", &n);
printf("Enter %d numbers: ", n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
int sum = 0, max = a[0], min = a[0];
for (int i = 0; i < n; i++) {
sum += a[i];
if (a[i] > max) max = a[i];
if (a[i] < min) min = a[i];
}
printf("Sum = %d, Average = %.2f\n", sum, (float)sum / n);
printf("Max = %d, Min = %d\n", max, min);
return 0;
}How many numbers? 6 Enter 6 numbers: 45 12 89 33 89 7 Sum = 275, Average = 45.83 Max = 89, Min = 7
Linear search
Check each element until you find the target. In the worst case you look at all n elements: O(n).
#include <stdio.h>
int linearSearch(int a[], int n, int key) {
for (int i = 0; i < n; i++)
if (a[i] == key) return i; // found: return its index
return -1; // not found
}
int main(void) {
int a[] = {14, 3, 27, 9, 41, 6};
int n = sizeof(a) / sizeof(a[0]);
printf("27 found at index %d\n", linearSearch(a, n, 27));
printf("50 found at index %d\n", linearSearch(a, n, 50));
return 0;
}27 found at index 2 50 found at index -1
Notice how arrays are passed to functions: int a[] plus the size n. Inside the function, sizeof(a) would not give the array size — the function only receives the address of the first element. That is why the size is passed separately.
Reverse an array in place
Swap the first and last, then the second and second-last, meeting in the middle:
#include <stdio.h>
int main(void) {
int a[] = {1, 2, 3, 4, 5, 6, 7};
int n = 7;
for (int i = 0, j = n - 1; i < j; i++, j--) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
for (int i = 0; i < n; i++) printf("%d ", a[i]);
printf("\n");
return 0;
}7 6 5 4 3 2 1
Counting frequencies
Use a second array as a tally. Here count[d] counts how often digit d appears:
#include <stdio.h>
int main(void) {
int votes[] = {2, 3, 2, 1, 2, 3, 3, 3, 0, 2};
int count[4] = {0};
for (int i = 0; i < 10; i++) count[votes[i]]++;
for (int c = 0; c < 4; c++) printf("Candidate %d: %d votes\n", c, count[c]);
return 0;
}Candidate 0: 1 votes Candidate 1: 1 votes Candidate 2: 4 votes Candidate 3: 4 votes
Test yourself
4 questions · pick an answer to check it instantly
- 1
What is the index of the last element in
int a[10];?Indexes start at 0, so the last valid index is size − 1 = 9. - 2
What does
int a[5] = {3, 4};contain?When an initialiser list is shorter than the array, the remaining elements are set to 0. - 3
Why do functions that take arrays usually also take the size?
Arrays decay to pointers when passed, sosizeofinside the function gives the pointer size, not the array size. - 4
What is the worst-case time of linear search on
nelements?In the worst case (key absent or last) every element is checked once.