Arrays in C - 7th Nov
Arrays in C - 7th Nov
Arrays in C
definition
Compile time initialization of array means we provide the value for the array in the code,
some uses of Arrays in C when we create the array,
to store list of Employee or Student names
list of customers and their telephone numbers data-type array-name[size] = { list of values };
table of daily rainfall data
to store marks of students
examples
Characteristics of Arrays int Employees[5] = {1, 2, 3, 4, 5};
All elements in the arrays share a common name .
Elements distinguished by index number . int Employees[ ] = {1, 2, 3, 4, 5}; // array size not mentioned. Compiler calculates the
Index (or) element number of an array plays vital role for calling each element size of an array by checking the number of elements.
Specific array elements can be modified .
Value of array element can be assigned to variables. int Employees[5] = {1, 4, 5}; // Employees array with size 5, but we only assigned 3
Array elements stored in continuous memory locations //variables. remaining values assigned to 0.
ARRAY DECLARATION int marks[4] = { 67, 87, 56, 77, 59 }; // Compile time error as size=4 elements
Single-Dimension Arrays
The general form for declaring a single-dimension array is char name[]= {‘J’, ‘O’, ‘H’, ‘N’, ‘\0’}; //same as char name[]=”JOHN”;
type var_name[size];
int i, Employees[100];
int a[10]; // array ‘a’ has 10 elements of type int, we would write for (i =0; i< 100 ; i++)
double balance[100]; // a 100-element array called balance of type double {
balance[3] = 12.23; //assigns element number 3 in balance array the value 12.23 Employees[i] = i*2;
char name[10]; // name is the character array variable that can hold a maximum }
of 10 characters
Array subscripting
To access a particular element of an array, we write the array name followed by an integer
value in square brackets called indexing or subscripting the array. Array elements are
always numbered starting from 0, so the elements of an array of length n are indexed from 0
to n-1. For example, if a is the array with 10 elements, they are designated by a[0], a[1],
a[2],...,a[9] as shown in the following figure
3 4
An array can also be initialized at runtime using scanf() function. This approach is usually //write a C program to print fibanocci series
used for initializing large arrays, or to initialize arrays with user specified values. To input #include <stdio.h>
elements in an array, we can use a for loop or insert elements at a specific index. #include <stdlib.h>
void main()
for(int i = 0; i < 10; i++) {
scanf("%d", &Arr[i]); int i,fibonacci[10];
fibonacci[0]=0;
fibonacci[1]=1;
Accessing Array elements in C for(i=2;i<10;i++)
fibonacci[i]=fibonacci[i-2]+fibonacci[i-1];
We already know how to access array elements. Yes, it is using the indexes. So let's see a few for(i=0;i<20;i++)
examples where we will print entire arrays or some specific values, etc. printf("Fibonacci[%d]=%d\n", i, fibonacci[i]);
}
To print all elements,
output
for(int i = 0; i < 10; i++)
Fibonacci[0]=0
printf("%d", Arr[i]); Fibonacci[1]=1
Fibonacci[2]=1
Fibonacci[3]=2
Fibonacci[4]=3
To access and print elements at specified index, Fibonacci[5]=5
Fibonacci[6]=8
printf("%d", Arr[0]); //prints first element of the array Fibonacci[7]=13
Fibonacci[8]=21
printf("%d", Arr[5]); //prints sixth element of the array
Fibonacci[9]=34
printf("%d", Arr[i]);
output
Enter the Number of Elements
5
1
2
3
4
5
The Sum is: 15
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The Two Dimensional Array in C language is nothing but an Array of Arrays. If the datatype array_name[ROW][COL];
data is linear, we can use the One Dimensional Array. However, to work with multi-
level data, we have to use the Multi-Dimensional Array. The total number of elements in a 2-D array is ROW*COL. Let’s take an example.
Two dimensional array is popularly known as tables or matrix and can be easily
visualized as having rows and columns. Matrix can also be thought of as arrays of int arr[2][3];
arrays.
This array can store 2*3=6 elements. You can visualize this 2-D array as a matrix of 2 rows
Declaration of Two Dimensional Array in C and 3 columns.
The basic syntax or, the declaration of two dimensional array in C Programming is as shown
below:
Data_Type Array_Name[Row_Size][Column_Size]
Data_type: This will decide the type of elements will accept by two dimensional
array in C. For example, If we want to store integer values then we declare the Data
Type as int, If we want to store Float values then we declare the Data Type as float etc
Array_Name: This is the name you want to give it to this C two dimensional array.
Row_Size: Number of Row elements an array can store.
Column_Size: Number of Column elements an array can store.
For Example,
int Employees[4][3];
For e.g. the following declaration creates a matrix of 4 rows and 5 columns.
int mat[4][5];
or
7 8
The individual elements of the above array can be accessed by using two subscript instead of Employees[0][0] = 4
one. Employees[0][1] = 0
Employees[0][2] = 0
arr[0][0] - refers to the first element Employees[1][0] = 6
arr[0][1] - refers to the second element Employees[1][1] = 7
arr[0][2] - refers to the third element Employees[1][2] = 0
arr[1][0] - refers to the fourth element
arr[1][1] - refers to the fifth element @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
arr[1][2] - refers to the sixth element
4.int Employees[ ][3] = { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} }; output
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ARRAY PROGRAMS
Example : The following program uses for loop to take input and print elements of a 1-
D array.
#include<stdio.h>
int main()
{
intarr[5], i;
for(i = 0; i< 5; i++)
{
printf("Enter a[%d]: ", i);
scanf("%d", &arr[i]);
}
printf("\n Printing elements of the array: \n\n");
for(i = 0; i< 5; i++)
{
printf("%d ", arr[i]);
}
// signal to operating system program ran fine
return 0;
}
Output
Enter a[0]: 8
Enter a[1]: 5
Enter a[2]: 0
Both Row major and column major order takes same amount of time. Since both have same Enter a[3]: 2
number of operations. i.e. Lets take A[m][n]. Then the corresponding row major and column Enter a[4]: 3
major formulas are :
Printing elements of the array:
11 12
sum=0;
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("The Sum is: %d", sum);
}
output
Enter the Number of Elements
5
1
2
3
4
5
The Sum is: 15
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@