ok I searched the forum... and followed some of the examples... but somehow it only prints out a list of 0....
heres what I did:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DATA_FILE "test_data.txt"
void initializeRandom(void); /* programmer written functions - prototypes */
int getRandom(int maximum_value);
int compare( void *a, void *b )
{
return ( a, b );
}
int main(void)
{
int ranges[10] = { 0 };
int rand_value = 0;
int max = 0;
int min = 1000;
int user_input = 0;
int i = 0, n=0;
int number_values = 0;
int array[999] = {number_values};
char id_string[8]; /* 7 characters for the ID# + 1 for \0 */
FILE *fp;
/* 1. Get user input */
printf("Enter your colleague ID # --> ");
scanf("%d", &user_input);
fp = fopen(DATA_FILE, "w");
/* Generate a string out of the value inputted by user */
sprintf(id_string, "%07d", user_input);
number_values = atoi(&id_string[4]);
initializeRandom();
fprintf(fp, "This is the number of values: %d\n", number_values);
printf("This is the number of values: %d\n", number_values);
fprintf(fp, "This is the list\n");
printf("This is the list\n");
for (i = 0; i < number_values; i++)
{
rand_value = getRandom(999);
fprintf(fp, "%d\n", rand_value);
printf("%d\n", rand_value);
if (rand_value > max)
{
max = rand_value;
}
if (rand_value < min)
{
min = rand_value;
}
ranges[rand_value / 100]++;
}
printf("The sorted list is:\n");
qsort(rand_value, number_values, number_values, compare);
for ( i=0; i<number_values; i++ )
printf( "%d\n", array[i] );
if (max > 0)
{
printf("this is the max value %d\n", max);
fprintf(fp, "this is the max value %d\n", max);
}
if (min > 0)
{
printf("this is the min value %d\n", min);
fprintf(fp, "this is the min value %d\n", min);
}
for (i = 0; i < 10; i++)
{
int lower = i * 100;
int upper = (i + 1) * 100;
printf("Number Records %03d-%03d: %d\n", lower, upper, ranges[i]);
fprintf(fp, "Number Records %03d-%03d: %d\n", lower, upper,
ranges[i]);
}
fclose(fp);
system("pause");
return (0);
}
/* Call this function to start random number generation, */
/* else every time you run you'll get the same answers - ONLY CALL 1 time */
void initializeRandom(void)
{
srand((unsigned) time(NULL));
}
/* get a random number between 1 and maximum_value passed in */
int getRandom(int maximum_value)
{
int random = 0;
random = rand();
/* Modulus gives you a value between 0 and 1 less */
/* then number dividing by, so add 1 to shift */
random = (random % maximum_value) + 1;
return (random);
}