0

In C I need to generate 100 random numbers and then sort them in descending order. As of now, I have this, and it is an absolute mess. I can tell I am going about it all wrong but am not experienced enough to actually be able to write this in a sane manner. Can anyone help?

/*100 random numbers in range 1-10*/
/*sorted in descending order*/
#include <stdio.h>
void random(char *nums, int count);

int main(void) {
    int random_num, count;
    char nums[100];
    for(count = 0; count < 100; count++) {
        random(&nums, count);
    }

    for(count = 0; count < 100; count++) {
        printf("%d\t", nums[count]);
    }

    return 0;
}

void random(char *nums, int count) {
    int random_data = fopen("/dev/random", "r");
    nums[count] = fread[random_data];
}
user3897320
  • 87
  • 1
  • 2
  • 7

3 Answers3

1

please, call rand() on stdlib.h:

#include <stdlib.h>

int main(void) {
    int random_num, count;

    srand(time(NULL));
    char nums[100];
    for(count = 0; count < 100; count++) {
        nums[count] = rand();
    }

    for(count = 0; count < 100; count++) {
        printf("%d\t", nums[count]);
    }

    return 0;
}
Jason Heo
  • 9,956
  • 2
  • 36
  • 64
  • Now all I have to do is sort them, I guess. Is it possible to seed `/dev/random`? – user3897320 Aug 05 '14 at 04:47
  • Did you mean you'll use `/dev/random` for seed number of `srand()`?. It's possible if you convert it to `int` type. – Jason Heo Aug 05 '14 at 04:50
  • Yes. It seems like `/dev/random` would definitely be a better choice anyways. I got the random numbers down thanks to you, now to sort them. – user3897320 Aug 05 '14 at 04:54
  • @user3897320 `/dev/random` might be better. but how about microsecond. it could be obtained by `gettimeofday()`. and enjoy your sort ;) – Jason Heo Aug 05 '14 at 04:56
  • Yeah, sorting is getting a little confusing but I don't want to ask for help. It seems so easy, but somehow it just isn't. I've done it before, but can't remember how. – user3897320 Aug 05 '14 at 04:58
0

1 random number: first srand(time(0)), then using rand() to generate the number you want

2 sort: using qsort(array, n, sizeof(int), comp);

 //here comp let the number in desc order
int  comp(const void*a,const void*b)
{
    return *(int*)b-*(int*)a;
}

both of them need < stdlib.h>

mickeyandkaka
  • 1,452
  • 2
  • 11
  • 21
  • I just made a for loop that I _think_ is quicksort. Your use of multiple pointers really throws me off. I desperately need to understand pointers and learn some damn algorithms! – user3897320 Aug 05 '14 at 05:04
  • The qsort function implements a quick-sort algorithm to sort an array of num elements, each of width bytes. The argument base is a pointer to the base of the array to be sorted. qsort overwrites this array with the sorted elements. The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort calls the compare routine one or more times during the sort, passing pointers to two array elements on each call: cmp((void*) & elem1, (void*)& elem2 ); you need to import int pointer. PS: pointer is important – mickeyandkaka Aug 05 '14 at 05:07
0

Try this code, I have also included code for sorting the values in descending order.

/*100 random numbers in range 1-10*/
/*Sorted in descending order*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void sorting(char *numbers) /* Sorting in descending order */
{
    int i,j,temp;
    for(i=1;i<100;i++)
    {
        for(j=0;j<100-i;j++)
        {
            if(numbers[j] < numbers[j+1])
            {
                temp = numbers[j+1];       
                numbers[j+1] = numbers[j];
                numbers[j] = temp;
            }
        }
    }
}

int main(void) {
    int random_num, count;

    srand(time(NULL));
    char nums[100];
    for(count = 0; count < 100; count++) {
        /* Mod by 11 as we need range 1-10 */
        nums[count] = rand() % 11;
    }
    /* Sorting in descending order */
    sorting(nums);

    printf("Printing values after sorting.\n");
    for(count = 0; count < 100; count++) {
        printf("%d\n", nums[count]);
    }
    return 0;
}
ani627
  • 5,578
  • 8
  • 39
  • 45