Arrays: Store A Single Value at A Time Store Roll No. of 100 Students?
Arrays: Store A Single Value at A Time Store Roll No. of 100 Students?
The normal variable allows us to store a single value at a time, what if we want to store roll
no. of 100 students?
For this task, we have to declare 100 variables, then assign values to each of them.
Arrays are the derived data type in C programming language which can store the
primitive type of data such as int, char, double, float, etc.
The array is the simplest data structure where each data element can be
randomly accessed by using its index number.
Syntax: datatype array_name[size];
datatype: It denotes the type of the elements in the array.
int age= 20; // Normal variable which stores only 1 data element.
Arrays
Def:An array is defined as the collection of similar type of data items stored at
contiguous memory locations.
int marks[9]; //Array variable which stores 9 data elements of same integer data items
float salary[5]; //Array variable which stores 5 data elements of same float data items
char name[30]; //Array variable which stores 30 data elements of same characters data items
--------------------Note----------------
Memory Sizes: For Normal Variable int – 2 bytes, float – 4 bytes, char – 1
byte
• marks[9] has 9 integer data elements only so 18(9 x 2) bytes of Memory
size it holds.
• X[5] has 5 float data elements only so 20(5 x 4) bytes of Memory size it
holds.
• name[30] has 30 character elements only so 30(30 x 1) bytes of
Memory size it holds.
• sum is one normal variable of integer data element only so 2 bytes of
Memory size it holds.
Declarations and Initialization of an Array Variables:
// Array declaration by initializing elements
1.int x[3]={20,30,5};
2. int arr[] = { 10, 20, 30, 40 };
// Compiler creates an array of size 4.
// above is same as "int arr [4] = {10, 20, 30, 40}“
3.int arr[7] = { 10, 20, 30, 40 };
// Compiler creates an array of size 7, initializes first
// 4 elements as specified by user and rest 3 elements as 0.
//above 3. is same as "int arr[] = {10, 20, 30, 40, 0, 0,0 };"
4. int arr[5];
arr[0] = 5;
arr[2] = -10; 5 0 -10 5 0
arr[3] = arr[0]; 0 1 2 3 4
5. int size=10;
int marks[size];
Declarations and Initialization of an Array Variables:
// Array declaration and initializing /accepting through keyboard
6. int number[5], i=0;
printf(“Enter 5 data elements in an array\n”);
while( i<5)
{
scanf(“%d”, &number[i]); // at i = 0, number[0] = keyboard value
number
num
Data elements 25 -55 79 345 0
Index values 0 1 2 3 4
Simple C Program on Arrays
#include <stdio.h>
void main()
{
// Array declaration by initializing it
// with more elements than specified size.
int m1[] = { 75,80,90 };
int m2[5] = { 60,70,80,90,75};
int x=20;
printf(“%d”, x);
printf(“%d”, m1[2]);//takes index value of 2 and prints 90
printf(“%d”, m1[0]); //takes index value of 0 and prints 75
printf(“%d”, m2[2]); //takes index value of 2 and prints 80
} m1
Data elements 75 80 90
Index values 0 1 2
m2
Data elements 60 70 80 90 75
Index values 0 1 2 3 4
Printing array values using LOOPs
#include <stdio.h>
void main()
{
int num[5];//just Declared an Array
int x=0;
clrscr();
num[0]=25;//Initializing the values into an array
num[1]= -55;
num[2]= 79;
num[3] = 345;
Index values 0 1 2 3 4
Simple C Program on Arrays
#include <stdio.h>
void main()
{
int num[5];//just Declared an Array
int i=0;
int x=0;
clrscr();
printf(“Enter 5 elements in to an array \n”);
Index values 0 1 2 3 4
Arrays
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the
elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we
need a few lines of code only.
4) Random Access: We can access any element randomly using the
array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration
of the array, we can't exceed the limit. So, it doesn't grow the size
dynamically like LinkedList which we will learn later.
Arrays are of two types
1. single dimensional array(1D)
2. two dimensional array(2D)
Two Dimensional Array in C
The two-dimensional array can be defined as an array of arrays.
The 2D array is organized as matrices which can be represented
as the collection of rows and columns.
data_type array_name[rows][columns];
int X[3][4];
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};