Answer to Question #250804 in C++ for Hazzy

Question #250804

Write a C program that read numbers from an integer array and graph the information in the form of bar chat. Sample output is given below.

Element Value Histogram

0 19 *******************

1 3 ***

2 15 ***************

3 7 *******

4 11 ***********

5 9 *********

6 13 *************

7 5 *****

8 17 *****************

9 1 *


1
Expert's answer
2021-10-13T20:38:44-0400

Source code

#include <stdio.h>
void printStar(int n){
    for(int i=0;i<n;i++){
        printf("*");
    }
}
void main()
{
    int array1[100];
    int frequency[100];
    int num, i, j, count;
	printf("Enter the number of elements to be stored in the array :");
       scanf("%d",&num);
   
       printf("Input %d elements in the array :\n",num);
       for(i=0;i<num;i++)
            {
    	      scanf("%d",&array1[i]);
    		  frequency[i] = -1;
	    }
    for(i=0; i<num; i++)
    {
        count = 1;
        for(j=i+1; j<num; j++)
        {
            if(array1[i]==array1[j])
            {
                count++;
                frequency[j] = 0;
            }
        }


        if(frequency[i]!=0)
        {
            frequency[i] = count;
        }
    }
    printf("\nElement \tValue \tHistogram: \n");
    for(i=0; i<num; i++)
    {
        if(frequency[i]!=0)
        {
            printf("%d \t%d \t", array1[i], frequency[i]);
            printStar(frequency[i]);
            printf("\n");
        }
    }
}


Output





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS