Sunday, December 25, 2011

Arrays

An array is a collection of variables of the same  type under a single name. Arrays are used to store data that have common purpose.

Arrays can be of one dimensional, two dimensional or multidimesional. Anyway, multidimensional array those are more than 3 dimensions are hard to imagine. Now, we are going to concentrate on one and  two dimensional arrays.

Very well then. How do I access the array values if all of them have the same name ? Its just like a book. Just as the pages are numbered in a book, each value in an array are identified with what is called as 'index'.

1. One Dimensional Array
Following picture portrays the one dimensional array of size 'n'. As you know, for an array of size 'n', the indices of the array are from '0' to 'n-1' (shown in the picture) :
An one dimensional array of size 'n'
An one dimensional array is declared as follows :
datatype identifier[size(optional)];
(or)
datatype identifier[size(optional)] = {Constants}; 
For example if we declare an array of type 'int' of size 5 and identifier as 'marks' , then the declaration would be as follows :
int marks[5];
//Or else it could be declared and initialized with the values as follows :
int marks[5]={60,67,73,66,70};
Here, marks[0] = 60 marks[1] = 67 marks[2] = 73 marks[3] = 66 marks[4] = 70
Now, lets see this with an example:
/* Fetch marks, Find Total Marks and Display */
#include<stdio.h>
void main()
{
    int marks[5],sum=0,i;
    char name[20];    /* String of size 20 */ 
    /* A String is an array of Characters */  
    printf("\n Demonstration of One Dimensional Array ");
    printf("\n Enter the name of the student : \t");
    gets(name);
    printf("\n Enter the marks obtained in 5 subjects : \n");
    for(i=0;i<5;i++)
    {
        scanf("%d",&marks[i]);        
    }
    printf("\n Student Name : \t %s",name);
    printf("\n Marks ");
    for(i=0;i<5;i++)
    {
        printf("\n %d ",marks[i]);
        sum += marks[i];
    }
    printf("\n Total Marks : \t %d",sum);
}
Sample Output : Demonstration of One Dimensional Array Enter the name of the student : Hari Hara Sudhan Enter the marks obtained in 5 subjects : 12 23 34 45 56 Student Name : Hari Hara Sudhan Marks 12 23 34 45 56 Total Marks : 170
2. Two Dimensional Array
The two dimensional array consists of rows and columns. An array of m rows and n columns is of size m * n. The numbering goes from 0 to m-1 for rows and 0 to n-1 for columns.


datatype identifier[rows][columns];
(or)
datatype identifier[rows][columns] = {Constants};


The two dimensional array is pictorially represented as follows :
Two Dimensional Array of 'm' rows and 'n' columns
Here is an example how a two dimensional array is declared and initialized :


int array[4][3]={{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
In this, each row is seperated from the other by using curly braces. Thus the first row is : array[0][0] = 1 array[0][1] = 2 array[0][2] = 3 The second row is : array[1][0] = 4 array[1][1] = 5 array[1][2] = 6 The third will be : array[2][0] = 7 array[2][1] = 8 array[2][2] = 9 The fourth row will be : array[3][0] = 10 array[3][1] = 11 array[3][2] = 12
Now, lets see this with an example :
#include<stdio.h>
void main()
{
    int item_details[20][3],i,count=0; /* Three Columns - Unit Price, Quantity, Total Price 
    item_details[some_row][0] - item price
    item_details[some_row][1] - item quantity 
    item_details[some_row][2] - item total price
    A maximum of twenty rows */
    char item_name[20][25],choice;    /* Here item_name - Maximum of 25 characters(columns) and 20 names(rows)*/
    printf("\n Demonstration of two dimensional array ");
    do
    {
         printf("\n Enter the Item name : \t ");
         gets(item_name[count]);
         /* gets - used to get a line of characters from the user */
         printf("\n Enter the per item price : \t ");
         scanf("%d",&item_details[count][0]);
         /* Get the item price */
         printf("\n Enter the quantity : \t ");
         scanf("%d",&item_details[count][1]);
         /* Get the item quantity */
         item_details[count][2] = item_details[count][0] * item_details[count][1];
         /* Calculate the total price
              total price = item price * quantity    */ 
         printf("\n Do you want to add another item (y/n) ? ");
         count++;
         choice = getchar();
         fflush(stdin);         /* Clear the Input Buffer */
    }while(choice=='y'||choice=='Y');
    printf("\n\n Items List : ");
    printf("\n Item Name \t Item Price \t Quantity \t Total Price");
    for(i=0;i<count;i++)
    {
 printf("\n %s \t\t %d \t\t %d \t\t %d",item_name[i],item_details[i][0],item_details[i][1],item_details[i][2]);
    }
}

No comments:

Post a Comment