Ok, the task at hand is one of finding the min, max, and average number in a file consisting of only numerals. I've gotten the average but, I'm having difficulty finding the min a max numbers.
What I've got so far (the average works, the min doesn't)
#include<stdio.h>
int main()
{
//char fileName[160];
FILE *inFile; //file stream name
double nextNum; //num read from
double total = 0.0; //total of numbers
int numRead = 0; //amount of numbers read
int min, max;
//printf("Which file do you want to count? ");
//scanf("%s", &fileName);
inFile = fopen("F:\\temps.txt", "r");
if(inFile == NULL){
printf("File could not be opened\n");
system("pause");
return 1;
}
while (fscanf(inFile, "%lf", &nextNum)!= EOF){
numRead++;
total += nextNum;
}
fclose(inFile);
if (numRead == 0)
printf("no nums read from file\n");
else
printf("Total numbers read: %d\n", numRead);
printf("Average of numbers read: %.2f\n", total / numRead);
while (fscanf(inFile, "%lf", &nextNum)!= EOF){
if(nextNum < 120)
nextNum = min;
(min > nextNum)
min = nextNum;
}
printf("The min number is: %.2f\n", min);
system("pause");
return 0;
}
How do I go about finding the min? I'm sure I can figure out the max.
Just take the first number you get as the initial minimum. After that, compare all of the following numbers with the initial minimum, if any of them is smaller it becomes the new minimum.
Finding the minimum and the maximum are actually almost, if not exactly, the same. Try working on the max portion then see if you can't edit it to work for the min part as well.
As for your actual code, unless you want the minimum to be shown as 120 if all the numbers happen to be greater, you should initialize min to the fire number you read, then start the loop.
Just take the first number you get as the initial minimum. After that, compare all of the following numbers with the initial minimum, if any of them is smaller it becomes the new minimum.
How would I go about this? Because I've basically tried that. Even though min started a 120, I was attempting to change it to the to first nextNum less than that and compare the each number after that to it.
#include<stdio.h>
int main()
{
//char fileName[160]; //array of file
FILE *inFile; //file stream name
double nextNum; //num read from
double total = 0.0; //total of numbers
int numRead = 0; //amount of numbers read
int min, max;
//printf("Which file do you want to count? ");
//scanf("%s", &fileName);
inFile = fopen("F:\\temps.txt", "r");
if(inFile == NULL){
printf("File could not be opened\n");
system("pause");
return 1;
}
while (fscanf(inFile, "%lf", &nextNum)!= EOF){
numRead++;
total += nextNum;
}
if (numRead == 0)
printf("No numbers read from file\n");
else
printf("Total numbers read: %d\n", numRead);
printf("Average of numbers read: %.2f\n", total / numRead);
fscanf(inFile, "%lf", &nextNum);
min = nextNum;
while(fscanf(inFile, "%lf", &nextNum)!= EOF){
if(nextNum < min)
min = nextNum;
}
printf("The min number is: %.2f\n", min);
fclose(inFile);
system("pause");
return 0;
}