C Program To Display Barchart

#include           <stdio.h>
#include           <stdlib.h>
#include           <float.h>
#include           <graphics.h>
#include           <math.h>
#include           <conio.h>
#define            MAX        50
#define            SIZE   5

void barchart(float p[]);

void main(void)
{
  int              i;
  int              scores[SIZE];
  float            percents[SIZE];

  for (i = 0; i < SIZE; i++)
    {
    printf("\nEnter score between 0 and %d:  ", MAX);
    scanf("%d", &scores[i]);
    }
  for (i = 0; i < SIZE; i++)
    percents[i] = ((float) scores[i]) / MAX;

  printf("\n\n\n\tSCORE\tPERCENT");
  for (i = 0; i < SIZE; i++)
    printf("\n%d. \t%d\t%3.0f", i + 1, scores[i], (percents[i] * 100));
  getch();
  barchart(percents);
}

void barchart(float p[])
{
  int   g_driver, g_mode;
  int   i, left, top, wide, bottom, deep;
  detectgraph(&g_driver, &g_mode);
  initgraph(&g_driver, &g_mode, "..\\bgi");
  wide = (int)((getmaxx()) / ((SIZE * 2 ) + 1));
  bottom = getmaxy() - 20;
  deep = (int) (wide / 4);
  left = wide;
  for (i = 0; i < SIZE; i++)
    {
    top = (bottom) - ((int)(p[i] * 300));
    setcolor(5);
    bar3d(left, top, (left + wide), bottom, deep, 1);
    left += (wide * 2);
    }
  getch();
  closegraph();
  return;
}

Sample Input :
Enter score between 0 and 50:  5
Enter score between 0 and 50:  6
Enter score between 0 and 50:  45
Enter score between 0 and 50:  3
Enter score between 0 and 50:  1

        SCORE   PERCENT
1.      5        10
2.      6        12
3.      45       90
4.      3         6
5.      1         2

Share:

Total Pageviews

CONTACT US

Prof. Roshan P. Helonde
Mobile: +917276355704
WhatsApp: +917276355704
Email: roshanphelonde@rediffmail.com

Enter Project Title

Popular Projects

All Archive

Contact Form

Name

Email *

Message *