-1

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();
}
Community
  • 1
  • 1
Ilan Aizelman WS
  • 1,630
  • 2
  • 21
  • 44
  • [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Dec 27 '15 at 12:54
  • 1
    Note: `switch(*arr)` at this point will be out of bound. – Sourav Ghosh Dec 27 '15 at 12:55
  • @SouravGhosh That's true. Well, maybe I need to put that switch in a for loop? Is this even possible? and use arr = arr - num before the switch so it wont be out of bound. – Ilan Aizelman WS Dec 27 '15 at 12:57
  • I guess `case 0 ... 10 :` (GCC extension) is better than `case 0-10:` (it means `case -10:`) – MikeCAT Dec 27 '15 at 12:58
  • you check return of `malloc` _good_ but then you carry on with your program after printing message . Whats the use of checking then ? – ameyCU Dec 27 '15 at 12:59

6 Answers6

2

You can count the number of occurrences of the possible range of a number by doing:

int count[10] = {0}; // Remember to set the values of each element of this array to 0

// ...a few lines later...

int val = 0;
for (int i = 0; i < num; i++) {
    val = arr[i];
    if (val == 10 || val == 20 || val == 30 ||
        val == 40 || val == 50 || val == 60 ||
        val == 70 || val == 80 || val == 90 ||
        val == 100) {
        val--;
    }

    val = val / 10;

    count[val]++;
}

To print the *, you could do:

for (int i = 0; i < 10; i++) {
    if (count[i] == 0) {
        continue;
    }

    int add = 0;
    if (i > 0) {
        add = i;
    }
    printf("%d-%d ", ((i + 1) * 10) + add, (i + 1) * 10);

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

    printf("\n");
}

NOTE: The code above assumes that your count[] array contains 10 elements.

Sean Francis N. Ballais
  • 2,338
  • 2
  • 24
  • 42
1

You don't need switch for this.

Create an int-array with 10 items initialized to 0. In your loop do something like count_arr[number % 10]++;

And make sure you check your ranges (number) first.

Also, you don't need to use malloc() for your array. But if you do you need to free() it too.

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
  • Thank you! I've updated my code in the post. All I've got to do is to print ' * ' instead of numbers. Do you have any ideas how to accomplish that? – Ilan Aizelman WS Dec 27 '15 at 13:29
  • Use 2 loops for that: one to print the '11-20's, and inside that loop antoher loop to print the *'s. – Danny_ds Dec 27 '15 at 13:33
1

You do not need a switch - an array will do.

Use integer division / by ten to put the number from the range 1..100 into the range 0..10

for (int i = 0 ; i != num ; i++) {
    countByDecade[arr[i] / 10]++;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you! I've updated my code in the post. All I've got to do is to print ' * ' instead of numbers. Do you have any ideas how to accomplish that? – Ilan Aizelman WS Dec 27 '15 at 13:29
1

I don't think switch is an appropriate tool for this problem. Every case takes a constant as input to which it evaluates the statement in the switch. So you would need to devise the constants for each of your options. Which essentially is the same problem you are facing and can be solved with ifs or other, but probably not switch.

niosus
  • 738
  • 8
  • 22
  • Thank you! I've updated my code in the post. All I've got to do is to print ' * ' instead of numbers. Do you have any ideas how to accomplish that? – Ilan Aizelman WS Dec 27 '15 at 13:29
0

Why do you need this checks?

if (temp == 100)
else if (temp > 0 && temp < 10)

The problem is you current code would print

20-29 **
30-39 **
40-49 *
50-59 *
60-69 *
90-99 *

So what you need is:

for (i = 0; i < num; i++) {
    temp = rand() % 100 + 1;
    arr1[(temp + 1) / 10]++;
    printf("\nRANDOM = %d", temp);
}

int j;
for (i = 0; i < N; i++) {
    if (arr[i] > 0) {
         printf("\n%d-%d: ", i*10 + 1, (i+1)*10);
         for (j = 0; j < arr[i]; j++) putchar('*');
    }
}
stek29
  • 395
  • 4
  • 14
-1

You can try below code:

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


int main()
{
    int num = 0, i, *arr;
    srand(time(NULL));
    printf("Please emter the number of values: ");
    scanf("%d", &num);
    arr = (int*)(malloc(num*sizeof(int)));
    if (arr == NULL)
        printf("Not enough memory\n");
    for (i = 0; i < num; i++,arr++)
    {
        *arr = rand() % 100 + 1;
        printf("arr[%d] = %d\n", i, *arr);

        switch((*arr -1) / 10)
        case 0: 
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
    }

    getch();
}
Chris Feng
  • 189
  • 5
  • 19