This program must be in C; This program will “draw” an ASCII histogram for 4 dat
ID: 669096 • Letter: T
Question
This program must be in C;
This program will “draw” an ASCII histogram for 4 data values A, B, C and D (read from the input)
Example: if A=3, B=7, C=2 and D=5, the output should look like
this:
X
X
X X
X X
X X X
X X X X
X X X X
---------------
A B C D
When run, the program reads four integers from the input once for each of A..D (example below).
If any of the values is negative, the program prints an error message and terminates.
Otherwise, the program prints the four values and then prints a nice ASCII histogram of the data and
terminate.
A: 3
B: 7
C: 2
D: 5
X
X
X X
X X
X X X
X X X X
X X X X
---------------
A B C D
Explanation / Answer
The C code for your program requirement is:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int A,B,C,D,i,large;
printf("Enter 4 data values: ");
scanf("%i%i%i%i",&A,&B,&C,&D);
if(A < 0 || B < 0 || C < 0 || D < 0)
{
printf("Sorry. Histogram cannot be generated for negative values.");
return 0;
}
large = largefinder(A,B,C,D);
printf("A: %i B: %i C: %i D: %i ",A,B,C,D);
for(i=large;i>0;i--)
{
if(A == i)
{
printf("X ");
A--;
}
else
printf(" ");
if(B == i)
{
printf("X ");
B--;
}
else
printf(" ");
if(C == i)
{
printf("X ");
C--;
}
else
printf(" ");
if(D == i)
{
printf("X ");
D--;
}
else
printf(" ");
printf(" ");
}
printf("-- -- -- A B C D ");
}
int largefinder(int A, int B, int C, int D)
{
if(A >= B && A >= C && A >= D)
return A;
else if(B >= A && B >= C && B >= D)
return B;
else if(C >= A && C >= B && C >= D)
return C;
else
return D;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.