Wednesday, 13 December 2017

Array Programs In C

  PROGRAM FOR ONE DIMENSIONAL ARRAY IN C:

#include<stdio.h>
#include<conio.h>
void main()
{
   int i;
    clrscr();
   int arr[5] = {10,20,30,40,50};

   /* Above array can be initialized as below also
      arr[0] = 10;
      arr[1] = 20;
      arr[2] = 30;
      arr[3] = 40;
      arr[4] = 50; */

   for (i=0;i<5;i++)
   {
      // Accessing each variable
      printf("value of arr[%d] is %d \n", i, arr[i]);
   }
getch();

}

PROGRAM  FOR  TWO DIMENSIONAL ARRAY IN C:

#include<stdio.h>
#include<conio.h>
void main()
{
      int i,j;
      clrscr();
   int arr[2][2] = {10,20,30,40};
   /* Above array can be initialized as below also
      arr[0][0] = 10;
      arr[0][1] = 20;
      arr[1][0] = 30;
      arr[1][1] = 40; */
   for (i=0;i<2;i++)
   {
      for (j=0;j<2;j++)
      {
         // Accessing variables
         printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]);
      }
   }

getch();

}
To Calculate Sum & Average of an Array
#include <stdio.h>
#include<conio.h>
void main()
{
    int arr[20],i,sum=0,n;
            float avg=0.0;
            clrscr();
            printf("Enter the number of element U want to enter:");
            scanf("%d",&n);
            printf("Enter %d elements to the array arr:",n);
            for(i=0;i<n;i++)           
                 scanf("%d",&arr[i]);                 //reading the array elements
            for(i=0;i<n;i++)
                    sum=sum+arr[i];
            avg =sum/n;
            printf("The array elements are:\t");
            for(i=0;i<n;i++)
            printf("%d\t",arr[i]);                  //displaying the array elements
            printf("The sum of elements =%d",sum);
            printf("The average of elements =%f",avg);
            getch();
}
To Find the Largest Two Numbers in a given Array
     #include<stdio.h>
     #include<conio.h>
      void main()
          {
        int array[5], i, larg1, larg2, temp;
             clrscr();
            printf("Enter five numbers \n"); //you have given arraysize 5
            for (i = 0; i < 5; i++)
            {
                scanf("%d", &array[i]);
            }
         
            printf("Input interger are \n");
            for (i = 0; i < 5; i++)
            {
              printf("%d", array[i]);
            }
            printf("\n");
            /*  assume first element of array is the first larges t*/
            larg1 = array[0];
    /*  assume first element of array is the second largest */
            larg2 = array[1];
            if (larg1 < larg2)
            {
        temp = larg1;
                larg1 = larg2;
                larg2 = temp;
            }
            for (i = 2; i < 4; i++)
            {
                if (array[i] >= larg1)
                {
                    larg2 = larg1;
                    larg1 = array[i];
                }
        else if (array[i] > larg2)
                {
                    larg2 = array[i];
                }
            }
   printf("%d is the first largest \n", larg1);

    printf("%d is the second largest \n", larg2);

 printf("Average of %d and %d = %d \n", larg1, larg2,(larg1 + larg2) / 2);
    getch();
 }

C Program to Find the Largest Number in an Array
   #include<stdio.h>
   #include<conio.h>
   void main()
        {
          int array[40], size, i, largest;
          clrscr();
            printf("\n Enter the size of the array: ");
            scanf("%d", &size);

            printf("\n Enter %d elements of  the array: ", size);

            for (i = 0; i < size; i++)
        scanf("%d", &array[i]);

    largest = array[0];

    for (i = 1; i < size; i++)
            {
                if (largest < array[i])
                    largest = array[i];
            }
 printf("\n largest element present in the given array is : %d", largest);


    getch();
 }

To Print the Number of Odd & Even Numbers in an Array
#include<stdio.h>
#include<conio.h>
 void main()
     {
 int array[50], i, num;
 clrscr();
printf("Enter the size of an array \n");
                scanf("%d", &num);
                printf("Enter the elements of the array \n");
                for (i = 0; i < num; i++)
                {
                    scanf("%d", &array[i]);
                }
                printf("Even numbers in the array are: ");
                for (i = 0; i < num; i++)
                {
                    if (array[i] % 2 == 0)
                    {
                        printf("%d \t", array[i]);
        }
                }
                printf("\n Odd numbers in the array are:");
                for (i = 0; i < num; i++)
          {
                    if (array[i] % 2 != 0)
                    {
            printf("%d \t", array[i]);
                    }
                }
       getch();
 }
To sort elements of array in ascending order.
#include<stdio.h>
#include<conio.h>
 void main()
{
 int arr1[50];
    int n, i, j, tmp;
          clrscr();
         
     
    printf("Input the size of array : ");
    scanf("%d", &n);

       printf("Input %d elements in the array :\n",n);
       for(i=0;i<n;i++)
            {
                printf("element - %d : ",i);
                scanf("%d",&arr1[i]);
              }

    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(arr1[j] <arr1[i])
            {
                tmp = arr1[i];
                arr1[i] = arr1[j];
                arr1[j] = tmp;
            }
        }
    }
    printf("\nElements of array in sorted ascending order:\n");
    for(i=0; i<n; i++)
    {
        printf("%d ", arr1[i]);
    }
       printf("\n");
 getch();
 }
To sort elements of an array in descending order.
#include<stdio.h>
#include<conio.h>
 void main()
{
 int arr1[50];
    int n, i, j, tmp;
         
  

    printf("Input the size of array : ");
    scanf("%d", &n);

       printf("Input %d elements in the array :\n",n);
       for(i=0;i<n;i++)
            {
                printf("element are %d : ",i);
                scanf("%d",&arr1[i]);
              }
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(arr1[i] < arr1[j])
            {
                tmp = arr1[i];
                arr1[i] = arr1[j];
                arr1[j] = tmp;
            }
        }
    }

     printf("Elements of array is sorted in descending order:\n");
    for(i=0; i<n; i++)
    {
        printf("%d ", arr1[i]);
    }
                  printf("\n");
 getch();
 }

Write a program in C for addition of two Matrices
#include<stdio.h>
#include<conio.h>
 void main()
{
int arr1[20][20],arr2[20][20],c[30][30],i,j,n;
 
    
        printf("Input the size of the square matrix (less than 5): ");
       scanf("%d", &n);
 
    /* Stored values into the array*/
       printf("Input elements in the first matrix :\n");
       for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                     printf("element - [%d],[%d] : ",i,j);
                     scanf("%d",&arr1[i][j]);
            }
        }  
 
       printf("Input elements in the second matrix :\n");
       for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                     printf("element - [%d],[%d] : ",i,j);
                     scanf("%d",&arr2[i][j]);
            }
        }   
  
/* calculate the sum of the matrix */  
   for(i=0;i<n;i++)
       for(j=0;j<n;j++)
            c[i][j]=arr1[i][j]+arr2[i][j];
   printf("\nThe Addition of two matrix is : \n");
   for(i=0;i<n;i++){
       printf("\n");
       for(j=0;j<n;j++)
            printf("%d\t",c[i][j]);
   }
   printf("\n");

getch();
 }
Write a program in C for multiplication of Matrices.
#include<stdio.h>
#include<conio.h>
 void main()
{
int arr1[20][20],arr2[20][20],c[20][20],i,j,k,r1,c1,r2,c2,sum=0;
  clrscr();
      
 
  printf("Input the rows and columns of first matrix : ");
  scanf("%d %d",&r1,&c1);
  printf("Input the rows and columns of second matrix : ");
  scanf("%d %d",&r2,&c2);
  if(c1!=r2){
      printf("Mutiplication of Matrix is not possible.");
      printf("Column of first matrix and row of second matrix must be same.");
  }
  else
      {
       printf("Input elements in the first matrix :\n");
       for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
            {
                printf("element - [%d],[%d] : ",i,j);
                scanf("%d",&arr1[i][j]);
            }
        }  
       printf("Input elements in the second matrix :\n");
     for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
            {
                printf("element - [%d],[%d] : ",i,j);
                scanf("%d",&arr2[i][j]);
            }
        }   
      printf("The First matrix is :\n");
          for(i=0;i<r1;i++)
          {
              printf("\n");
              for(j=0;j<c1;j++)
              printf("%d\t",arr1[i][j]);
          }
 
     printf("The Second matrix is :\n");
          for(i=0;i<r2;i++)
          {
              printf("\n");
              for(j=0;j<c2;j++)
              printf("%d\t",arr2[i][j]);
          }
              //multiplication of matrix
           for(i=0;i<r1;i++)
          for(j=0;j<c2;j++)
           c[i][j]=0;
             for(i=0;i<r1;i++)    //row of first matrix
                 {
        for(j=0;j<c2;j++)    //column of second matrix
              { 
            sum=0;
        for(k=0;k<c1;k++)
          sum=sum+arr1[i][k]*arr2[k][j];
         c[i][j]=sum;
                     }
                 }
  printf("The multiplication of two matrices is : \n");
  for(i=0;i<r1;i++)
     {
        printf("\n");
        for(j=0;j<c2;j++)
         {
           printf("%d\t",c[i][j]);
         }
     }
 
printf("\n");
getch();
 }                         
Write a program in C to find transpose of a given matrix.
#include<stdio.h>
#include<conio.h>
 void main()
{
int arr1[50][50],arr2[50][50],i,j,k=0,r,c;
 
   clrscr();
   printf("\n\nTranspose of a Matrix :\n");
       printf("---------------------------\n");


       printf("\nInput the rows and columns of the matrix : ");
       scanf("%d %d",&r,&c);

       printf("Input elements in the first matrix :\n");
       for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                       printf("element - [%d],[%d] : ",i,j);
                       scanf("%d",&arr1[i][j]);
            }
        }

             printf("\nThe matrix is :\n");
                        for(i=0;i<r;i++)
                        {
                      

                        for(j=0;j<c;j++)
                        {
            
               printf("%d\t",arr1[i][j]);
                        }
                printf("\n");
                      
                   }
 

 for(i=0;i<r;i++)
     {
      for(j=0;j<c;j++)
            {
                   arr2[j][i]=arr1[i][j];
            }
      }


      printf("\n\nThe transpose of a matrix is : ");
      for(i=0;i<c;i++){
      printf("\n");
      for(j=0;j<r;j++){
           printf("%d\t",arr2[i][j]);
      }
  }

      printf("\n");
getch();
 }

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...