« Previous                                                                  Next Chapter »

One Dimensional Arrays

An array is a collection of variables of similar data type stored under one name. Whenever user want to work with more variables of same data type it is better to work with an Array. 

If we are working with individual variables the fallowing are the problems.

  • The editor will be filled with variables if they are more in number.
  • It is tough to refer a variable immediately as and when we required.
  • Taking input and printing output is tough. As we have to used more control or format strings.
  • For all the variables the memory will be allotted randomly.

If we use array the fallowing are the advantages.

  • We can declare any number of variables with in very less space.
  • It is easy to refer a variable immediately as and when we require.
  • Taking input and printing output is easy by using one control or format string and with the help of loop we can refer all the variables.
  • For all the variables the memory will be allocated sequentially (or) contiguously.

We have three types of arrays in C language. They are

  1. Single Dimensional Array
  2. Double Dimensional Array
  3. Multi Dimensional Array

Syntax: Datatype array_ name[size];

Ex: int  A[5];

For the above declaration with the name automatically 10 bytes of memory will be allocated. Immediately it will be divided into 5 equal parts of each two bytes. To refer each part an identification number will be provided which can be called as index. The index will start with 0 and ends with 4

                                    0      1      2      3     4

 

 

 

 

 

                                               A 10 bytes

To refer a required cell in the array, the following syntax we need to use

Syntax:  array name[index];

Ex: To refer the 1st variable the index is 0 

       To refer the 4th variable the index is 3 etc

Note:

The number of bytes of memory that has to be allotted for the array is nothing but  the product of size, number of  bytes for each variable for that data type. The number of partition’s in the array is based on the size of the array. To refer each variable identification number will be provided which can be called as index number. The index will start with 0 and ends with (Array Size - 1). To refer the 1st variable the index is 0 to refer the nth variable the index is n - 1.


More Examples
  /* program to initialize the array and print it */
#include<stdio.h>
#include<conio.h>
main()
{
int i,a[5]={10,20,30,40,50};
clrscr();
for(i=0;i<5;i++)
printf("a[%d]=%d\n",i,a[i]);
}

/* program to read values into the array and print them */
#include<stdio.h>
#include<conio.h>
main()
{
int a[5],i;
clrscr();
for(i=0;i<5;i++)
{
printf("enter the element of a[%d]",i);
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
printf("a[%d]=%d\n",i,a[i]);
}

/* program to read values into n variables in the array */
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],i,n;
clrscr();
printf("enter the no of variables to be used");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the values of a[%d]",i);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
printf("a[%d]=%d\n",i,a[i]);
}

/* program to find sum,sume,sumo of the values in the array */
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],i,n,sum=0,sume=0,sumo=0;
clrscr();
printf("enter the no of variables to be used");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the values of a[%d]",i);
scanf("%d",&a[i]);
sum=sum+a[i];
if(a[i]%2==0)
sume=sume+a[i];
else
sumo=sumo+a[i];
}
for(i=0;i<n;i++)
printf("a[%d]=%d\n",i,a[i]);
printf("the sum of the all the values is%d\n",sum);
printf("the sum of the all even values is%d\n",sume);
printf("the sum of the all odd values is%d\n",sumo);
}

/* program to find the sum of first, last and middle elements of the array */
#include<stdio.h>
#include<conio.h>
main()
{
 int a[10],i,n;
 clrscr();
 printf("enter the no of variables to be used");
 scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the value of a[%d]",i);
scanf("%d",&a[i]);
}
printf("the sum is %d\n",a[0]+a[n-1]+a[n/2]);
}

/* program to find minimum element in the array */
#include<stdio.h>
#include<conio.h>
main()
{
 int a[10],i,n,min;
 clrscr();
 printf("enter the no of variables to be used");
 scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the value of a[%d]",i);
scanf("%d",&a[i]);
}
min=a[0];
for(i=1;i<n;i++)
{
if(a[i]<min)
{
min=a[i];
}
}
printf("the minimum value is %d\n",min);
}

/* program to find the maximum element in the array */
#include<stdio.h>
#include<conio.h>
main()
{
 int a[10],i,n,max;
 clrscr();
 printf("enter the no of variables to be used");
 scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the value of a[%d]",i);
scanf("%d",&a[i]);
}
max=a[0];
for(i=1;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
printf("the minimum value is %d\n",max);
}

/* program to delete an element in the array */
#include<stdio.h>
#include<conio.h>
main()
{
 int a[10],i,n,pos;
 clrscr();
 printf("enter the no of variables to be used");
 scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the value of a[%d]",i);
scanf("%d",&a[i]);
}
printf("enter the position to be deleted");
scanf("%d",&pos);
for(i=pos;i<n;i++)
{
a[i]=a[i+1];
}
n--;
for(i=0;i<n;i++)
{
printf("a[%d]=%d\n",i,a[i]);
}
}

/* program to insert element into the array in a given position */
#include<stdio.h>
#include<conio.h>
main()
{
 int a[10],i,n,pos;
 clrscr();
 printf("enter the no of variables to be used");
 scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the value of a[%d]",i);
scanf("%d",&a[i]);
}
printf("enter the position to be inserted");
scanf("%d",&pos);
for(i=n;i>pos;i--)
{
a[i]=a[i-1];
}
printf("enter the value to be inserted");
scanf("%d",&a[pos]);
n++;
for(i=0;i<n;i++)
{
printf("a[%d]=%d\n",i,a[i]);
}
}

/* program to find whether the element is present or not in the array */
#include<stdio.h>
#include<conio.h>
main()
{
 int a[10],i,n,se,found=0;
 clrscr();
 printf("enter the no of variables to be used");
 scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the value of a[%d]",i);
scanf("%d",&a[i]);
}
printf("enter the searching element");
scanf("%d",&se);
for(i=0;i<n;i++)
{
if(a[i]==se)
{
printf("the element is found");
found=1;
break;
}
}
if(found==0)
{
printf("element is not found");
}
}

/* second logic */
#include<stdio.h>
#include<conio.h>
main()
{
 int a[10],i,n,se;
 clrscr();
 printf("enter the no of variables to be used");
 scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the value of a[%d]",i);
scanf("%d",&a[i]);
}
printf("enter the searching element");
scanf("%d",&se);
for(i=0;i<n;i++)
{
if(a[i]==se)
{
printf("the element is found");
break;
}
}
if(i==n)
{
printf("element is not found");
}
}

 /* program to find the no of occurances of a given element in the array */
#include<stdio.h>
#include<conio.h>
main()
{
 int a[10],i,n,se,count=0;
 clrscr();
 printf("enter the no of variables to be used");
 scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the value of a[%d]",i);
scanf("%d",&a[i]);
}
printf("enter the searching element");
scanf("%d",&se);
for(i=0;i<n;i++)
{
if(a[i]==se)
{
count++;
}
}
printf("the element is found %d\n",count);
}

/* binary search */
#include<stdio.h>
#include<conio.h>
main()
{
int top,bot,mid,a[10],i,n,found=0,se;
clrscr();
printf("enter the no of variables to be searched");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the value of a[%d]",i);
scanf("%d",&a[i]);
}
printf("enter the value to be searched");
scanf("%d",&se);
top=0;
bot=n-1;
do
{
mid=(top+bot)/2;
if(a[mid]==se)
{
printf("the element is found");
found=1;
break;
}
else if(a[mid]>se)
{
bot=mid-1;
}
else if(a[mid]<se)
{
top=mid+1;
}
}while((top<=bot));
if(found==0)
{
printf("the element is not found");
}
getch();
}

/* bubble sorting */
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],n,i,j,temp;
clrscr();
printf("enter the no of variables to be used");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the value of a[%d] ",i);
scanf("%d",&a[i]);
}
printf("the original array is\n");
for(i=0;i<n;i++)
{
printf("a[%d]=%d\n",i,a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("the array in sorted order is\n");
for(i=0;i<n;i++)
{
printf("a[%d]=%d\n",i,a[i]);
}
getch();
}

/* program to read  name and print it */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40];
clrscr();
printf("enter the name");
scanf("%s",&x);
printf("the name is %s\n",x);
}

/* program to read name and print space also */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40];
clrscr();
printf("enter the name ");
scanf("%[^\n]",&x);
printf("the name is %s\n",x);
}

/* second logic */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40];
clrscr();
printf("enter the name");
gets(x);
printf("the given name is");
puts(x);
}



 « Previous                                                                  Next Chapter »