0% found this document useful (0 votes)
5 views8 pages

Array Notes

An array in C is a collection of variables of the same data type stored under a single name, allowing for easier management and access to multiple values. Arrays can be declared in one or multiple dimensions, and values can be stored and accessed using indices. The document provides examples of declaring, initializing, and manipulating arrays, along with sample programs demonstrating their usage.

Uploaded by

trishnadigital32
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Array Notes

An array in C is a collection of variables of the same data type stored under a single name, allowing for easier management and access to multiple values. Arrays can be declared in one or multiple dimensions, and values can be stored and accessed using indices. The document provides examples of declaring, initializing, and manipulating arrays, along with sample programs demonstrating their usage.

Uploaded by

trishnadigital32
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is an Array in C

Array is a group of variables of the same data type stored together under one name.
Example:
If you want to store 5 student marks:
• Without array → 5 separate variables
int m1=80, m2=75, m3=73, m4=86, m5=64;
• With array → 1 variable store all

int marks[5];

Why We Use Array

Without Array With Array

Hard to store many values Easy to store many

Hard to manage Easy to manage

Difficult to loop Loop makes it simple

How to Declare an Array


Syntax:
dataType arrayName[size];

Example:
int numbers[10]; // array of 10 integers
float price[20]; // array of 20 floats
char name[50]; // array of 50 characters

How to Store Values in Array


Method 1: One by one
int arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
Method 2: Direct Initialization
1. With Specific Values:
int arr[5] = {10, 20, 30, 40, 50};

Visual Representation

Memory:

1000 1004 1008 1012 1016

| | | | |

10 20 30 40 50

2. Partial Initialization:
If fewer values are provided, the rest are set to 0 by default.

int numbers [5] = {1, 2}; // Initializes to {1, 2, 0, 0, 0}

3. Omitting Size:
The size can be omitted when initializing, and it will be determined
by the number of values provided.

int numbers [] = {1, 2, 3, 4, 5};

When array size CANNOT be omitted

You must write size if there is no initialization.

int arr[]; // ERROR

How to Print Array Values


Use index.
Index starts from 0.
Element Index
10 0
20 1
30 2
40 3
50 4
Example:

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


How to print all the array value:
#include <stdio.h>
int main() {
int num[5] = {10,20,30,40,50};
int i;
printf("You Entered:\n");
for(i = 0; i < 5; i++) {
printf("%d ", num[i]);
}
return 0;
}

What this program does:


• Takes 5 inputs
• Stores in array
• Prints them using loop

Types of Arrays
1. One-Dimensional Array:
Like a single row
int arr[5];

2. Two-Dimensional Array (Matrix)/ Multi-Dimensional Array:


Rows and Columns like table
Syntax : data_type array_name[rows][columns];

int arr[3][4]; // 3 rows, 4 columns

Example:
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

Important Points
Point Meaning
Array index starts from 0 arr[0] is 1st element
Same data type only all elements must be int/float/char…
Stored in continuous memory fast access
Program – 1
Store and print 5 numbers (Number should given by user)
#include <stdio.h>

int main() {
int num[5];
int i;

printf("Enter 5 numbers:\n");

for(i = 0; i < 5; i++) {


scanf("%d", &num[i]);
}

printf("You entered:\n");
for(i = 0; i < 5; i++) {
printf("%d ", num[i]);
}

return 0;
}

Step-by-Step Explanation
Step 1: Program starts
Array created in memory.
num[ ] = [ _ , _ , _ , _ , _ ]

Step 2: User inputs 5 numbers


Suppose user enters:
3
6
9
12
15

After input, the array stores:

Index 0 1 2 3 4
Value 3 6 9 12 15
Memory now:
num[ ] = [ 3 , 6 , 9 , 12 , 15 ]

Step 3: Print values in same order


Second loop: for(i = 0; i < 5; i++)
Loop i num[i] Output printed
1st 0 3 3
2nd 1 6 36
3rd 2 9 369
4th 3 12 3 6 9 12
5th 4 15 3 6 9 12 15

Step 4: Final Output


You entered:
3 6 9 12 15

Program – 2
Print the array in reverse order

#include <stdio.h>

int main() {
int arr[5];
int i;

printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++) {
scanf("%d", &arr[i]); If input → 10 20 30 40 50
} Output → 50 40 30 20 10

printf("Reverse order:\n");
for(i = 4; i >= 0; i--) {
printf("%d ", arr[i]);
}

return 0;
}
Step-by-Step Explanation
Step 1: Program starts
Array created in memory.
arr[ ] = [ _ , _ , _ , _ , _ ]

Step 2: User inputs 5 numbers


Suppose user enters:
5
10
15
20
25
After input, the array stores:

Index 0 1 2 3 4
Value 5 10 15 20 25

Memory now:
arr[ ] = [ 5 , 10 , 15 , 20 , 25 ]

Step 3: Print in reverse


Second loop: for(i = 4; i >= 0; i--)
Loop i num[i] Output printed
1st 4 25 25
2nd 3 20 25 20
3rd 2 15 25 20 15
4th 1 10 25 20 15 10
5th 0 5 25 20 15 10 5

Step 4: Final Output


Reverse order:
25 20 15 10 5
Program – 3
Sum of all elements
#include <stdio.h>

int main() {
int a[5], i, sum = 0;

printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++) {
scanf("%d", &a[i]);
}

for(i = 0; i < 5; i++) {


sum = sum + a[i];
}

printf("Sum = %d", sum);

return 0;
}

Step-by-Step Explanation
Step 1: Program starts

• a[5] → empty array of 5 integers


• sum → 0

a[ ] = [ _ , _ , _ , _ , _ ]

sum = 0

Step 2: User inputs 5 numbers


Suppose user enters:
10
20
30
40
50

After scanning, array becomes:


a[ ] = [ 10 , 20 , 30 , 40 , 50 ]

sum = 0
Step 3: Start adding numbers (second loop)
Loop runs 5 times:

Loop i a[i] sum before Operation sum after

1st 0 10 0 0 + 10 10

2nd 1 20 10 10 + 20 30

3rd 2 30 30 30 + 30 60

4th 3 40 60 60 + 40 100

5th 4 50 100 100 + 50 150

Final: sum = 150

Step 4: Output
Sum = 150 (show it on screen)

You might also like