\"Writing.Com
*Magnify*
SPONSORED LINKS
Printed from https://writing.com/main/view_item/item_id/1755013-C-Language---Array
Item Icon
Rated: E · Chapter · Educational · #1755013
Array is a very good thing which a procedural language like C provides to its users.
Array


1.Definition -

The array is a collection of similar data elements stored in continuous or contagious memory locations.The term memory used here is for the memory which is being used by the computer during program execution.

2.Need for an array -

Suppose, we have to use 100 values in any C program then we can do it in two ways:

Either we can declare 100 different variables of different names and then store our 100 values in those variables or we can declare an array of size 100 and store all the 100 values in it.It is surely better to use the second option that will save our valueable time because we will have to declare only one array instead of declaring 100 different variables.

3.A real life example for better understanding -

This is a life example and if you will study through this example then I am sure that you will never forget the concept of Array.Consider an array like a pod of pea(matar),if your look inside it,there are a number of pea beans in it,the array is like that pod of pea in which all the variables are systematically arranged like those pea beans.Imagine that if that pod of pea will not be there then a farmer would have to harvest single-single pea bean separately,how time-consuming it might be?Moreover,the storage of those all loose pea beans might be a big problem,but luckily the god has granted us the pea beans in the pea pod so that we can conveiniently handle them.In the similar way the Dennis Ritchie(creator of C language) has granted us the array so that we can manage and handle a large number of Data elements in a better way.

4.Types of array -

A. 1 - dimensional array

B. 2 - dimensional array

C. Multi - dimensional array

5. A short program for demonstration of 1 - dimensional array(character array - string)

To check whether the given string is pallindrome or not :~

#include<stdio.h>

#include<conio.h>

void main()

{

         char str[50];

         int i=0,j,flag=0,L=0;

         clrscr();

         printf("\nEnter any string\n");

         fflush(stdin);

         gets(str);

         while(str[i]!='\0')

         {

                   L++;

                   i++;

         }

         j=L-1;

         i=0;

         while(i<=j)

         {

                   if(str[i]==str[j])

                   flag=1;

                   else

                   {

                             flag=0;

                             break;

                   }

                   i++;

                   j--;

         }

         if(flag==1)

         printf("A Pallindrome\n");

         else

         printf("Not a pallindrome\n");

         getch();

}

Example for demonstrating a 2 - dimentional array -

To find out the sum of non - diagonal elements in a given matrix:-



#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

long int a[10][10],i,j,m,n,sum;

void main()

{

         clrscr();

         void input();

         void calculate();

         void output();

                getch();

}

void input()

{

             

                printf("\n\n\nEnter no. of rows and columns:\n");

         scanf("%d%d",&m,&n);

         if(m!=n)

         {

                   printf("Sum is not possible!!");

                   getch();

                   exit(1);

         }

         printf("\n\nEnter "%d" elements of Matrix:\n",m*n);

         for(i=0;i<m;i++)

                   for(j=0;j<n;j++)

                   scanf("%d",a[i][j]);

}

void calculate()

{

                   sum=0;

                   for(i=0;i<m;i++)

                             for(j=0;j<n;j++)

                             {

                                       if(i!=j)

                                       sum=sum+a[i][j];

                                       if(i==j+2||j==i+2)

                                       sum=sum-a[i][j];



                             }

}

void output()

{    printf("\nSum of non - diagonal elements=\n",sum);}
© Copyright 2011 Chitrank (chitwrite at Writing.Com). All rights reserved.
Writing.Com, its affiliates and syndicates have been granted non-exclusive rights to display this work.
Printed from https://writing.com/main/view_item/item_id/1755013-C-Language---Array