>I am needing to print my array in 5 lines of 10 numbers evenly spaced.
Test the remainder of division with 10:
Code:
for (x = 0; x < 50; x++) {
printf("%-4d", ia[x]);
if ((x + 1) % 10 == 0)
printf("\n");
}
>int compare (void *a, void *b);
qsort expects the comparison function to take pointers to const void:
Code:
int compare(const void *a, const void *b);
>void main(void)
On a hosted implementation, main must return int. We usually claim this because the standard says so in exquisite detail, but there are practical reasons as well. If the run-time startup code uses the return value for main, and you defined main as returning void, the result will be an indeterminate value. Aside from being undefined behavior if accessed, it also causes untold confusion for programs that use it. Even worse, due to different call/return sequences expected by the C run-time, the program may simply crash when it tries to call main, thus giving you an incredibly difficult bug to track down and fix.
That said, your template for main without arguments is:
Code:
int main(void)
{
return 0;
}
And your template for main with arguments is:
Code:
int main(int argc, char *argv[])
{
return 0;
}
Or anything equivalent. The two biggies are the proper declaration and a valid return value.
>srand(time(NULL));
This will probably work (even though time(NULL) should be cast to unsigned int), but keep in mind that due to the restrictions on time_t, it's not portable. If you really want to use the current time as a seed for rand--a reasonable desire--then you can do so fairly easily by punning the time_t result of time(NULL) to a pointer to unsigned char and create a hash from the bytes:
Code:
#include <limits.h>
unsigned int random_seed(void)
{
int i;
unsigned seed = 0;
time_t now = time(NULL);
unsigned char *p = (unsigned char *)&now;
for (i = 0; i < sizeof now; i++)
seed = seed * (UCHAR_MAX + 2U) + p[i];
return seed;
}
>qsort(ia, 50, sizeof(int), compare);
qsort should be called outside of the loop, after all of the values have been assigned. At present, you're forcing qsort to access indeterminate values, which has undefined behavior.
>return *(int*)a-*(int*)b;
This is an exceptionally dangerous construct. You risk signed integer underflow (another thing with undefined behavior). You could make them unsigned integers, but that opens up new problems when (the now well defined) underflow reverses the desired effect.
Compare this with what you had:
Code:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
unsigned int random_seed(void);
int compare(const void *a, const void *b);
int main(void)
{
int x, y, ia[50];
srand(random_seed());
for (x = 0; x < 50; x++) {
y = rand() % 1000 + 1;
ia[x] =y ;
}
qsort(ia, 50, sizeof(int), compare);
for (x = 0; x < 50; x++) {
printf("%-4d", ia[x]);
if ((x + 1) % 10 == 0)
printf("\n");
}
return 0;
}
unsigned int random_seed(void)
{
int i;
unsigned seed = 0;
time_t now = time(NULL);
unsigned char *p = (unsigned char *)&now;
for (i = 0; i < sizeof now; i++)
seed = seed * (UCHAR_MAX + 2U) + p[i];
return seed;
}
int compare(const void *a, const void *b)
{
const int *ia = a;
const int *ib = b;
if (*ia < *ib)
return -1;
else if (*ia > *ib)
return +1;
else
return 0;
}