Can't figure out my MAX/MIN issue

Hey all- so here is what i'm trying to do-

have the user input a .txt file, read the values from the 1-dim array, and output the average, largest, and smallest values.

here's what I have thus far:


#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
// Declare variables---------------------------------------------------------------------------------------------
int sum = 0;
int x;
ifstream inFile;
char userFile [25];
int count = 0;
int arraySize = count;
double smallest=1000000;
double largest=1;

// Prompt user for file to be opened & then delcare what you are reading from------------------------------------
cout << "Please enter the name of the file to open (name.txt): " << endl << endl;
cin >> userFile;
cout << endl << "Reading from: " << userFile << endl << endl;
inFile.open(userFile);

// If the file will not open prompt user of error-----------------------------------------------------------------
if (!inFile)
{
cout << "Unable to open file!" << endl << endl;
system ("pause");
exit(1);
}

// If the file opens, sum values and find the largest and smallest values------------------------------------------
while (inFile >> x)
{
sum = sum + x;
count++;


if (x > largest)
{
largest=x;
}


if (sum < smallest)
{
smallest=sum;
}
}


// Close the file, output our findings: sum, ave, largest, and smallest---------------------------------------------
inFile.close();
cout << "Largest Value = " << largest << endl << endl;
cout << "Sum = " << sum << endl << endl;
cout << "Average = " << (sum/count) << endl << endl;
cout << "Smallest Value = " << smallest << endl << endl;

system ("pause");
return 0;
}



My laregst and smallest figuring is all screwed up. I am currently using the C++ second edition. And have used the code noted in the book, but it didn't work. This program seems to not read all of the values when it is calculating my largest and smallest. ie- when I know the list has val 99 in it...it listed val 88 as the largest...which is incorrect. Do I need to store the data to an array and then sort it? Any help would be appreciated.

BTW- my teacher uses LabGrader...PITA.
also-

should I use a &variable to I can store the data each loop through?
1. Why do you think the values are wrong? What are your inputs and outputs?
2. You calculate largest by checking the input value, but calculate smallest by checking the sum.
3. You should initialise largest/smallest start values with the first value you read in.
4. If you're reading in int's, then largest/smallest should be of type int too.
Last edited on
1. we have to read in a random .txt file with the values given- i know the program's wrong by looking at the values in the file.

2. I used smallest and sum because I was wrong lol- it only worked because I had my test set in order...realized that last night.

3. So, since 'x' represents each value, how would I go about this? I'm still confused as to the logistics of this whole thing.

4. largest and smallest must be doubles- so i will change x to double....that should correct my error there. Thank you for catching that.
Ok- so after you caught my stupid mistakes I have this corrected and working 100%!!! Thanks for catching those; helps to have someone point out the stupid mistakes.

here's what I came up with (for those who need an in your face example; not intended to be stolen for HW!!!!)

----------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
// Declare variables---------------------------------------------------------------------------------------------
double sum = 0;
double x;
ifstream inFile;
char userFile [25];
int count = 0;
int arraySize = count;
double smallest=1000000;
double largest=1;

// Prompt user for file to be opened & then delcare what you are reading from------------------------------------
cout << "Please enter the name of the file to open (name.txt): " << endl << endl;
cin >> userFile;
cout << endl << "Reading from: " << userFile << endl << endl;
inFile.open(userFile);

// If the file will not open prompt user of error-----------------------------------------------------------------
if (!inFile)
{
cout << "Unable to open file!" << endl << endl;
system ("pause");
exit(1);
}

// If the file opens, sum values and find the largest and smallest values------------------------------------------
while (inFile >> x)
{
sum = sum + x;
count++;


if (x > largest)
{
largest=x;
}


if (x < smallest)
{
smallest=x;
}
}


// Close the file, output our findings: sum, ave, largest, and smallest---------------------------------------------
inFile.close();
cout << "Sum = " << sum << endl << endl;
cout << "Largest Value = " << largest << endl << endl;
cout << "Smallest Value = " << smallest << endl << endl;
cout << "Average = " << (sum/count) << endl << endl;


system ("pause");
return 0;
}



Now...my next step is to separate these into their appropriate functions:

1- Write a function named largestArrayValue that finds and returns the largest value in the array of doubles doubArray. The parameters should include the number of elements in the array and the array itself, and the return value should be the largest number in the array.

2- Write another function named smallestArrayValue that finds and returns the smallest value in the array of doubles doubArray.The parameters should include the number of elements in the array and the array itself, and the return value should be the smallest number in the array.

3- Write another function named averageOfArray that finds and returns the average of the array values. The parameters should include the number of elements in the array and the array itself, and the return value should be the average of the numbers in the array.

4-Write another function named loadDoublesFromFileIntoArray that reads doubles from a file and stores them in the array myArrayOfDoubles. The parameters should include the filename, a count of how many numbers are in the file, and an array of doubles. The only thing passed to the function is the filename. The values of count and the array values should be determined within the function. (Will you need pass by reference on count?) The file will have one double per line, and you will have to count the numbers as they are read in. Use a primer read or it won't work!! DO NOT use this function to calculate or determine such things as the largest, smallest and average!!

5- Finally, write a main program that asks the user for a filename and then calls each of the functions above as needed to get the numbers from the file and to display the largest, smallest, and average of the numbers in the file.
Topic archived. No new replies allowed.