Array

Array:-

Array is a collection of variables which have same name and same data type and equal length. Array is used to over come the problem to use of many variables which is minimize the length of the program and increase the quality of the program. Array is used contiguous memory location, contagious memory location means the memory location of every cell of array present in the memory location in a sequence.


SYNTAX:

datatype variable name[size]

int A[10];
char B[20];
float C[4];


int A[10] means 'A' is an integer type variable which is equal of 10 independent integer type variables, A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8], A[9]. The size of each part of the array is 2 byte and the total; size of variable 'A' is 20 bytes(10 * 2 = 20). In 'c' language array is start from zero(0) so when we declare a array of size n(array[n]) but we get the last element is n-1 as the array start from zero(0).


char B[20] means 'B' is an character type variable which is equal of 20 independent character type variables, A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8], A[9], A[10] , A[11] , A[12] , A[13] , A[14] , A[15] , A[16] , A[17] , A[18] , A[19]  . The size of each part of the array is 2 byte and the tota; size of variable 'A' is 20 bytes(10 * 2 = 20).


ADVANTAGE OF AN ARRAY:

Array is used contiguous memory location and for this reason we can access them vey easily in a particular sequential order. As the array elements are stored in a sequential manner that's why we don't need to remember the location or address of the variable. We can store similar type and same size of data. We can store huge data in an array.


DISADVANTAGE OF AN ARRAY:

As we know that array is used contiguous memory location but if the size of an array will huge and the required space (sequential space) is not available in RAM then it's through an run error, because array uses contiguous memory location. We can't store different type of data in an array and we can't use different size in an array.


int a[]={10,20,30,40,50,60,70,80,90,100};









  • The upper image is showing that the internal structure of an Array. Array is a collection of same type and same length variables with a single name but in an Array every location pr cell have a unique address.





REPRESENTATION OF AN INTEGER ARRAY:


  1. # include <stdio.h>
  2. int main()
  3. {
  4.        int a[5],i;
  5.        for(i=0;i<5;i++)
  6.        {
  7.               printf("Enter value:  ");
  8.               scanf("%d",& a[i]);
  9.         }
  10.               return 0;
  11.   }




OUTPUT:

DISCUSSION: Here we are showing a program and the input output screen and the internal structure of an integer Array. In the main memory Array is created like that. If you see this vary care fully then you can find that the every address location has equal 2 interval that means every cell address captured 2 byte that's why the difference of every location is 2 or 2 bytes. But why 2 byte because the datatype is integer and we know that every integer variable have 2 bytes length.



REPRESENTATION OF AN CHARACTER ARRAY:

  1. # include <stdio.h>
  2. int main()
  3. {
  4.        char a[5],i;
  5.        for(i=0;i<5;i++)
  6.        {
  7.             printf("Enter value:  ");
  8.             scanf("%c",& a[i]);
  9.         }
  10.               return 0;
  11.   }

OUTPUT:-



 




DISCUSSION:  Here we are showing a program and the input output screen and the internal structure of an character Array. In the main memory Array is created like that. If you see this vary care fully then you can find that the every address location has equal 1 interval that means every cell address captured 1 byte that's why the difference of every location is 1 or 1 byte. But why 1 byte because the datatype is char/character and we know that every character/char variable 1 byte length. To store a character in an Array we need 1 byte or 8 bits.


REPRESENTATION OF AN REAL/FLOAT ARRAY:





  1. # include <stdio.h>
  2. int main()
  3. {
  4.        float a[5],i;
  5.        for(i=0;i<5;i++)
  6.        {
  7.             printf("Enter value:  ");
  8.             scanf("%f",& a[i]);
  9.         }
  10.               return 0;
  11.   }
OUTPUT: 

 

DISCUSSION:   Here we are showing a program and the input output screen and the internal structure of an character Array. In the main memory Array is created like that. If you see this vary care fully then you can find that the every address location has equal 4 interval that means every cell address captured 4 bytes that's why the difference of every location is 4 or 4 bytes. But why 4 bytes because the datatype is real/float and we know that every real/float variable 4 bytes length. To store a real number or float number in an Array we need 4 bytes or 32 bits.


SAMPLE PROGRAM OF AN ARRAY


QUESTION 1: Write a program to input 10 numbers in an array and show the stored item.

   CODE:
  1. # include <stdio.h>
  2. int main()
  3. {
  4.        int a[10],i;
  5.        for(i=0;i<10;i++)
  6.        {
  7.             printf("Enter value:  ");
  8.             scanf("%d",& a[i]);
  9.         }
  10.         
  11. printf("The entered number is: ");
  12. for(i=0;i<10;i++)
  13.        {
  14.             printf("\nEnter value:  %d",a[i]);
  15.        }
  16.               return 0;
  17.   }
OUTPUT:-

DISCUSSION: In this program we are showing the mechanism of an array where we can store some element in an array and display them with the help of for statement/ loop statement.


QUESTION 2: Write a program to input 10 numbers in an array and show them in reverse order.

CODE:-
  1. # include <stdio.h>
  2. int main()
  3. {
  4.        int a[10],i;
  5.        for(i=0;i<10;i++)
  6.        {
  7.             printf("Enter value:  ");
  8.             scanf("%d",& a[i]);
  9.         }
  10.         
  11. printf("The entered number is from end: ");
  12. while(i)
  13.        {
  14.             --i;
  15.             printf("\nEnter value:  %d",a[i]);
  16.        }
  17.               return 0;
  18.   }
DISCUSSION:-

DISCUSSION: Here the program is showing the input and output value in an array where we input value in an array and at the time of output we display them from reverse.


QUESTION 3: Write a program to store a name in an array and show the name.

CODE:

  1. # include <stdio.h>
  2. int main()
  3. {
  4.        char name[10];
  5.        printf("The enter your name: ");
  6.         scanf("%s",name);
  7.        printf("\nEnter name is:  %s",name);
  8.        return 0;
  9.   }
OUTPUT:-

DISCUSSION: In this program we store a string in an array and display the string. To store a string we need and array because string means collection of characters.


QUESTION 4: Write a program to store 5 names in an array and show the names.

CODE:
  1. # include <stdio.h>
  2. int main()
  3. {
  4.        char name[5][25];
  5.        int i=5;
  6.        while(i)
  7.        {
  8.         i--;
  9.              printf("The enter your name: ");
  10. scanf("%s",name[i]);
  11.   }
  12.  printf("The entered name is: ");
  13.       for(i=0;i<5;i++)
  14.  printf("\nEnter name is:  %s",name[i]);
  15.       return 0;
  16.   }

OUTPUT:-


DISCUSSION: Here in this program we represent a two dimensional array or 2D array, where we store a single string in an array but if we store multiple string the we have to use two dimensional array or 2D array.

name[5][25] means name is a character type variable where 5 row and 25 columns, each string have maximum 25 length and maximum 5 string can store in this variable.




QUESTION 5: Write a program to summation of two array 

CODE:-
  1. # include <stdio.h>
  2. int main()
  3. {
  4.        int Arr1[5],Arr2[5],Sum[5];
  5.        int i;
  6.        printf("Enter element of an Array1:");
  7.        for(i = 0;i<5;i++)
  8.         {
  9.         printf("\nEnter No %d = ",i+1);
  10.         scanf("%d",& Arr1[i]);  
  11.         }
  12.         printf("Enter element of an Array2:");
  13.        for(i = 0;i<5;i++)
  14.         {
  15.         printf("\nEnter No %d = ",i+1);
  16.         scanf("%d",& Arr2[i]);
  17.        
  18.         }
  19.        for(i = 0;i<5;i++)
  20.         {
  21.         Sum[i] = Arr1[i] + Arr2[i];
  22.         }
  23.  printf("\n\nTHE RESULT OF SUMMATION IS: ");
  24.       for(i=0;i<5;i++)
  25.  printf("\n%d + %d = %d ",Arr1[i],Arr2[i],Sum[i]);
  26.       return 0;
  27.   }

OUTPUT:

DISCUSSION:  


QUESTION 6: Write a program to multiplication of two array, and store the result in an third array.


CODE:

# include<stdio.h>
int main()
{
int Num1[5],Num2[5],Num3[5],i;
printf("Enter 1st Array value: \n");

for(i = 0; i< 5 ; i++)
{
printf("Enter No %d: ",i+1);
scanf("%d",&Num1[i]);
}

printf("Enter 2nd Array value: \n");

for(i = 0; i< 5 ; i++)
{
printf("Enter No %d: ",i+1);
scanf("%d",&Num2[i]);
}

printf("RESULT OF SUMMATION: \n");

for(i = 0; i< 5 ; i++)
{ Num3[i] = Num1[i] * Num2[i];
printf("\n%d + %d =  %d",Num1[i],Num2[i],Num3[i]);

}
return 0;

}


OUTPUT:





DISCUSSION:


QUESTION 7:   Write a program to input a string and print character by character vertically.

CODE:

#include <stdio.h>
int main()
{
char Str[20];
int i = 0,j;

printf("Enter your string: ");
gets(Str);

while(Str[i]!='\0')
{
printf("\n  %c", Str[i]);
i++;
}
return 0;

}

OUTPUT:



QUESTION 8:  Write a program to input a string and print character by character vertically in capital letter if the enter character can be any case.


CODE:

#include <stdio.h>
int main()
{
char Str[20];
int i = 0,j;

printf("Enter your string: ");
gets(Str);

while(Str[i]!='\0')
{
if(Str[i]>90)
Str[i]-=32;
printf("\n  %c", Str[i]);
i++;
}
return 0;
}

OUTPUT:



DISCUSSION:





























Translate