Monday, 19 June 2023

C Programming Chapter 5 Array

ARRAY

C – PROGRAMS.

 

/* Write a program to show an array */

 

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10]={0,1,2,3,4,5,6,7,8,9};

int i;

clrscr();

for(i=0;i<10;i++)

            {

            printf("The Element in %d = %d\n",i,a[i]);

            }

getch();

}

 

/* Write a program to accept the array from the keyboard */

 

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10];

int i;

clrscr();

 

for(i=0;i<10;i++)

            {

            printf("Enter The Element in %d ",i);

            scanf("%d",&a[i]);

            }

 

for(i=0;i<10;i++)

            {

            printf("The Element in %d = %d\n",i,a[i]);

            }

getch();

}

 

/* Write a program to show reverse of the elements of an array */

 

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10];

int i,sum=0;

clrscr();

 

for(i=0;i<10;i++)

            {

            printf("Enter The Element in %d ",i);

            scanf("%d",&a[i]);

            }

 

for(i=10-1;i>=0;i--)

            {

            sum+=a[i];

            printf("The Element in %d = %d\n",i,a[i]);

            }

getch();

}

 

/* Write a program to add the elements of an array */

 

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10];

int i,sum=0;

clrscr();

 

for(i=0;i<10;i++)

            {

            printf("Enter The Element in %d ",i);

            scanf("%d",&a[i]);

            }

 

for(i=0;i<10;i++)

            {

            sum+=a[i];

            printf("The Element in %d = %d\n",i,a[i]);

            }

printf("\nThe sum of the elements = %d",sum);

getch();

}

 

/* Write a program to add the runs of ten batsmen with average */

 

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10];

int i;

float sum=0,avg=0;

clrscr();

 

for(i=1;i<=11;i++)

            {

            printf("Enter The Runs for Batsman_%d ",i);

            scanf("%d",&a[i]);

            }

 

for(i=1;i<=11;i++)

            {

            sum+=a[i];

            printf("The Element in %d = %d\n",i,a[i]);

            }

 

avg=sum/11;

printf("Total Runs =%.0f\nAverage = %.2f",sum,avg);

getch();

}

 

/*Programs to count positive, negative, odd & even numbers in an array*/

 

#include<stdio.h>

#include<conio.h>

void main()

{

int num[20],i,neg=0,pos=0,odd=0,even=0;

clrscr();

printf("Enter the Elements of the array\n");

for(i=0;i<=19;i++)

            {

            printf("%d\t",i);

            scanf("%d",num[i]);

            }

for(i=0;i<=19;i++)

            {

            num[i]<0 ? neg++ : (pos++);

            num[i]%2 ? odd++ : (even++);

            }

printf("\n Negative Elements = %d",neg);

printf("\n Positive Elements = %d",pos);

printf("\n Even Elements = %d",even);

printf("\n Odd Elements = %d",odd);

printf("\n\n Press Any Key to Exit");

getch();

}

 

/*Programs to make a selection sort of an array*/

 

#include<stdio.h>

#include<conio.h>

void main()

{

int a[25],i,j,t;

clrscr();

printf("Enter the Elements of the array\n");

for(i=0;i<=24;i++)

            {

            printf("%d\t",i);

            scanf("%d",&a[i]);

            }

for(i=0;i<=23;i++)

            {

            for(j=i+1;j<=24;j++)

                        {

                        if(a[i]>a[j])

                                    {

                                    t=a[i];

                                    a[i]=a[j];

                                    a[j]=t;

                                    }

                        }

            }

printf("\n Sorted Numbers are \n");

for(i=0;i<=24;i++)

            {

            printf("%d  ",a[i]);

            }

printf("\n\n Press Any Key to Exit");

getch();

}

 

/*Programs to make an exchange sort of an array*/

 

#include<stdio.h>

#include<conio.h>

void main()

{

int a[25],i,j,m,t;

clrscr();

printf("Enter the Elements of the array\n");

for(i=0;i<=24;i++)

            {

            printf("%d\t",i);

            scanf("%d",&a[i]);

            }

for(i=0;i<=24;i++)

            {

            for(j=0;j<24-i;j++)

                        {

                        if(a[j]>a[j+1])

                                    {

                                    t=a[j];

                                    a[j]=a[j+1];

                                    a[j+1]=t;

                                    }

                        }

            }

printf("\n Sorted Numbers are \n");

for(i=0;i<=24;i++)

            {

            printf("%d  ",a[i]);

            }

printf("\n\n Press Any Key to Exit");

getch();

}

 

//Write a program to add two arrays

 

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10],b[10],c[10],i;

clrscr();

printf("\n 1st Array..............\n\n");

for(i=0;i<10;i++)

            {

            printf("Enter the Element %d   ",i);

            scanf("%d",&a[i]);

            }

printf("\n 2nd Array..............\n\n");

for(i=0;i<10;i++)

            {

            printf("Enter the Element %d   ",i);

            scanf("%d",&b[i]);

            }

 

printf("\n Result Array..............\n\n");

for(i=0;i<10;i++)

            {

            c[i]=a[i]+b[i];

            printf(" %d + %d = %d\n",a[i],b[i],c[i]);

            }

printf("Press Any Key to Exit");

getch();

}

 

// C program to find the largest element in an array

 

#include<stdio.h>

int main(){

  int a[50],size,i,big;

  clrscr();

  printf("\nEnter the size of the array: ");

  scanf("%d",&size);

  printf("\nEnter %d elements in to the array:", size);

  for(i=0;i<size;i++)

      scanf("%d",&a[i]);

  big=a[0];

  for(i=1;i<size;i++)

  {

      if(big<a[i])

           big=a[i];

  }

  printf("\nBiggest: %d ",big);

  printf("Press any key to exit");

  getch();

  return 0;

}

 

 

//C program to find the second largest element in an array

 

#include<stdio.h>

int main()

{

  int a[50],size,i,j=0,big,secondbig;

  clrscr();

  printf("Enter the size of the array: ");

  scanf("%d",&size);

  printf("Enter %d elements in to the array: ", size);

  for(i=0;i<size;i++)

      scanf("%d",&a[i]);

 

  big=a[0];

  for(i=1;i<size;i++)

  {

      if(big<a[i]){

           big=a[i];

           j = i;

      }

  }

 

  secondbig=a[size-j-1];

  for(i=1;i<size;i++)

  {

      if(secondbig <a[i] && j != i)

          secondbig =a[i];

  }

 

  printf("Second biggest: %d", secondbig);

  printf("\n\nPress any key to exit.............");

  getch();

  return 0;

}

 

// C program to find the second smallest element in an array

 

#include<stdio.h>

#include<conio.h>

int main()

{

  int a[50],size,i,j=0,small,secondsmall;

  clrscr();

  printf("Enter the size of the array: ");

  scanf("%d",&size);

  printf("Enter %d elements in to the array: ", size);

  for(i=0;i<size;i++)

         scanf("%d",&a[i]);

 

  small=a[0];

  for(i=1;i<size;i++){

         if(small>a[i]){

               small=a[i];

               j = i;

      }

  }

 

  secondsmall=a[size-j-1];

  for(i=1;i<size;i++){

         if(secondsmall > a[i] && j != i)

              secondsmall =a[i];

  }

 

  printf("Second smallest: %d", secondsmall);

  printf("\nPress any key to exit ...............");

  getch();

  return 0;

}

 

// C code to find largest and smallest number in an array

 

 

#include<stdio.h>

int main()

{

  int a[50],size,i,big,small;

  clrscr();

  printf("\nEnter the size of the array: ");

  scanf("%d",&size);

  printf("\nEnter %d elements in to the array: ", size);

  for(i=0;i<size;i++)

      scanf("%d",&a[i]);

 

  big=a[0];

  for(i=1;i<size;i++)

  {

      if(big<a[i])

           big=a[i];

  }

  printf("\nLargest element: %d",big);

 

  small=a[0];

  for(i=1;i<size;i++)

  {

      if(small>a[i])

           small=a[i];

  }

  printf("\nSmallest element: %d",small);

  printf("\n Press any key to exit");

  getch();

  return 0;

}

 

// Write a program to get the frequency of the number

 

#include<stdio.h>

#include<conio.h>

void main()

{

            int num[10],i,count=0,n;

            clrscr();

            printf("Enter 10 Elements of array");

            for(i=0;i<=9;i++)

                        {

                        printf("\n%d => ",i);

                        scanf("%d",&num[i]);

                        }

            printf("Enter the Element to search");

            scanf("%d",&n);

           

            for(i=0;i<=9;i++)

            {

                        if (num[i]==n)

                                    count++;

            }

            printf("\n The Number %d found %d time(s) in the Array",n,count);

            printf("\n Press Any Key to Exit.......");

            getch();

}

 

 

// Write A Program To Create A Double Dimension Array

 

#include<stdio.h>

#include<conio.h>

void main()

{

int a[3][3]=                  {

                                    1,2,3,

                                    4,5,6,

                                    7,8,9

                                    };

int i,j;

clrscr();

for(i=0;i<3;i++)

            {

            for(j=0;j<3;j++)

                        {

                        printf("Elements in [%d,%d] = %d\n",i,j,a[i][j]);

                        }

            }

printf("\n Press Any Key to Exit");

getch();

}

 

// Write A Program To Accept And Show A Double Dimension Array

 

#include<stdio.h>

#include<conio.h>

void main()

{

int a[3][3];

int i,j;

clrscr();

for(i=0;i<3;i++)

            {

            for(j=0;j<3;j++)

                        {

                        printf("Enter the Element in [%d,%d] = ",i,j);

                        scanf("%d",&a[i][j]);

                        }

            }

for(i=0;i<3;i++)

            {

            for(j=0;j<3;j++)

                        {

                        printf("Elements in [%d,%d] = %d\n",i,j,a[i][j]);

                        }

            }

printf("\n Press Any Key to Exit");

getch();

}

 

// Write a program to accept and show and sum double dimension array

 

#include<stdio.h>

#include<conio.h>

void main()

{

int a[3][3];

int i,j,sum=0;

clrscr();

for(i=0;i<3;i++)

            {

            for(j=0;j<3;j++)

                        {

                        printf("Enter the Element in [%d,%d] = ",i,j);

                        scanf("%d",&a[i][j]);

                        }

            }

for(i=0;i<3;i++)

            {

            for(j=0;j<3;j++)

                        {

                        sum+=a[i][j];

                        printf("Elements in [%d,%d] = %d\n",i,j,a[i][j]);

                        }

            }

printf("\n\nSum of the Elements = %d\n",sum);

printf("\n Press Any Key to Exit");

getch();

}

 

 

 

 

// Write a program to accept and add two double dimension arrays

 

#include<stdio.h>

#include<conio.h>

void main()

{

long int a[3][2],b[3][2],c[3][2];

long int i,j,sum=1;

clrscr();

printf("\n1st Array...........\n\n");

for(i=0;i<3;i++)

            {

            for(j=0;j<2;j++)

                        {

                        printf("Enter the Element in [%ld,%ld] = ",i,j);

                        scanf("%ld",&a[i][j]);

                        }

            }

printf("\n2nd Array..............\n\n");

for(i=0;i<3;i++)

            {

            for(j=0;j<2;j++)

                        {

                        printf("Enter the Element in [%ld,%ld] = ",i,j);

                        scanf("%ld",&b[i][j]);

                        }

            }

printf("\n Result Array...... \n\n");

for(i=0;i<3;i++)

            {

            for(j=0;j<2;j++)

                        {

                        c[i][j]=a[i][j]+b[i][j];

                        printf("Elements in %ld + %ld = %ld\n",a[i][j],b[i][j],c[i][j]);

                        }

            }

printf("\n Press Any Key to Exit");

getch();

}

 

// Write a program to accept and Subtract two double dimension arrays

 

#include<stdio.h>

#include<conio.h>

void main()

{

long int a[3][2],b[3][2],c[3][2];

long int i,j,sum=1;

clrscr();

printf("\n1st Array...........\n\n");

for(i=0;i<3;i++)

            {

            for(j=0;j<2;j++)

                        {

                        printf("Enter the Element in [%ld,%ld] = ",i,j);

                        scanf("%ld",&a[i][j]);

                        }

            }

printf("\n2nd Array..............\n\n");

for(i=0;i<3;i++)

            {

            for(j=0;j<2;j++)

                        {

                        printf("Enter the Element in [%ld,%ld] = ",i,j);

                        scanf("%ld",&b[i][j]);

                        }

            }

printf("\n Result Array...... \n\n");

for(i=0;i<3;i++)

            {

            for(j=0;j<2;j++)

                        {

                        c[i][j]=a[i][j]-b[i][j];

                        printf("Elements in %ld - %ld = %ld\n",a[i][j],b[i][j],c[i][j]);

                        }

            }

printf("\n Press Any Key to Exit");

getch();

}

 

 

 

 

 

 

 

// Multiplication Of Two Matrices Using C Program

 

 #include<stdio.h>

int main()

{

  int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;

  clrscr();

  printf("\nEnter the row and column of first matrix");

  scanf("%d %d",&m,&n);

  printf("\nEnter the row and column of second matrix");

  scanf("%d %d",&o,&p);

  if(n!=o)

            {

      printf("Matrix multiplication is not possible");

      printf("\nColumn of first matrix must be same as row of second matrix");

            }

  else

            {

      printf("\nEnter the First matrix->");

      for(i=0;i<m;i++)

                        for(j=0;j<n;j++)

           scanf("%d",&a[i][j]);

      printf("\nEnter the Second matrix->");

      for(i=0;i<o;i++)

                        for(j=0;j<p;j++)

           scanf("%d",&b[i][j]);

      printf("\nThe First matrix is\n");

      for(i=0;i<m;i++)

                        {

                        printf("\n");

                                    for(j=0;j<n;j++)

                                                {

                                                printf("%d\t",a[i][j]);

                                                }

                        }

      printf("\nThe Second matrix is\n");

      for(i=0;i<o;i++)

                        {

                        printf("\n");

                        for(j=0;j<p;j++)

                                                {

                                                printf("%d\t",b[i][j]);

                                                }      

                        }

      for(i=0;i<m;i++)

      for(j=0;j<p;j++)

           c[i][j]=0;

      for(i=0;i<m;i++)

                        { //row of first matrix

                        for(j=0;j<p;j++)

                                    {  //column of second matrix

           sum=0;

           for(k=0;k<n;k++)

               sum=sum+a[i][k]*b[k][j];

           c[i][j]=sum;

                                    }                      

                        }

  }

  printf("\nThe multiplication of two matrix is\n");

  for(i=0;i<m;i++){

      printf("\n");

      for(j=0;j<p;j++){

           printf("%d\t",c[i][j]);

      }

  }

  printf("\n Press Any Key to Exit .......");

  getch();

  return 0;

}

 

Expalnation:

 

 

 

 

//Write a C program to print the frequency of elements in an array.

 

  #include <stdio.h>

  #define MAX 256

  #define DUP 1

 

  int main()

  {

        int i, j, n, count, arr[MAX][2];

                        clrscr();

        /* get the number of entries from the user */

        printf("Enter the number of array elements:");

        scanf("%d", &n);

 

        /* get the array entries */

        printf("Enter the array entries:\n");

        for (i = 0; i < n; i++)

                        {

                printf("Array[%d]: ", i);

                scanf("%d", &arr[i][0]);

                arr[i][1] = 0;

        }

 

        /* find the frequency for each elements */

        printf("\nElements   Frequency\n");

        for (i = 0; i < n; i++)

                        {

                count = 1;

 

                if (arr[i][1])

                        continue;

 

                for (j = i + 1; j < n; j++) {

                        if (arr[i][0] == arr[j][0]) {

                                arr[j][1] = DUP;

                                count++;

                        }

                }

 

                /* print the result */

                printf("%d     =>    %d\n", arr[i][0], count);

        }

                        printf("\nPress any key to exit......");

                        getch();

        return 0;

  }

 

 

 

No comments:

Post a Comment

JAVA PROGRAMMING - OOPS CHAPTER 1

Object-Oriented Programming Chapter 1: Basics by Souradeep Roy Using IntelliJ IDEA Community version:- _____________________________________...