Tuesday, 27 March 2012

Programs - Array


Program-1  : Write a program to create an array of integers
// Create an array of integer

#include<stdio.h>
#include<conio.h>

void main()
{
   int no[50],i,n;
  
   clrscr();

   printf("\n Enter how amny numbers : ");
   scanf("%d",&n);

   printf("\n Input numbers in array ");
   printf("\n _______________________");

   for(i=0;i<n;i++)
    {
          printf("\n Enter value of number %d : ",i+1);
          scanf("%d",&no[i]);
    }

    printf("\n output ");

    for(i=0;i<n;i++)
    {
          printf("\n%d",no[i]);
         
    }
getch();

}


Program-2 :  Write a program to calculate sum and average of number stored in array

// Calculate sum  and average of numbers
#include<stdio.h>
#include<conio.h>

void main()
{
   int no[50],i,n,sum=0;
   float avg;

   clrscr();

   printf("\n Enter how amny numbers : ");
   scanf("%d",&n);

   printf("\n Input numbers in array ");
   printf("\n _______________________");

   for(i=0;i<n;i++)
    {
          printf("\n Enter value of number %d : ",i+1);
          scanf("%d",&no[i]);
    }

    printf("\n output ");

    for(i=0;i<n;i++)
    {
          printf("\n%d",no[i]);
         
          sum=sum+no[i];
    }

    avg=sum/n;
    printf("\n Sum     = %d",sum);
    printf("\n Average = %f",avg);
    getch();
}

Program-3 : Write a program to count total number of odd and even numbers

// Count total odd and even numbers

#include<stdio.h>
#include<conio.h>

void main()
{
   int no[50],i,n,o=0,e=0;
   float avg;

   clrscr();

   printf("\n Enter how amny numbers : ");
   scanf("%d",&n);

   printf("\n Input numbers in array ");
   printf("\n _______________________");

   for(i=0;i<n;i++)
    {
          printf("\n Enter value of number %d : ",i+1);
          scanf("%d",&no[i]);
    }

    printf("\n output ");

    for(i=0;i<n;i++)
    {
          printf("\n%d",no[i]);
          if(no[i]%2 == 0)
            e++;
          else
            o++;
    }

printf("\n Total Odd =  %d \n Total Even = %d",o,e);

}

Program -4  :Write a program for addition of two different one dimensional array.

// addition of elements of two different array

#include<stdio.h>
#include<conio.h>

void main()
{
   int no1[5],no2[5],no3[5],i;
   clrscr();
   printf("\n Enter elements for Array-1");
   for(i=0;i<5;i++)
    {
          printf("\n Enter value of number %d : ",i+1);
          scanf("%d",&no1[i]);
    }

    printf("\n Enter elements for Array-2");

    for(i=0;i<5;i++)
    {
          printf("\n Enter value of number %d : ",i+1);
          scanf("%d",&no2[i]);
    }
    // Addition
    for(i=0;i<5;i++)
     {
          no3[i]=no1[i]+no2[i];
     }

    printf("\n output Array-1");
    for(i=0;i<5;i++)
    {
          printf("\n%d",no1[i]);
    }
    printf("\n output Array-2");
    for(i=0;i<5;i++)
    {
          printf("\n%d",no2[i]);
    }
    printf("\n output Array-3");
    for(i=0;i<5;i++)
    {
          printf("\n%d",no3[i]);
    }
 getch();
  }


Program - 5 :  Write a program to create 3X3 matrix
// Create matrix (Two dimensional array)
#include<stdio.h>
#include<conio.h>

void main()
          {
                   int i,j;
                   int mat[3][3];
                   clrscr();
                   printf("\n Input \n" );
                   for(i=0;i<3;i++)
                   {

                             for(j=0;j<3;j++)
                             {
                             printf("\n i=%d j=%d",i,j);
                             printf("\n enter element [%d][%d] : ",i+1,j+1);
                             scanf("%d",&mat[i][j]);
                             }
                   }


                   printf("\n output \n" );
                   for(i=0;i<3;i++)
                   {
                             for(j=0;j<3;j++)
                             {
                                      printf("%4d",mat[i][j]);
                             }
                             printf("\n");
                   }
             getch();
          }


Program - 6 :  Write a program to for addition of two 3X3 matrix

// Addition of  two  matrix

#include<stdio.h>
#include<conio.h>

void main()
          {
                   int i,j;
                   int mat1[3][3],mat2[3][3],mat3[3][3];
                   clrscr();
                   printf("\n Input Matrix -1  \n" );
                   for(i=0;i<3;i++)
                   {
                             for(j=0;j<3;j++)
                             {
                             printf("\n enter element [%d][%d] : ",i+1,j+1);
                             scanf("%d",&mat1[i][j]);
                             }
                   }
                   printf("\n Input Matrix -2  \n" );
                   for(i=0;i<3;i++)
                   {
                             for(j=0;j<3;j++)
                             {
                             printf("\n enter element [%d][%d] : ",i+1,j+1);
                             scanf("%d",&mat2[i][j]);
                             }
                   }

                   printf("\n output  Matrix-1 \n" );
                   for(i=0;i<3;i++)
                   {
                             for(j=0;j<3;j++)
                             {
                                      printf("%4d",mat1[i][j]);
                             }
                             printf("\n");
                   }
                   printf("\n output  Matrix-2 \n" );
                   for(i=0;i<3;i++)
                   {
                             for(j=0;j<3;j++)
                             {
                                      printf("%4d",mat2[i][j]);
                             }
                             printf("\n");
                    }

                   for(i=0;i<3;i++)
                    {
                      for(j=0;j<3;j++)
                        {
                             mat3[i][j]=mat1[i][j]+mat2[i][j];
                        }
                      }
             printf("\n output  Matrix-3 \n" );
                   for(i=0;i<3;i++)
                   {
                             for(j=0;j<3;j++)
                             {
                                      printf("%4d",mat3[i][j]);
                             }
                             printf("\n");
                   }

             getch();
          }


Posted By  Ruchita Panday

No comments:

Post a Comment