📚 Arrays in C Programming
One-Dimensional Arrays, Indexing & Practical Examples
Learn How to Store and Manage Multiple Values Efficiently 🚀
🚀 Introduction
Suppose you want to store the marks of 5 students. Without an array, you might create five different variables:
int mark1 = 75; int mark2 = 82; int mark3 = 68; int mark4 = 91; int mark5 = 77;
This works, but it becomes difficult when you have 100 or 1,000 values. An array solves this problem by allowing us to store multiple values of the same data type under one variable name.
💡 Simple Definition
An array is a collection of elements of the same data type stored under one variable name.
📌 Declaring an Array
The basic syntax is:
data_type array_name[size];
Example:
int marks[5];
This creates an integer array capable of storing 5 values.
🔢 Understanding Array Indexing
C arrays use zero-based indexing. That means the first element has index 0, not 1.
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Value | 75 | 82 | 68 | 91 | 77 |
Therefore:
marks[0] = 75 marks[1] = 82 marks[2] = 68
📝 Initializing an Array
You can declare and initialize an array at the same time.
int marks[5] = {75, 82, 68, 91, 77};
The values are automatically stored in consecutive array positions.
🔍 Accessing Array Elements
To access a particular element, use its index.
#include <stdio.h>
int main()
{
int marks[5] = {75, 82, 68, 91, 77};
printf("%d\n", marks[0]);
printf("%d\n", marks[2]);
printf("%d\n", marks[4]);
return 0;
}
Output:
68
77
🔁 Using a Loop with an Array
Loops and arrays work extremely well together. A loop can visit every element without writing separate statements for each one.
#include <stdio.h>
int main()
{
int marks[5] = {75, 82, 68, 91, 77};
int i;
for(i = 0; i < 5; i++)
{
printf("%d\n", marks[i]);
}
return 0;
}
The loop starts from index 0 and continues up to index 4.
⌨️ Taking Array Input from the User
We can use scanf() inside a loop to fill an array.
#include <stdio.h>
int main()
{
int numbers[5];
int i;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
printf("You entered:\n");
for(i = 0; i < 5; i++)
{
printf("%d\n", numbers[i]);
}
return 0;
}
➕ Practical Example: Find the Sum of Array Elements
#include <stdio.h>
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
int i;
int sum = 0;
for(i = 0; i < 5; i++)
{
sum = sum + numbers[i];
}
printf("Sum = %d", sum);
return 0;
}
Output:
📊 Practical Example: Find the Average
#include <stdio.h>
int main()
{
int marks[5] = {70, 80, 90, 75, 85};
int i;
int sum = 0;
float average;
for(i = 0; i < 5; i++)
{
sum = sum + marks[i];
}
average = (float)sum / 5;
printf("Average = %.2f", average);
return 0;
}
⚠️ Important Rules of Arrays
- All elements normally have the same data type.
- Array indexing starts from 0.
- The last index is size - 1.
- Do not access an index outside the array's valid range.
- Arrays make it easier to process large collections of data.
⚖️ Variable vs Array
| Variable | Array |
|---|---|
| Usually stores one value | Stores multiple values |
| Example: int age | Example: int ages[10] |
| Simple data storage | Efficient collection processing |
🌍 Where Are Arrays Used?
- 📚 Student marks and records
- 🛒 Product lists
- 📈 Stock market data
- 🎮 Game scores
- 🌡️ Temperature readings
- 🔢 Mathematical calculations
🚀 Practice Programs
- Store and display 10 numbers.
- Find the largest element in an array.
- Find the smallest element in an array.
- Calculate the sum and average of elements.
- Count even and odd numbers.
- Search for a particular number in an array.
- Reverse an array.
🎯 Conclusion
Arrays are one of the most important data structures for beginners. They allow programmers to store and process multiple values efficiently.
Once you understand arrays and loops, you are ready to solve much more interesting programming problems.
No comments:
Post a Comment