Delicious Digg Facebook Favorites More Stumbleupon Twitter
Showing posts with label Arrays in 'C'. Show all posts
Showing posts with label Arrays in 'C'. Show all posts

Thursday, 20 September 2012

Two dimensional array



                                                 Two dimensional Array

                  Two dimensional array syntax

                  datatype variablename arraylenth;

                  Ex:-
                            int a[][];
                            int a[10][10],b[10][10];

                  /* Sum of all the elements in the matrix */
         
                  #include<stdio.h>
                  main()
                  {
                          int j,k,a[2][2],sum=0;
                          printf("\nEnter the elements for matrix a:");
                          for(j=0;j<2;j++)
                          {
                               for(k=0;k<2;k++)
                               {
                                    printf("\nEnter a[%d][%d]:",j,k);
                                    scanf("%d",&a[j][k]);
                                }
                           }
                           printf("\n the matrix is...:\n");
                           for(j=0;j<2;j++)
                           {
                               for(k=0;k<2;k++)
                               {
                                     printf("\t%d",a[j][k]);
                                     sum=sum+a[j][k];
                                }
                                printf("\n");
                            }

         printf("\nthe sum of all elements in the matrix is:%d",sum);
       
          }

 

One dimensional array example 3



   /*  Program to displaying numbers in reverse order */
        #include<stdio.h>
        main()
       {
            int a[10],k;
            clrscr();
            for(k=0;k<10;k++)
           {
                 printf("\nEnter the element for a[%d]:",k);
                 scanf("%d",&a[k]);
           }
                 printf("\nwriting the numbers in the reverse order:\n");
                 for(k=10-1;k>=0;k--)
                 printf("\nthe element in indexa[%d]=%d",k,a[k]);
                 getch();
       }

  
    

One dimensional array example 2



                   One dimensional array example

           /* program to calculate sum of 10 numbers in an array */

           #include<stdio.h>
           main()
           {
               int array[10],j,sum=0;
               clrscr();
               printf("\nEnter 10 numbers :");
               for(j=0;j<=9;j++)
               scanf("%d",&array[j]);
               printf("\ncalculating the sum of elements in the array:");
               for(j=0,sum=0;j<=9;j++)
               sum=sum+array[j];
               printf("\nsum of the elements of the array =%d",sum);
               getch();
            }
 

Wednesday, 19 September 2012

One dimensional array example 1


                      One dimensional Array

        One dimensional array  syntax
     
        datatype variablename arraylenth;
    
        Ex:-
                  int a[];
                  int a[10],b[10];

                  #include<stdio.h>
                  main()
                 {
                     int a[5],i;
                     clrscr();
                     for(i=0;i<5;i++)
                     {
                        printf("\nEnter the elements for a[%d]:",i);
                        scanf("%d",&a[i]);
                      }
                      printf("original order is....\n");
                      for(i=0;i<5;i++)
                      printf("\n the element in index a[%d]=%d",i,a[i]);
                      getch();
                   }

              

Monday, 10 September 2012

Array

Array



An array  is a collection of similar elements.
Their similar elements can be all integers or floats or chars.
The array of characters is called as a string or word.
The array of integers or floats is called by the original name(n) as array.
The first element in the  array  is numbered as ‘0’ , there fore  the last element
Is one less than the original size(n-1) of the array.
An array is also termed as “Subscripted variable” .
The elements inside an array are stored in continuous memory locations ,irrespective  of its size.
Array declaration :-  

datatype variablename arraylenth;
Example :-   
                         int array[];
                         int a[10];
                         int a[3]={4,6,12};

Arrays are divided into 2 types.
They are,
                    
                        * one dimensional array.
                        * Two/multi dimensional array.