Unit 7: Arrays in C Programming | B. Sc Year: III

 🧭 Introduction

So far, you have learned how to store values in variables.
But what if you need to store many values of the same type—like marks of 50 students, temperatures of 30 days, or prices of 100 products?

This is where arrays come in.

Arrays allow us to store a collection of values using a single variable name, making programs simpler, organized, and efficient.
This unit is very important for exams, especially for program-based questions.


🔑 Key Concepts (Explained Simply)


1️⃣ What is an Array?

An array is a collection of elements of the same data type stored in contiguous memory locations.

Concept Example:

marks = [65, 70, 80, 90, 75]

Instead of declaring many variables:

int m1, m2, m3, m4, m5;

We use:

int marks[5];

✅ All elements share the same name but have different index numbers.


2️⃣ Types of Arrays in C

C mainly supports:

  1. One-Dimensional Array (1D)

  2. Two-Dimensional Array (2D)


3️⃣ One-Dimensional Array

A 1D array stores data in a single row.

Declaration:

int arr[5];

Initialization:

int arr[5] = {10, 20, 30, 40, 50};

📌 Index starts from 0, not 1.

Index01234
Value1020304050

4️⃣ Accessing Array Elements

Elements are accessed using index number:

printf("%d", arr[2]); // prints 30

✅ Index must be within array size.


5️⃣ Using Loops with Arrays

Arrays are usually processed using loops.

Example: Reading and Displaying Values

int a[5]; for (int i = 0; i < 5; i++) { scanf("%d", &a[i]); }

✅ This avoids writing multiple scanf() statements.


6️⃣ Two-Dimensional Array

A 2D array stores data in rows and columns (like a table or matrix).

Declaration:

int matrix[3][3];

Representation:

012
0abc
1def
2ghi

Example: Matrix Input

int mat[2][2]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { scanf("%d", &mat[i][j]); } }

✅ Uses nested loops.


7️⃣ Practical Use: Storing Marks

int marks[5] = {60, 70, 80, 90, 50};

Common uses:

  • Student marks

  • Salaries

  • Scores

  • Sensor readings


🧩 Step-by-Step Program Examples (Exam-Focused)

✅ Program 1: Find Largest and Smallest Element in an Array

#include <stdio.h> int main() { int arr[5] = {10, 45, 2, 90, 20}; int max = arr[0], min = arr[0]; for (int i = 1; i < 5; i++) { if (arr[i] > max) max = arr[i]; if (arr[i] < min) min = arr[i]; } printf("Largest = %d\n", max); printf("Smallest = %d\n", min); return 0; }

✅ 1D vs 2D Array Declaration

int a[5]; // 1D array int b[3][3]; // 2D array

📝 Important Exam Questions

  1. Define array. Write its advantages

  2. Explain 1D array with example

  3. Explain 2D array with example

  4. Write a program to find largest element in an array

  5. Write a program to find sum of array elements

  6. Difference between 1D and 2D arrays

  7. Why does array indexing start from 0?


🔍 Difference Between 1D and 2D Arrays

One-DimensionalTwo-Dimensional
Single rowRows and columns
Uses one subscriptUses two subscripts
Simple storageTable/matrix storage
Example: marksExample: matrix

🎯 Exam Tips / Key Points

  • Array index starts from 0

  • Out-of-range index causes error

  • Arrays store same data type only

  • Use loops to process arrays

  • Practice array programs for exams

  • Use arr[0] to initialize max/min


✅ Short Summary

In Unit 7, you learned:

  • What arrays are and why they are used

  • 1D and 2D arrays

  • Accessing and processing arrays using loops

  • Important array-based programs for exams

Arrays are a core concept in C and are heavily used in functions, pointers, and file handling.

Netra Koirala

Netra Koirala

Computer Science Educator

Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.

Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.

Featured Post

Grade 10 Computer Science: Specification Grid & Model Questions

Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...

Followers