Monday 3 October 2016

Printing a Histogram in C Using Functions


This C program scans total number of subjects from the user who's marks are also scanned from the user.The subject marks are to be entered out of 100  and at last it prints a histogram of the marks entered  in various ranges such as 0-9,10-19,20-29,...,so on upto 100.

#include<stdio.h>
#include<conio.h>
void m (int,int*,int,int);
void main()
{
 clrscr();
 int a[100],s,i,j,k;

 //s stores total no. of data values entered.
 //j is lower bound and k is upper bound of any range in histogram.
 //a array stores the marks entered while scanning.
 //i is for the counter.

 printf("Enter no. of marks to be entered:");
 scanf("%d",&s);
 printf("\n \n Enter marks of %d students out of 100:\n " , s);


  for(i=0; i<s; i++)
  {      printf("\n enter:");
  scanf("%d", &a[i]);

  }


 //calling functions via a loop
 j=0;
 k=9;
 for(i=0; i<=10; i++)
 {
  m(s,a,j,k);
  j=k+1;   //increment the range
  k=j+9;
 }
 printf("\n Visit www.EngineersBurger.com for more such programs!");
 getch();
}

void m(int s, int *a, int j, int k)
{
 int i;
 if(j==100)
 printf("\n %3d - %3d:\t\t",j,j);
 else
 printf("\n %3d - %3d:\t\t", j,k);

 //logic for printing *
 for(i=0; i<s; i++)
 {
  if( a[i] >= j && a[i] <= k )
  {
   printf("*");
  }
 }

}

0 comments:

Post a Comment

Have A Wonderful Day !

--------------------------------------------------------------------

Word Of The Day | Learn And Use A New Word Everyday!

--------------------------------------------------------------------

"Insidious"

Working or spreading in a hidden and usually injurious way

"glaucoma is an insidious disease"

Copyright © 2016 Engineers Burger - All Rights Reserved. Digital Craftmanship - Nikhil Jha and Jay Akbari.
MyBloggerLab.com