how would I fill a hist array with a histogram based of the length of the strings.

I need to write a function that takes in a data set of string names a hist array and the size that are already created to form a histogram array that I can use to display in an output file.


It will fill the hist array with a histogram based of the length of the strings.
Example:
Given matt, bob, erik, sue, arthur, (in the **strings which is already sorted in another function) I will see this hist array will be filled such
as:
.......0 . 1 . 2 .3 .4 ..5 .6.... 24 (dots are for formatting)
hist[0][0][0][1][2][0][1]…..[0]


The prototype will look like this
 
void histogram(char **strings, int *hist, int size);



output will print

Number of names with 3 characters: **************** 
Number of names with 4 characters: ********** 
Number of names with 5 characters: ******** 
Number of names with 6 characters: ****** 
Number of names with 7 characters: ******* 
Number of names with 9 characters: * 


any suggestions on how to get started?


what im thinking is something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void histogram(char **strings, int *hist, int size)
{
int i,j;
int counter;

for(i=0;i<size;i++){
counter = 0;
for(j=0;j<size;j++){
if(strlen(strings[i])==j){
counter++
}
hist[i]=counter;
}


}



then for the output

1
2
3
4
5
6
histoogram(strings,hist,size);

for((i=0;i<size;i++){
fprintf(output_file,"numbers of names with %d",i);
for(j=0j<hist[i];j++){
fprintf(output_file,"*");
Last edited on
Topic archived. No new replies allowed.