C – Arrays

After reading this arrays topic, you will understand its theory and examples, you will know the types of arrays also you will able to implement one-dimensional arrays.

Ordinary variables are capable to store only one value at a time, many times we need to store many values and declaring many variables for each value is not logically right, so construct one variable capable of storing many values is the logic of an array.

An array is a collection of memory location under a single variable or we can say when one variable is used to hold the finite number of elements of the same type then this variable called an array.

Types of Arrays:

1 One-dimensional array.
2 Multidimensional array.

One-dimensional array

An array with the finite number of same type elements where each element is stored in consecutive memory location referred by single index and this array called as one-dimensional array.

Declaration of an array

The general form of array declaration is:

data_type array_name[size];

An array should be declared as any basic data type (i.e. integer, float, etc.) with the name of array write after data type and size of array written within the square brackets (i.e.[ ]).
For example :

int ra[5];

Show that

  • ra is the name of an array.
  • int represent data type is integer.
  • The size of an array is 5.

Initialization of an array

Like any other variable array can be initialized during declaration or we can say in an easy way that we can write/store elements into an array at the time of declaration of the array.
The general form of initialization of an array is:

data_type array_name[size]={element1,element2,….};

For example,

int ra[5]={2,10,23,79,100};

Show that

  • ra is the name of an array.
  • int represent data type is integer.
  • The size of array is 5
  • The elements of array are 2,10,23,79 and 100.

Program (1): To find the average of three numbers using an array. The three numbers are 12, 34 and 10.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int i,j;
int a[3]={12,34,10};
float avg,sum=0;
//Calculate average of three numbers and print result
for(i=0;i<3;i++)
{
sum=sum+a[i];
}
avg=sum/3;
printf("Result of avg is
getch();
}

Output (1)

Result of avg is 18.666666

Array memory/storage representation

Consider the array initialized as

int ra[5]={5,23,22,13,10};

When array int a[5] is declared, the compiler allocates memory for 1st element at address 100 (consider) having data 5 and index number 0. For the last element allocate memory at address 108 (consider) having data 10 and index number 4 as shown below. The address of consecutive memory location increase by 2 bytes for an integer data type.

Writing/inserting the elements into an array

One method of writing elements into an array during initialization of array which we already discussed earlier and another way to write elements into an array at runtime by using index along with any looping statement.
Let us assume, to insert 50 elements into an array so better solution by using loop statement as

for( i = 0; i < 50; ++i)
{
scanf(â€
}

The for loop runs and accept data from user up to 50 times.

Reading an element from an array:

Assume that 50 elements stored in an array and we want to read or access them so the better solution is,

for( i = 0; i < 50; ++i)
{
printf (â€
}

Here, the for loop runs and print data from memory up to 50 times.

Example:
Program (1): To accept 5 elements from the user and read/print them using an array.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a[5],i,j,t;
//accepting values into array
for(i=0;i<5;i++)
{
printf("Enter
scanf(
}
//Reading elements from array
printf("nThe elements aren");
for(i=0;i<5;i++)
{
printf(
}
getch();
}

Output (1)

Enter 1 element :- 34
Enter 2 element :- 2
Enter 3 element :- 34
Enter 4 element :- 3
Enter 5 element :- 67
The elements are
34      2       34      3       67

Example:
Program (1): To find the sum of 3 numbers entered by the user using an array.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a[3],i,j,sum=0;
//accepting values into array
for(i=0;i<3;i++)
{
printf("Enter
scanf(
}
//Calculate sum of three numbers and print result
for(i=0;i<3;i++)
{
sum=sum+a[i];
}
printf("Result of sum is
getch();
}

Output (1)

Enter 1 element :- 2
Enter 2 element :- 4
Enter 3 element :- 7
Result of sum is 13

Program (1): To find the largest element and its position in an array.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a[5],i,j,lar;
//accepting values into array
for(i=0;i<5;i++)
{
printf("Enter
scanf(
}
//find largest element and its position
lar=a[0];
for(i=0;i<5;i++)
{
if(a[i]>lar)
{
lar=a[i];
j=i+1;
}
}
printf("largest element
printf("and position is
getch();
}

Output (1)

Enter 1 element :- 3
Enter 2 element :- 54
Enter 3 element :- 2
Enter 4 element :- 44
Enter 5 element :- 5
largest element 54
and position is 2

Example:
Program (1): To sort an array in ascending order.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a[5],i,j,t;
clrscr();
//accepting values into array
for(i=0;i<5;i++)
{
printf("Enter
scanf(
}
//Sorting process begin
for(i=0;i<5;i++)
{
for(j=0;j<5-i-1;j++)
{
if(a[j]>a[j+1])
{
//swapping procedure
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
//Printing sorted list
printf("nnAfter sorting list containn");
for(i=0;i<5;i++)
{
printf(
}
getch();
}

Output (1)

Enter 1 element :- 4
Enter 2 element :- 5
Enter 3 element :- 78
Enter 4 element :- 12
Enter 5 element :- 34
After sorting list contain
4       5       12      34      78

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *