0% found this document useful (0 votes)
29 views7 pages

Arrays in C - 7th Nov

Arrays in C are defined as finite ordered collections of homogeneous data stored in contiguous memory locations. Arrays allow storing lists of related data such as employee names, customer information, or test scores. Arrays have characteristics like all elements sharing a common name, being distinguished by index numbers, and elements being stored in continuous memory locations. Arrays are declared with a type, name, and size and individual elements can be accessed using subscript notation. Common operations on arrays include initialization, accessing or modifying specific elements, and iterating over the entire array with for loops.

Uploaded by

arunkumars0121
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)
29 views7 pages

Arrays in C - 7th Nov

Arrays in C are defined as finite ordered collections of homogeneous data stored in contiguous memory locations. Arrays allow storing lists of related data such as employee names, customer information, or test scores. Arrays have characteristics like all elements sharing a common name, being distinguished by index numbers, and elements being stored in continuous memory locations. Arrays are declared with a type, name, and size and individual elements can be accessed using subscript notation. Common operations on arrays include initialization, accessing or modifying specific elements, and iterating over the entire array with for loops.

Uploaded by

arunkumars0121
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
You are on page 1/ 7

2

Arrays in C
definition

In C language, arrays are referred to as structured data types. An array is defined


as finite ordered collection of homogenous data, stored in contiguous memory Arrays and for loops go hand-in-hand. Many programs contain for loops whose job is to
locations. perform some operation on every element in an array. Here are a few examples of typical
operations on an array ‘a’ of length N:
 finite means data range must be defined.
 ordered means data must be stored in continuous memory addresses. ARRAY INITIALIZATION
 homogenous means data must be of similar data type.
1.Compile time Array initialization in C

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

2.Runtime Array initialization in C ARRAY PROGRAMS

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

To access elements at alternate index, @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

for(int i = 0; i < 10; i+=2)

printf("%d", Arr[i]);

Write a C program to sum the elements in the one dimensional array


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ #include<stdio.h>
void main()
. {
int a[10], n, i, sum;
printf("Enter the Number of Elements\n");
scanf("%d", &n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sum=0;
for(i=0;i<n;i++)
{
5
6

sum=sum+a[i]; #define MAXROW 4


} #define MAXCOL 5
printf("The Sum is: %d", sum); int mat[MAXROW][MAXCOL];
}

output
Enter the Number of Elements
5
1
2
3
4
5
The Sum is: 15

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

TWO DIMENSIONAL ARRAY initialization of 2D arrays – compile time initialisation


Two Dimensional Array Syntax:

 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

Runtime initialization of a two dimensional Array


examples:
1.int Employees[4][3] = { 10, 20, 30, 15, 25, 35, 22, 44, 66, 33, 55, 77 }; Now let's see how we can initialize a two dimensional array at
runtime.
The first three elements will be 1st row, the second three elements will be 2nd row, the next 3
#include<stdio.h>
elements will be 3rd row, and the last 3 elements will be 4th row. Here we divided them into
void main()
3 because our column size = 3.
{
The above C two dimensional array statement can also be written as int arr[3][4];
int i, j, k;
2.int Employees[4][3] = { printf("Enter array elements:\n");
{10, 20, 30}, for(i = 0; i < 3;i++)
{15, 25, 35}, {
{22, 44, 66}, for(j = 0; j < 4; j++)
{33, 55, 77} {
}; scanf("%d", &arr[i][j]);
Here, We surrounded each row with curly braces ({}). It is always good practice to use the }
curly braces to separate the rows. }
for(i = 0; i < 3; i++)
3.int Employees[ ][ ] = { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} }; {
for(j = 0; j < 4; j++)
Here, We haven’t mentioned the row size and column size. However, the compiler is {
intelligent enough to calculate the size by checking the number of elements inside the row printf("%d", arr[i][j]);
and column. }
}
We can also write this two dimensional array in c as }

4.int Employees[ ][3] = { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} }; output

Enter array elements:


5.int Employees[2][3] = { {4}, {6, 7} }; 1 2 3 4 5 6 7 8 9 10 11 12
Here, we declared Employees array with row size =2 and column size = 3, but we only 1 2 3 4 5 6 7 8 9 10 11 12
assigned 1 column in the 1st row and 2 columns in the 2nd row. In these situations, the
remaining values will assign to default values (0 in this case).

The above array will be:

Employees[2][3] = { {4, 0, 0}, {6, 7, 0} };


//It means
9 10

ROW / COLUMN MAJOR FORMATS


row major = [base address + (i*n + j) * size of the data_type].
The elements of an array can be stored in column-major layout or row-major layout. For an array stored in column- Column major = [base address + (j*m + i) * size of the data_type].
major layout, the elements of the columns are contiguous in memory. In row-major layout, the elements of the rows are
contiguous.
Multi-Dimensional Array in C Language:
An array of arrays is called a multi-dimensional array. In simple words, an array created with
more than one dimension (size) is called a multi-dimensional array. The multi-dimensional
array can be of a two-dimensional array or three-dimensional array or four-dimensional array
or more.

Syntax: type name[size1][size2]…[sizeN];


Example: int a[3][3][3];

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

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

85023 enter 6 numbers :


6
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 7
Example - The following program prints the sum of elements of an array. 8
3
#include<stdio.h> 4
int main() 1
{ in reverse order:143876
intarr[5], i, s = 0;
for(i = 0; i< 5; i++) //write a C program to print fibanocci series
{ #include <stdio.h>
printf("Enter a[%d]: ", i); #include <stdlib.h>
scanf("%d", &arr[i]); void main()
} {
for(i = 0; i< 5; i++) int i,fibonacci[10];
{ fibonacci[0]=0;
s += arr[i]; fibonacci[1]=1;
} for(i=2;i<10;i++)
printf("\nSum of elements = %d ", s); fibonacci[i]=fibonacci[i-2]+fibonacci[i-1];
return 0; for(i=0;i<20;i++)
} printf("Fibonacci[%d]=%d\n", i, fibonacci[i]);
}
output
output
Enter a[0]: 22
Enter a[1]: 33 Fibonacci[0]=0
Enter a[2]: 56 Fibonacci[1]=1
Enter a[3]: 73 Fibonacci[2]=1
Enter a[4]: 23 Fibonacci[3]=2
Fibonacci[4]=3
Sum of elements = 207 Fibonacci[5]=5
Fibonacci[6]=8
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Fibonacci[7]=13
Fibonacci[8]=21
Write a C Program to reverse a series of numbers Fibonacci[9]=34
#include<stdio.h>
#define N 6
void main() @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
{
int a[N], i; Write a C program to sum the elements in the one dimensional array
printf("enter %d numbers :\n" , N); #include<stdio.h>
for( i =0;i<N;i++) void main()
scanf("%d", &a[i]); {
printf(" in reverse order:"); int a[10], n, i, sum;
for (i=N-1; i>=0; i--) printf("Enter the Number of Elements\n");
printf("%d", a[i]); scanf("%d", &n);
} for(i=0;i<n;i++)
{
Output scanf("%d",&a[i]);
}
13

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
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

You might also like