I wrote a program which gets the number of random values from the user, which are ranged 1-100
. for example, if the user enters "5
", then the output is 5
numbers from 0
to 100
.
So far so good, now I want to sort every age with the function switch / cases, and the sorting will be for every 10 years. every match will get '*
'.
for example if I got 8 ages, 55, 35, 36 ,22, 25, 40, 60, 90
then:
The output will be:
21-30 **
31-40 ***
51-60 **
81-90 *
The problem is, I'm not sure how to write the switch function properly. I know I can solve it easily with if's, but the point here is to train my understanding with switch/case function. Any help would be much appreciated.
Code Updated: (without switch, because switch is not suitable for this problem.)
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#define N 10
int main()
{
int num = 0, i, arr1[N] = { 0 }, temp = 0,j;
srand(time(NULL));
printf("Please emter the number of values: ");
scanf("%d", &num);
for (i = 0; i < num; i++)
{
temp = rand() % 100 + 1;
if (temp == 100)
arr1[9]++;
else if (temp > 0 && temp < 10)
arr1[0]++;
else
arr1[temp / 10]++;
printf("%d ", temp);
}
for (i = 0; i < N; i++)
{
if (arr1[i] == 0) {
printf("\n%d-%d ", (i * 10 + 1), (i + 1) * 10);
}
int add = 0;
if (i > 0) {
add = i;
}
if (arr1[i] != 0)
printf("\n%d-%d ", (i * 10 + 1), (i + 1) * 10);
for(j = 0; j < arr1[i]; j++)
printf("*");
}
getch();
}