Tuesday, 30 October 2012

Practical Program Examples on Arrays


Write a C program to find the average height of 'n' people where n is entered by user.
#include <stdio.h>
int main(){
    int height[100], n, i;
    float sum=0.0, average;
    printf("Enter the total number of person: ");
    scanf("%d",&n);
    while (n>100 || n<=0)
    {
        printf("Error! number should in range of (1 to 100).\n");
        printf("Please enter the number again.\n");
        scanf("%d",&n);
    }
   for(i=0;i<n;++i)
   {
      printf("%d. Enter height: ",i+1);
      scanf("%d",&height[i]);
      sum+=height[i];
   }
   average=sum/n;
   printf("Average height = %.2f",average);
   return 0;
}
Write a program to find largest element of an array.
#include <stdio.h>
int main(){
    int i;
    float arr[10];
    printf("Enter 10 elements to find largest among them: \n");
    for(i=0;i<10;++i)
    {
       printf("Enter Number %d: ",i+1);
       scanf("%f",&arr[i]);
    }
    for(i=1;i<10;++i)
    {
        if(arr[0]<arr[i])
            arr[0]=arr[i];
    }
    printf("Largest element = %.2f",arr[0]);
    return 0;
}
Write a C program to sort the array in ascending order.
#include <stdio.h>
int main(){
     int marks[100],n,i,step,temp;
     printf("Enter the number of students(should not be more than 100).\n");
     scanf("%d",&n);
     for(i=0;i<n;++i){
        printf("Enter marks%d:\n",i+1);
        scanf("%d",&marks[i]);
     }
     for(step=0;step<n-1;++step)
        for(i=0;i<n-step;++i)
        {
           if(marks[i]>marks[i+1])    
           {
              temp=marks[i];
              marks[i]=marks[i+1];
              marks[i+1]=temp;
           }
        }
     printf("In ascending order:\n");
     for(i=0;i<n;++i)
         printf("%d\t",marks[i]);
     return  0;
}
Write a C program to sort the array of 10 elements in descending order. To sort the data, make function Descend() to sort data in descending order and display the data in main() function.
#include <stdio.h>
void Descend(int c[]);
int main(){
   int c[10],i;
   for(i=0;i<10;++i){
     printf("Enter element %d:",i+1);
     scanf("%d",&c[i]);
   }
   Descend(c);
   printf("In descending order: ");
   for(i=0;i<10;++i){
       printf("%d\t",c[i]);
   }
   return 0;
}
Descend(int c[]){
    int i,j,temp;
    for(i=0;i<9;++i)
        for(j=i+1;j<10;++j)
        {
            if(c[i]<c[j]){
                temp=c[i];
                c[i]=c[j];
                c[j]=temp;
            }
        }
}
Write a C program to find the sum of two matrix entered by user using multidimensional arrays.
#include <stdio.h>
int main(){
    int r,c,a[100][100],b[100][100],sum[100][100],i,j;
    printf("Enter number of rows: ");
    scanf("%d",&r);
    printf("Enter number of columns: ");
    scanf("%d",&c);
    printf("Enter elements of 1st matrix");
    for(i=0;i<r;++i)
       for(j=0;j<c;++j)
       {
           printf("Enter a%d%d: ",i+1,j+1);
           scanf("%d",&a[i][j]);
       }
    printf("Enter elements of 2nd matrix");
    for(i=0;i<r;++i)
       for(j=0;j<c;++j)
       {
           printf("Enter a%d%d: ",i+1,j+1);
           scanf("%d",&b[i][j]);
       }
   for(i=0;i<r;++i)
       for(j=0;j<c;++j)
           sum[i][j]=a[i][j]+b[i][j];
    printf("Sum of two matrix is: \n\n");
    for(i=0;i<r;++i)
       for(j=0;j<c;++j)
       {
           printf("%d   ",sum[i][j]);
           if(j==c-1)
               printf("\n\n");
       }
return 0;
}
Write a C program to multiply two matrix. To perform this program,pass two matrix to separate function multiply() to multiply two matrix and display the output in main() function
#include <stdio.h>
void multiply(int a[][50],int b[][50],int c[][50],int r1,int c2,int c1);
int main(){
    int i,j,a[50][50],b[50][50],c[50][50],r1,c1,r2,c2;
    printf("For first matrix: Enter rows and column respectively.");
    scanf("%d%d",&r1,&c1);
    printf("For second matrix: Enter rows and column respectively.");
    scanf("%d%d",&r2,&c2);
    if(c1==r2)
    {
        for(i=0;i<r1;++i)
            for(j=0;j<c1;++j)
            {
                printf("Enter element a%d%d: ",i+1,j+1);
                scanf("%d",&a[i][j]);
            }
        for(i=0;i<r1;++i)
            for(j=0;j<c1;++j)
            {
                printf("Enter element b%d%d: ",i+1,j+1);
                scanf("%d",&b[i][j]);
            }
        multiply(a,b,c,r1,c2,c1);
        for(i=0;i<r1;++i)
            for(j=0;j<c2;++j)
            {
                printf("%d  ",c[i][j]);
                if(j==c2-1)
                   printf("\n");
            }
    }
    else
       printf("Error! column of 1st matrix not equal to row of 2nd");
    return 0;
}
void multiply(int a[][50],int b[][50],int c[][50],int r1,int c2,int c1){
    int i,j,k;
    for(i=0;i<r1;++i)
         for(j=0;j<c2;++j)
               c[i][j]=0;
    for(i=0;i<r1;++i)
         for(j=0;j<c2;++j)
            for(k=0;k<c1;++k)
                 c[i][j]+=a[i][k]*b[k][j];

}

No comments:

Post a Comment