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;
}

Share this

Related Posts

Previous
Next Post »