Nearby lessons

57 of 124

C - One Dimensional Array

The 1D array is a single row of elements — the simplest and most used array in C. Learn its memory layout and the standard algorithms: sum, average, largest, smallest, reverse and search.

Structure of a 1D Array

A one-dimensional array needs a single index because it has only one row:

Example01
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int marks[6] = {85, 92, 78, 95, 88, 73};
6 int i;
7 
8 printf("Index : ");
9 for (i = 0; i < 6; i++) printf("%4d", i);
10 
11 printf("\nValue : ");
12 for (i = 0; i < 6; i++) printf("%4d", marks[i]);
13 printf("\n");
14 return 0;
15}
Output
Index :    0   1   2   3   4   5
Value :   85  92  78  95  88  73

The Memory Layout

Elements sit one after another with no gaps. Because the spacing is uniform, C computes any element's address with one multiplication:

address of arr[i] = base address + (i × size of one element)

Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int nums[5] = {10, 20, 30, 40, 50};
6 int i;
7 
8 for (i = 0; i < 5; i++)
9 printf("nums[%d] = %2d at address %p\n",
10 i, nums[i], (void *) &nums[i]);
11 return 0;
12}
Output
nums[0] = 10  at address 000000000061FE00
nums[1] = 20  at address 000000000061FE04
nums[2] = 30  at address 000000000061FE08
nums[3] = 40  at address 000000000061FE0C
nums[4] = 50  at address 000000000061FE10

Sum and Average

Accumulate into a running total, then divide. Note the cast — without it you get integer division:

Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int marks[6] = {85, 92, 78, 95, 88, 73};
6 int size = sizeof(marks) / sizeof(marks[0]);
7 int i, total = 0;
8 
9 for (i = 0; i < size; i++)
10 total += marks[i];
11 
12 printf("Total : %d\n", total);
13 printf("Average : %.2f\n", (float) total / size);
14 return 0;
15}
Output
Total   : 511
Average : 85.17

Largest and Smallest

Start by assuming element 0 is both the largest and the smallest, then compare the rest against it:

Example04
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[7] = {45, 12, 89, 33, 67, 8, 91};
6 int size = sizeof(a) / sizeof(a[0]);
7 int i, max = a[0], min = a[0];
8 int maxAt = 0, minAt = 0;
9 
10 for (i = 1; i < size; i++)
11 {
12 if (a[i] > max) { max = a[i]; maxAt = i; }
13 if (a[i] < min) { min = a[i]; minAt = i; }
14 }
15 
16 printf("Largest : %d at index %d\n", max, maxAt);
17 printf("Smallest : %d at index %d\n", min, minAt);
18 return 0;
19}
Output
Largest  : 91 at index 6
Smallest : 8 at index 5

Reversing an Array

Swap the ends and walk inwards. Only loop to the middle — going all the way reverses it twice, back to the original:

Example05
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[6] = {10, 20, 30, 40, 50, 60};
6 int size = sizeof(a) / sizeof(a[0]);
7 int i, temp;
8 
9 printf("Before : ");
10 for (i = 0; i < size; i++) printf("%d ", a[i]);
11 
12 for (i = 0; i < size / 2; i++) /* only half the way */
13 {
14 temp = a[i];
15 a[i] = a[size - 1 - i];
16 a[size - 1 - i] = temp;
17 }
18 
19 printf("\nAfter : ");
20 for (i = 0; i < size; i++) printf("%d ", a[i]);
21 printf("\n");
22 return 0;
23}
Output
Before : 10 20 30 40 50 60
After  : 60 50 40 30 20 10 

Linear Search

Walk the array comparing each element. Record the index and stop early with break:

Example06
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[6] = {45, 12, 89, 33, 67, 8};
6 int size = sizeof(a) / sizeof(a[0]);
7 int i, key = 33, found = -1;
8 
9 for (i = 0; i < size; i++)
10 {
11 if (a[i] == key) { found = i; break; }
12 }
13 
14 if (found != -1)
15 printf("%d found at index %d\n", key, found);
16 else
17 printf("%d not found\n", key);
18 return 0;
19}
Output
33 found at index 2

Counting Even and Odd

A single pass with the modulus operator classifies every element:

Example07
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[8] = {12, 7, 45, 8, 33, 20, 91, 6};
6 int size = sizeof(a) / sizeof(a[0]);
7 int i, even = 0, odd = 0;
8 
9 for (i = 0; i < size; i++)
10 {
11 if (a[i] % 2 == 0) even++;
12 else odd++;
13 }
14 
15 printf("Even: %d\n", even);
16 printf("Odd : %d\n", odd);
17 return 0;
18}
Output
Even: 4
Odd : 4

Common Mistakes

  • Reversing with a full loopi < size instead of i < size/2 swaps everything twice and changes nothing.
  • Integer division in the averagetotal / size truncates; cast one operand to float.
  • Initialising max to 0 — fails for an array of all-negative numbers. Initialise to a[0].
  • Using found = 0 as "not found" — 0 is a valid index. Use -1.
  • Starting the max loop at i = 0 — harmless, but comparing a[0] with itself is wasted work; start at 1.
📝 Key Takeaways
  • A 1D array is one row of same-type elements: type name[size].
  • Elements occupy contiguous memory, indexed 0 to size-1.
  • Address of element i = base + (i * sizeof(type)).
  • Sum, max, min, reverse and search are all single loops.
  • Track the index, not just the value, when searching.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4