Saturday, May 22, 2010

Write a C program to generate a bar chart for a list of numbers.?

Write a program to generate a bar chart for a list of numbers, where each number is between -10 and 10. Given a list of numbers, your program needs to first sort the numbers. Your program should then generate the bar chart where the left hand side represents the negative values and the right hand side represents the positive values (see example output below). Your bar chart should only use the *, | and space characters.





Example:


Input: 10 –5 5 –10 0


Sorted list: -10 -5 0 5 10


Output:


Bar Chart








|**********| |


| *****| |


| | |


| |***** |


| |********** |


1 hour ago - 3 days left to answer.

Write a C program to generate a bar chart for a list of numbers.?
I'm not going to write the program for you, luv, but I'll make some suggestions.





You're not dealing with a huge amount of data, so it isn't essential that your sorting algorithm be particularly efficient. I'd suggest a bubble sort - everyone knows how to write it, and the algorithm is easily available. If you want something a touch less common, then go with a bucket sort.





Once you have the numbers sorted, then set up a set of nested loops. Use the outer one to cycle through all of the data values. Use the inner one to construct an output string, using the datum at that level as the sentinal value to terminate a string of asterisks. Putting the pipes at the beginning and end should be child's play. Once the output string has been constructed, display it and terminate the program.





Hope that helps.


No comments:

Post a Comment