Nearby lessons

61 of 124

C - Array Operations (Sum, Max, Search)

Array Operations (Sum, Max, Search) is one of the foundational topics in C programming. This lesson explains Program 1: Read and Print an Array, Program 2: Sum and Average and Program 3: Find the Maximum with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Program 1: Read and Print an Array

Example01
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int marks[5], i;
6 
7 printf("Enter 5 marks: ");
8 for (i = 0; i < 5; i++) {
9 scanf("%d", &marks[i]);
10 }
11 
12 printf("Marks are: ");
13 for (i = 0; i < 5; i++) {
14 printf("%d ", marks[i]);
15 }
16 printf("\n");
17}
Output
Enter 5 marks: 80 90 75 60 88 Marks are: 80 90 75 60 88

Program 2: Sum and Average

Example02
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int marks[5] = {80, 90, 75, 60, 88};
6 int i, total = 0;
7 
8 for (i = 0; i < 5; i++) {
9 total = total + marks[i];
10 }
11 
12 printf("Sum = %d\n", total);
13 printf("Average = %.2f\n", total / 5.0);
14}
Output

Sum     = 393
Average = 78.60
      

Program 3: Find the Maximum

In simple words: the max program uses a running champion — start with the first element as the champion, then challenge each next element. Whoever is bigger becomes the new champion.
Example03
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int marks[5] = {80, 90, 75, 60, 88};
6 int i, max = marks[0]; // start with the first element
7 
8 for (i = 1; i < 5; i++) {
9 if (marks[i] > max) max = marks[i];
10 }
11 
12 printf("Maximum = %d\n", max);
13}
Output
Maximum = 90

Program 4: Find the Minimum

Example04
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int marks[5] = {80, 90, 75, 60, 88};
6 int i, min = marks[0]; // start with the first element
7 
8 for (i = 1; i < 5; i++) {
9 if (marks[i] < min) min = marks[i];
10 }
11 
12 printf("Minimum = %d\n", min);
13}
Output
Minimum = 60

Program 5: Linear Search

Trainer's Note: The search pattern above is called linear search — it checks every element one by one. The trick found = -1 (a value that can never be a valid index) tells us 'not found' at the end.
Example05
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int marks[5] = {80, 90, 75, 60, 88};
6 int key, i, found = -1; // -1 means 'not found'
7 
8 printf("Enter value to search: ");
9 scanf("%d", &key);
10 
11 for (i = 0; i < 5; i++) {
12 if (marks[i] == key) {
13 found = i;
14 break;
15 }
16 }
17 
18 if (found == -1)
19 printf("Not found\n");
20 else
21 printf("Found at index %d\n", found);
22}
Output
Enter value to search: 75 Found at index 2
📝 Key Takeaways
  • Array = same-type values under one name, accessed by index 0 to size-1.
  • Declare: int marks[5]; Initialize: int a[3]={1,2,3};
  • Use loops to read and print arrays.
  • Separate programs: sum/average, max, min, linear search.
  • 2D array = table; process with nested loops (rows + columns).
  • Matrix addition: c[i][j] = a[i][j] + b[i][j].

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2