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

Yeah that sounds logical.

``````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.

sorry it is

``if(check>19)``
Question Answered as of 6 Years Ago by Majestics and iamthwee

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

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: