C Program - Count the occurrence of numbers using array
#include<stdio.h>
#define RESPONSE_SIZE 100 // Actual number of times.
#define FREQUENCY_SIZE 101 // Integers value from 0 to 100
int main()
{
size_t answer; // counter the loop through 100 times
size_t rating; // counter the loop through frequency 1 to 100.
int i = 1; // i is for loop to check that if 0 comes stop inputing
//Initializing the arrays to 0.
int frequency[FREQUENCY_SIZE] = { 0 };
int responses[RESPONSE_SIZE] = { 0 };
printf("Enter the Integer between 1 and 100 = ");
scanf("%d", &responses[0]);
while (responses[i - 1] != 0 && i<100)
{
scanf("%d", &responses[i]);
i++;
}
// for each answer, use the value as a array subscript
//to determine the element to increment
for (answer = 0; answer < RESPONSE_SIZE; ++answer)
{
++frequency[responses[answer]];
}
// to print the output of the frequencies
// if the number of rating for certain integer is 0
// it ignores that value and print other values
for (rating = 1; rating < FREQUENCY_SIZE; ++rating)
{
if (frequency[rating] != 0)
{
printf("%d occurs %d times.\n", rating, frequency[rating]);
}
}
getch();
}
#define RESPONSE_SIZE 100 // Actual number of times.
#define FREQUENCY_SIZE 101 // Integers value from 0 to 100
int main()
{
size_t answer; // counter the loop through 100 times
size_t rating; // counter the loop through frequency 1 to 100.
int i = 1; // i is for loop to check that if 0 comes stop inputing
//Initializing the arrays to 0.
int frequency[FREQUENCY_SIZE] = { 0 };
int responses[RESPONSE_SIZE] = { 0 };
printf("Enter the Integer between 1 and 100 = ");
scanf("%d", &responses[0]);
while (responses[i - 1] != 0 && i<100)
{
scanf("%d", &responses[i]);
i++;
}
// for each answer, use the value as a array subscript
//to determine the element to increment
for (answer = 0; answer < RESPONSE_SIZE; ++answer)
{
++frequency[responses[answer]];
}
// to print the output of the frequencies
// if the number of rating for certain integer is 0
// it ignores that value and print other values
for (rating = 1; rating < FREQUENCY_SIZE; ++rating)
{
if (frequency[rating] != 0)
{
printf("%d occurs %d times.\n", rating, frequency[rating]);
}
}
getch();
}
Comments
Post a Comment