sorting random numbers?

hey guys i need help with a simple code. i'm super new to coding and i'm pretty sure sorting randomly generated numbers have not been taught by my profs yet. however, an exercise requires me to use my current skills (loops and decision making) to generate 50 random numbers within 4 to 6 and then sort it into 4 groups with intervals of 0.5 (eg 4-4.5, 4.5-5, etc etc). i managed to generate 50 numbers within the range but i have absolutely no idea how to continue from here. also, i have yet to learn arrays so i'm not sure if i can use it in the code. are there any other ways to do that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    int i = 1, max = 50, random_num, student_height;
    srand((unsigned) time(NULL));

    while (i <= max)
    {
        random_num = rand()%200+400;
        double(student_height) = random_num / 100.0;
        printf("The height of a random student is %.2lf \n", student_height);
        i++;
    }
    return 0;
}
Hello, @eggosbae,

I think the word "sort" is slightly misleading here. My reading is that you are probably being asked to count the number of times student_height falls within each of the intervals [4,4.5), [4.5,5), [5, 5.5), [5.5, 6). If you don't want to use arrays then call these counts a,b,c,d (or whatever), initialise them to zero and use your "current skills (...decision making)" - aka if statements - to increment the correct one as you generate each student_height.


Some other things about your code:
- It's an unholy mix of C and C++; maybe you want to use the C++ headers <cstdio>, <cstdlib>, <ctime>
- Just int main() - it doesn't need / probably shouldn't have - void
- The one that really threw me was your variable(s) student_height. C++ shell helped me a little here by pointing out that the int variable declared on line 7 isn't actually used. Consider what's actually happening with your double(student_height) =. It declares a new variable of type double within the scope of the while loop, which will hide the original of the same name (and different type). I think the best thing would be to not declare student_height as an int (since it clearly isn't) and declare it as a double either at the beginning:
double student_height;
or at the point of first use within the scope where it is used:
double student_height = random_num / 100.0;
Last edited on
heyyy my prof used the phrase "classify into groups according to their heights", so i'm not entirely sure if i'm supposed to show the number of times it falls within the intervals. i might email my prof to clarify it, though.

- The one that really threw me was your variable(s) student_height. C++ shell helped me a little here by saying that the int variable declared on line 7 isn't actually used! I'm not sure what's actually happening with your double(student_height) =: it seems to be declaring a new variable of type double within the scope of the while loop. However, I think the best thing would be to not declare student_height as an int (since it clearly isn't) and declare it as a double either at the beginning:
double student_height;
or at the point of first use:
double student_height = random_num / 100.0;

thanks for this!! i got really confused half way through it. i'm declaring it as double from the beginning now.

also, i think i'm using c instead of c++ since i was taught the headers to be that way and my profs have been using int main(void) since day 1?? i'm sorry, i have literally 0 knowledge on coding and being thrown this exercise is just devastating for me.
what you are doing looks mostly like C.
c++ would use

<cstdlib>
<cstdio>
<ctime>
and would use cout instead of printf.

This program is legal in c++ as well; most C programs work as C++ programs.

you should review your class description, book, or other such material to C which language you are supposed to be learning.

if you want to sort the values, you pretty much need an array or other container for the values.

if you just want a count of how many in each band, you can do that without it.
you can modify that print statement to say which band the value fell into, print the 50 values with that, and then print the counts.

the only other way I know to do it without an array and sorting would be to make 4 strings of extreme length and strcat into them your output statement (complete with end of line) as you generate each value, then print the 4 statements at the end. They would not be sorted but they would be grouped.


Topic archived. No new replies allowed.