Blogger templates

Pages

Saturday, 23 June 2012

WAP of C Language that accepts the size of Array and Stores the Element in num Array

Array

Statement of C Program: This Program accepts n Integers and stores them in an Array called num. It also prints them:

#include<stdio.h>
#include<conio.h>
void main()
{
int num[20];
int n , i ;
clrscr();
printf(" Enter the size of an Array\n");
scanf("%d" , &n);

printf(" Enter the elements\n");

for(i = 0 ; i<n ; i++)
{
scanf("%d" , num[i]);
}

printf(" Array:\n ");

for(i = 0 ; i<n ; i++)
{
printf(" num[%d] = %d\n" , i , num[i]);
}
}                                                  /* End of main() */


 Output:
 Enter the size of an Array
 5
 Enter the elements
 10
 20
 30
 40
 50
 Array:
 num[0] = 10
 num[1] = 20
 num[2] = 30
 num[3] = 40
 num[4] = 50


Explanation of the Program:

  • Array declaration: 
Like other variables, Array also needs to be declared so that Compiler will know what kind of an Array and how large an Array we want. This is done with the statement:


      int num[20];      


  1. int specifies the type of the variable. 
  2. num specifies the name of variable.
  3. [20] , The number 20 tells how many elements of the type int will be in our Array. This number is often called 'dimension' of the Array.
  4. The bracket [ ] tells the compiler that we are dealing with Array.
  5. num[15] is not the 15th element of the Array but the 16th element. 
  6. The number with in the square brackets also specifies the element's position in the Array.
  • Entering Data into an Array:
             for(i = 0 ; i<n ; i++)
             {
             scanf("%d" , num[i]);
             }

  • Reading Data from an Array:
            for(i = 0 ; i<n ; i++)
            {
            printf(" num[%d] = %d\n" , i , num[i]);
            }


                                                                     That's All


0 comments:

Post a Comment