1,105,402 Community Members

Frequency Array and Histogram

Member Avatar
noble3ad
Newbie Poster
13 posts since Feb 2008
Reputation Points: 24 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello all,

I have written a program that stores randomly generated numbers between 0 and 19 into an array of 100 elements. I create a frequency array of 20 elements that shows the frequency of each number and print it's histogram. Now, I'm suppose to increase the frequency array by one element. That last element in the frequency array is suppose to keep track of how many numbers were generated that are outside the data range of 0 to 19. The changes I suspect I have to make are in italics.

The code is as follows:

#include <stdio.h>
#include <stdlib.h>

#define NUM_RANGE	20  //[I]#define NUM_RANGE 21[/I]
#define	MAX_NUMBERS	100

void BuildFreqArray(int nums[], int freq[], int arry_size);
void PrintHistogram(int freq[], int arry_size);
void getData(int nums[], int arry_size);

int main()
{
	int freq_array[NUM_RANGE] = {0};
	int numbers[MAX_NUMBERS];

	getData(numbers, MAX_NUMBERS);
	BuildFreqArray(numbers, freq_array, MAX_NUMBERS);
	PrintHistogram(freq_array, NUM_RANGE);

	return 0;
}

void BuildFreqArray(int nums[], int freq[], int arry_size)
{
	int i;

	for(i=0; i<arry_size; i++)
	{
		freq[nums[i]]++;
	}

	return;
}

void PrintHistogram(int freq[], int arry_size)
{
	int i, j;

	printf("our histogram:\n");

	for(i=0; i<arry_size; i++)
	{
		printf("%2d %2d:\t",i, freq[i]);

		for(j=0; j<freq[i]; j++)
		{
			printf("*");
		}

		printf("\n");
	}

	return;
}

void getData(int nums[], int arry_size)
{
	int i;

	//instead of reading data from a file, we will just generate
	//numbers in the range of 0 and 19

	srand(626);

	for(i=0; i<arry_size; i++)
	{
		nums[i] = rand()%20; // [I]nums[i] = rand () % 30 --change will generate numbers beyond 19[/I]
	}

	return;
}

How would I keep track of the numbers generated beyond 19? I think I have to include an "if" statement within the PrintHistogram function....something like "if(freq > 19)"..... Any ideas on how to approach this would be greatly appreciated.

Thanks

Member Avatar
iamthwee
Posting Sage
7,081 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 596 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

Yeah that sounds logical.

Member Avatar
Majestics
Practically a Master Poster
694 posts since Jul 2007
Reputation Points: 84 [?]
Q&As Helped to Solve: 66 [?]
Skill Endorsements: 6 [?]
 
0
 
for(i=0; i<arry_size; i++)
	{
            int check=rand()4%20;
             if(check!=19)            
 	nums[i] = check; 
	}

I dont got your point clearly. May be it is helpful to you.

Member Avatar
Majestics
Practically a Master Poster
694 posts since Jul 2007
Reputation Points: 84 [?]
Q&As Helped to Solve: 66 [?]
Skill Endorsements: 6 [?]
 
0
 

sorry it is

if(check>19)
Question Answered as of 6 Years Ago by Majestics and iamthwee
Member Avatar
mevanga
Newbie Poster
15 posts since May 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

What if I want the user to input the data instead of just generating them randomly?


Hello all,

I have written a program that stores randomly generated numbers between 0 and 19 into an array of 100 elements. I create a frequency array of 20 elements that shows the frequency of each number and print it's histogram. Now, I'm suppose to increase the frequency array by one element. That last element in the frequency array is suppose to keep track of how many numbers were generated that are outside the data range of 0 to 19. The changes I suspect I have to make are in italics.

The code is as follows:

#include <stdio.h>
#include <stdlib.h>

#define NUM_RANGE	20  //[I]#define NUM_RANGE 21[/I]
#define	MAX_NUMBERS	100

void BuildFreqArray(int nums[], int freq[], int arry_size);
void PrintHistogram(int freq[], int arry_size);
void getData(int nums[], int arry_size);

int main()
{
	int freq_array[NUM_RANGE] = {0};
	int numbers[MAX_NUMBERS];

	getData(numbers, MAX_NUMBERS);
	BuildFreqArray(numbers, freq_array, MAX_NUMBERS);
	PrintHistogram(freq_array, NUM_RANGE);

	return 0;
}

void BuildFreqArray(int nums[], int freq[], int arry_size)
{
	int i;

	for(i=0; i<arry_size; i++)
	{
		freq[nums[i]]++;
	}

	return;
}

void PrintHistogram(int freq[], int arry_size)
{
	int i, j;

	printf("our histogram:\n");

	for(i=0; i<arry_size; i++)
	{
		printf("%2d %2d:\t",i, freq[i]);

		for(j=0; j<freq[i]; j++)
		{
			printf("*");
		}

		printf("\n");
	}

	return;
}

void getData(int nums[], int arry_size)
{
	int i;

	//instead of reading data from a file, we will just generate
	//numbers in the range of 0 and 19

	srand(626);

	for(i=0; i<arry_size; i++)
	{
		nums[i] = rand()%20; // [I]nums[i] = rand () % 30 --change will generate numbers beyond 19[/I]
	}

	return;
}

How would I keep track of the numbers generated beyond 19? I think I have to include an "if" statement within the PrintHistogram function....something like "if(freq > 19)"..... Any ideas on how to approach this would be greatly appreciated.

Thanks

Member Avatar
mevanga
Newbie Poster
15 posts since May 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

What if I want the user to input the data instead of just generating them randomly?

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article