Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts
C PROGRAMS : ARRAYS

C PROGRAMS : ARRAYS

C PROGRAMS : ARRAYS
16. Programs to multiply two Matrices
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{
 // Declaring variable a, b, c with max size=10 X 10
 int a[10][10],b[10][10],c[10][10];
 
 // Declaring variable m, n, p, q = size of matrix entered by user
 int m, n, p, q;
 
 // Declaring variable i, j, k = to iterate loop
 int i, j, k;

 // Inputing size of first matrix
 printf("Enter the size of first matrices : \n");
 
 printf("Enter number of rows : ");
 scanf("%d",&m);
 
 printf("Enter number of columns : ");
 scanf("%d",&n);
 
 // Inputing size of second matrix
 printf("Enter the size of second matrices : \n");
 
 printf("Enter number of rows : ");
 scanf("%d",&p);
 
 printf("Enter number of columns : ");
 scanf("%d",&q);


 // Checking if multiplication is possible or not if possible then proceed
 if(n==p)
 {
 // Inputing first matrx elemets
 printf("Enter first matrices elements : ");
 
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }
 
 // Inputing second matrx elemets
 printf("Enter second matrix elements : ");
 
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  {
   scanf("%d",&b[i][j]);
  }
 }
 
 // Performing multiplication
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  {
   c[i][j]=0;
   for(k=0;k<n;k++)
   {
   c[i][j]=c[i][j]+a[i][k]*b[k][j];
   }
  }
 }
 
 // Printing multiplied matrix
 printf("The multiplication of Matrices : \n"); 
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  {
  printf("%d ",c[i][j]);
  }
  
  printf("\n");
 }
}

 else
 printf("Multiplication is not possible");

 getch(); // Linux user - Remove this

 return 0;
}

17. Program to print a diagonal matrix with diagonal value enter by user
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variale a with size = 4 X 4
 int a[4][4],i,j;
 
 // Declaring varibale d = diagonal value by user
 int d;
 
 // Inputing diagonal value
 printf("Enter diagonal value : ");
 scanf("%d",&d);
 
 // Setting diagonal value as "d" and all other value as "0"
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   if(i==j)
   a[i][j]=d;
   else
   a[i][j]=0;
  }
 }
 
 // Printing diagonal matrix
 for(i=0;i<4;i++)
 {
 
  for(j=0;j<4;j++)
  printf("%2d",a[i][j]);
  printf("\n");
 }
  
 
 getch(); // Linux user - Remove this

 return 0;
}

18. Program to print a anti diagonal matrix with diagonal value enter by user
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variale a with size = 4 X 4
 int a[4][4],i,j;
 
 // Declaring varibale d = diagonal value by user
 int d;
 
 // Inputing diagonal value
 printf("Enter diagonal value : ");
 scanf("%d",&d);
 
 // Setting diagonal value as "d" and all other value as "0"
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   if(i+j==3)
   a[i][j]=d;
   else
   a[i][j]=0;
  }
 }
 
 // Printing anti diagonal matrix
 for(i=0;i<4;i++)
 {
 
  for(j=0;j<4;j++)
  printf("%2d",a[i][j]);
  printf("\n");
 }
  
 getch(); // Linux user - Remove this

 return 0;
}

19. Program to print the sum of diagonal values and anti-diagonal values of a matrix
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring 2D array
 int a[4][4];
 
 // Declaring variable i, j to iterate loop
 int i, j;
 
 // Declaring variabl sd=to store sum of diagonal value, sa=store sum of anti diagonal value
 int sd=0, sa=0; 
 
 // Inputing matrix
 printf("Enter matrix element of size 4 X 4 : ");
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }
 
 // Calculating sum of diagonal values and anti diagonal values
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   if(i==j)
   sd=sd+a[i][j];
   
   if(i+j==3)
   sa=sa+a[i][j];
  }
 }

 // Printing element of matrix 
 printf("\nEntered elements in matrix : \n");
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   printf("%d ",a[i][j]);
  }
  printf("\n");
 }

 // Printing sum and diagonal of matrix
 printf("\nSum of diagonal values : %d ",sd);
 printf("\nSum of anti diagonal values : %d ",sa);
 
 getch(); // Linux user - Remove this

 return 0;
}
C PROGRAMS : ARRAYS

C PROGRAMS : ARRAYS

C PROGRAMS : ARRAYS
11. Program to generate histogram of entered number
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declarring array to enter elements
 int a[10];
 
 // Declaring variable "i" and "j" to iterate loop
 int i, j;
 
 // Inputting 10 numbers
 printf("Enter 10 numbers :\n");
 for(i=0;i<10;i++)
 {
  scanf("%d",&a[i]);
 }
 
 // Generating histogram
 printf("%s %12s %15s\n","Element", "Value", "Hisogram");
 for(i=0;i<10;i++)
 {
  printf("%4d %12d           ",i, a[i]);
  for(j=0;j<a[i]; j++)
  {
   printf("*");
  }
  printf("\n");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

12. Program to enter values into a two-dimensional integer array of size (4 X 3) and then display it in matrix form
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable info with row size = 4 and column size = 3
 int info[4][3];
 
 // Declaring variable "i" and "j" to iterate loop
 int i, j;
 
 // Inputting value
 
 for(i=0;i<4;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("Enter value of info[%d][%d] : ",i ,j);
   scanf("%d",&info[i][j]);
  }
 }
 
 // Diplaying elements
 printf("Elements elements in matrix form :\n");
 for(i=0;i<4;i++)
 {
 for(j=0;j<3;j++)
 {
  printf("%d ",info[i][j]);
 }
 printf("\n");
 }
 getch(); // Linux user - Remove this

 return 0;
}

13. Program to enter values into a two-dimensional integer array of size (4 X 3) and then display the elements of the second row
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable info with row size = 4 and column size = 3
 int info[4][3];
 
 // Declaring variable "i" and "j" to iterate loop
 int i, j;
 
 // Inputting value
 
 for(i=0;i<4;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("Enter value of info[%d][%d] : ",i ,j);
   scanf("%d",&info[i][j]);
  }
 }
 
 // Diplaying elements of second row
 printf("Elements of second row : ");
 for(j=0;j<3;j++)
 {
  printf("%d ",info[1][j]);
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

14. Program to find the sum of elements that are greater than 5 within a two-dimensional array through a function receiving the array as arguments
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

// funciton prototype
int sum(int[][4], int, int);

int main()
{
 // Declaring variable a with row size=2 and column size=4
 int a[2][4];
 
 // Declaring variable "i" and "j" to iterate loop for rows and columns
 int i, j;
 
 // Declaring variable result = to hold the returned result 
 int result=0;
 
 // Inputting elements
 for(i=0;i<2;i++)
 {
  for(j=0;j<4;j++)
  {
   printf("Enter value of a[%d][%d] : ",i ,j);
   scanf("%d",&a[i][j]);
  }
 }
 
 // Calling function sum 
 result=sum(a, 2, 4);
 
 // Printing returned result
 printf("Sum of elements > 5 in an array = %d ",result);
 
 getch(); // Linux user - Remove this

 return 0;
}

int sum(int p[][4], int m, int n) // m is row size, n is column size
{
 // Declaring variable s=sum to hold the sum
 int s=0;
 
 // Declaring variable "i" and "j" to iterate loop for rows and columns
 int i, j;
 
 // Calculating sum
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  {
   // Checking whether element is greater than 5
   if(p[i][j]>5)
   {
    s=s+p[i][j];
   }
  }
 } 
 
 // Returning sum
 return s;
}

15. Program To Accept 5 Student - Roll No, Marks in 3 Subjects of each student and Calculate Total, Average and Print it along with student roll Number
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring rn=roll no
 int rn[5];
 
 // Declaring 2D array 
 int marks[5][3];
 
 // Declaring variable "i" and "j" to iterate loop
 int i,j;
 
 // Declaring variable s=sum and avg=average
 int s[5]={0}, avg[5]={0};

 // Inputting name, roll no and marks
 for(i=0;i<5;i++)
 {
  printf("\nEnter %d student Roll no : ",i+1);
  scanf("%d",&rn[i]); 
  
  // Inputing stuent marks
  for(j=0;j<3;j++)
  {
   printf("Enter Marks of %d subject : ",j+1 );
   scanf("%d",&marks[i][j]); 
  }
 }
 
 // Calculating total and average
 for(i=0;i<5;i++)
 {
  for(j=0;j<3;j++)
  {
   s[i]=s[i]+marks[i][j];
  }
  
  avg[i]=s[i]/3;
 }
 
 // Printing details
 printf("\nDetails of student : \n");
 for(i=0;i<5;i++)
 {
  printf("\nStudent Roll Number : %d",rn[i]);
  printf("\nStudent Total marks = %d, Average marks = %d\n",s[i],avg[i]);
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : ARRAYS

C PROGRAMS : ARRAYS

C PROGRAM : Arrays
6. Program to find Minimum element in an array
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable a with size five to store five elements
 int a[5];
 
 // Declaring variable "i" to iterate loop
 int i;
 
 // Declaring variable min=minimum
 int min;
 
 // Inputting Element of an array
 printf("Enter five element in an array : \n");
 for(i=0;i<5;i++)
 {
  scanf("%d",&a[i]);
 }
 
 // Initialising min with first element assuming it to be minimum
 min=a[0];
 
 // Determining minimum element
 for(i=0;i<5;i++)
 {
 if(min>a[i+1])
 min=a[i+1];
 }
 
 // Displaying minimum element
 printf("Minimum Element = %d",min);
 
 getch(); // Linux user - Remove this

 return 0;
}

7. Program to find highest minimum(-) temperature and lowest maximum(+) temperature
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 int i,j;
 int counternegative=0, counterpositive=0;
 int temp[12];
 int max=0;
 int min=0;
 printf("Enter temperature of 12 months : " );
 
 for(i=0;i<12;i++)
 {
  scanf("%d",&temp[i]); 
 }
 
 for(i=0;i<12;i++)
 {
  if(temp[i]>0 && counterpositive==0)
  {
  min=temp[i];
  counterpositive=1;
  }
  
  else if(temp[i]<0 && counternegative==0)
  {
  max=temp[i];
  counternegative=1;
  }
 }
 
 for(i=0;i<12;i++)
 {
    if(temp[i]<0)
    {
     if(max<temp[i])
     {
     max=temp[i];
     }
    }
    
    else if(temp[i]>0)
    {
     if(min>temp[i])
     {
      min=temp[i];
     }
    }
  }
 
 printf("Max negative temperature : %d \n",max);
 printf("Min positive temperature : %d ",min);
 
 getch(); // Linux user - Remove this

 return 0;
}

8. Program to accept 10 numbers and print first five numbers in original order and print last five numbers in reverse order
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{ 

// Declaring variabel i = for iteration of loop
int i,a[10];

// Inputting elements
printf("Enter 10 elements : ");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}

// Printing first 5 numbers in original order
printf("Printing first 5 numbers in original order : ");
for(i=0;i<=4;i++)
printf("%d ",a[i]);

// Printing last five numbers in reverse order
printf("\nPrinting last 5 numbers in reverse order : ");
for(i=9;i>=5;i--)
printf("%d ",a[i]);

getch( ); // Linux user - Remove this

return 0;
}


9. Program showing Passing array to function. Program finds the average of 5 marks input by user
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

/* We have declared return type as float since value to be returned 
i.e "average" is a float value. */
float avg(float x[5])
{
      // Declaring variable sum to hold the sum of five elements entered
   float sum=0;
   
   // Declaring variable "i" to iterate loop
   int i;
   
   // Calculating sum 
      for(i=0;i<5;i++)
      {
              sum=sum+x[i];
      }
      
      // Returning average i.e sum/5
      return sum/5;
}
int main()
{
    // Declaring array variale marks with maximum size "5"
    float marks[5];
    
     // Declaring variable "i" to iterate loop
     int i;
    
    // Inputting elements or marks
    printf("Enter marks for five subject : ");
    for(i=0;i<5;i++)
    {
            scanf("%f",&marks[i]);
    }
 
 // Calling function and printing the returned resutl ( i.e average )
 printf("Average marks = %f",avg(marks));

 getch(); // Linux user - Remove this

 return 0;
}

10. Program to initialize a character array and display the it in reverse order
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable names of size 5 and initializing it
 char names[5]={'h','e','l','l','o'};
 
 // Declaring variable "i" to iterate loop
        int i;
    
        // Printing in reverse order
        printf("Initialized characters in reverse order : ");
  for(i=4;i>=0;i--)
  {
  printf("%c ",names[i]);
 }

 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : ARRAYS

C PROGRAMS : ARRAYS

C PROGRAMS : Array
1. Program to accept elements in an array and display it
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
    // Declaring array variable with max size=10
    int a[10];

   // Declaring variable i = to iterate loop
   int i;

   // Inputting elements
   printf("Enter 10 elements in an array : ");
   for(i=0; i<10; i++)
   {
        scanf("%d",&a[i]);
   }

   // Displaying entered elements
   printf("Entered elements are : ");
    for(i=0; i<10; i++)
   {
        printf("%d ",a[i]);
   }

   // to hold the screen defined in conio.h   
   getch(); // Linux user - Remove this

   return 0;
}

2. Program to accept elements in an array and display sum of all the elements
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
    // Declaring array variable with size=10
    int a[10]; 

   // Declaring variable s=sum to store the sum, i = to iterate loop
   int i, s=0;

   // Inputting elements
   printf("Enter 10 elements in an array : ");
   for(i=0; i<10; i++)
   {
        scanf("%d",&a[i]);
   }

   // Displaying entered elements
 printf("Entered elements are : ");
    for(i=0; i<10; i++)
   {
        printf("%d ",a[i]);
   }

   // Performing sum of all elements
    for(i=0; i<10; i++)
   {
        s = s + a[i];
   }

   // Displaying result
   printf("\nSum of all elements  :  %d ",s);

   getch(); // Linux user - Remove this

   return 0;
}

3. Program to insert element in between an array. ( INSERTION )
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable "i" to iterate loop
int i;

// Declaring variable pos=postion from where we want to delete element
int pos;

// Declaring n=number which we want to insert and "m" to store "n"
int n,m;

// Declaring and initialising array
int a[6]={2,4,6,8,10};

// Printing values in array before insertion
printf("Elements before deletion\n");
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
printf("\n");

// Inputting position
printf("Enter the position of element for insertion : ");
scanf("%d",&pos);

// Inputting value
printf("Enter the value which you want to insert : ");
scanf("%d",&n);
m=n;

// Performing insertion
for(i=5;i>=pos-1;i--)
{
a[i+1]=a[i];
}
a[pos-1]=m;

// Printing elements after insertion
printf("Elements after insertion\n");
for(i=0;i<6;i++)
{
printf("%d ",a[i]);
}

getch(); // Linux user - Remove this

return 0;
}


4. Program to delete element from an array. ( DELETION )
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable "i" to iterate loop
int i;

// Declaring variable pos=postion from where we want to delete element
int pos;

// Declaring and initialising array
int a[5]={2,4,6,8,10};

// Printing values in array before deletion
printf("Elements before deletion\n");
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
printf("\n");

// Inputting position
printf("Enter the position of element for deletion\n");
scanf("%d",&pos);

// Performing deletion
for(i=pos-1;i<5;i++)
{
a[i]=a[i+1];
}

// Printing elements after deletion
printf("Elements after deletion\n");
for(i=0;i<4;i++)
{
printf("%d ",a[i]);
}

getch(); // Linux user - Remove this

return 0;
}


5. Program to perform Transpose of a Matrix
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{

// Declaring variable i,j to iterate loop 
int i,j;

// Declaring variable a to store matrix element
int a[2][2];

// Inserting element in Matrix
printf("Enter 2X2 matrix\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}

// Displaying Matrix
for(i=0;i<2;i++)
{
 printf("\n");
 for(j=0;j<2;j++)
 {
  printf("%d ",a[i][j]);
 }
}

// Performing transpose of Matrix and displaying transposed Matrix
printf("\n");
printf("Transpose of matrix is \n");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("%d ",a[j][i]);
}
}

getch(); // Linux user - Remove this

return 0;
}
COMMON PROGRAMMING ERRORS - ARRAY

COMMON PROGRAMMING ERRORS - ARRAY

COMMON PROGRAMMING ERRORS - ARRAY
  • Declaring an array without specifying any value as size of array.
  • Declaring an array taking variable n as size of array. C does not allow a variable length array.
  • Initializing more values than the specified size.
  • Accessing array elements beyond the range limits. 
  • Array elements are to be used from 0 to maxsize-1. 
  • C does not report any error if user tries to access elements beyond this range but some garbage value is printed.
  • Declaring array of type void.