Counting number from a random file
Hi!! I am a newbie at C programming, and I need help wih my program...
I am suppose to write a C program that opens up the generated file and produces a report that looks like:
Number of records : you determine the value and print it here
Maximum Value : you determine the value and print it here
Minimum Value : you determine the value and print it here
so far I have this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define DATA_FILE "c:\\test_data.txt"
void initializeRandom(void); /* programmer written functions - prototypes */
int getRandom(int maximum_value);
int main (void)
{
int min = 0;
int max =0;
int count = 0;
int user_input = 0;
int i = 0;
int number_values = 0;
char id_string[8]; /* 7 characters for the ID# + 1 for \0 */
char number_string[4];
int rand_value = 0;
FILE *fp;
/* 1. Get user input */
printf("Enter your colleague ID # --> ");
scanf("%d", &user_input);
fp = fopen(DATA_FILE, "r");
/* Generate a string out of the value inputted by user */
sprintf(id_string, "%07d", user_input);
strncpy(number_string, &id_string[4], 3);
number_values = atoi(number_string);
initializeRandom();
//printf("%s results %s is %d \n", id_string, number_string, number_values);
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>i)
max = rand_value;
if(min< rand_value)
min =rand_value;
if (max >0)
printf("This is the max %d\n", max);
printf("This is the min %d\n", min);
}
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 ((time_t *)NULL));
}
/* get a random number between 1 and maximum_value passed in */
int getRandom(int maximum_value)
{
int random = 0;
random = rand();
random = (random % maximum_value) + 1; /* Modulus gives you a value between 0 and 1 less then number dividing by, so add 1 to shift */
return(random);
}
I can't seem to get the max and min value... and I dont know how to count the total number of values...
So can someone help me please...