John DAngelo
John DAngelo

Reputation: 15

C++ List Processing: initializing min to first value of list

The issue I am having with my code is that the min always reads 0. The reason is because I haven't properly initialized it to the first value of "random.txt". How would I do that?

Here's my code:

using namespace std;

int main()
{

    ifstream inputFile;
    //open file
    inputFile.open("random.txt");
    int numCount = 0;
    int number, max, min, i, x;
    double average, sum;

    while(inputFile >> number)
    {
        for (i = 0; i < numCount; i++)
        {
            x = number;
            if (x < min)
            {
                min = x;
            }
            else if (x > max)
            {
                max = x;
            }
        }

        numCount++;
        sum = number + sum;
    }
    if (numCount > 0)
    {
        average = sum/numCount;
    }

    inputFile.close();

    cout << "Number of numbers is: " << numCount << endl;
    cout << "Sum of numbers is: " << sum << endl;
    cout << "Average of numbers is: " << average << endl;
    cout << "Max of numbers is: " << max << endl;
    cout << "Min of numbers is: " << min << endl;

    return 0;
}

Upvotes: 0

Views: 1231

Answers (4)

user2673663
user2673663

Reputation:

ifstream inputFile;
//open file
inputFile.open("random.txt");
int numCount = 0;
int number = 0, max = 0, min = 0, i = 0;
double average = 0, sum = 0;
bool first = true;
while (inputFile >> number)
{
    if (first){
        first = false;
        max = min = number;
    }
    if (number < min)
    {
        min = number;
    }
    else if (number > max)
    {
        max = number;
    }
    numCount++;
    sum = number + sum;
}

This is I edit. I hope you can get your answer.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409166

Initialize min to the largest possible value, and max to the lowest possible value.

Like

int min = std::numeric_limits<int>::max();
int max = std::numeric_limits<int>::min();

See here for a reference of std::numeric_limits.


As hinted to by WhozCraig in a comment to this answer, your if-else if construct is not the best if the numbers in the file are just getting lower, and no values are bigger than the previous value.

You might want to do either what WhozCraig suggests in the comment, or change to two separate if statements.


Sample

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

int main()
{
    std::ifstream inputFile("random.txt");
    int number, max=0, min=0;
    int numCount = 0;
    double average=0, sum=0;

    if (inputFile >> number)
    {
        min = max = number;
        numCount = 1;
        while(inputFile >> number)
        {
            if (number < min)
                min = number;
            else if (number > max)
                max = number;

            sum += number;
            ++numCount;
        }

        average = static_cast<double>(sum)/numCount;
    }

    cout << "Number of numbers is: " << numCount << endl;
    cout << "Sum of numbers is: " << sum << endl;
    cout << "Average of numbers is: " << average << endl;
    cout << "Max of numbers is: " << max << endl;
    cout << "Min of numbers is: " << min << endl;

    return 0;
}

Upvotes: 2

Mr. 47
Mr. 47

Reputation: 96

Initialize min to the maximum integer value possible:

  1. Include the <climits> header file (See here for details)
  2. Initialize min = INT_MAX.

This will work great, but won't work as expected in case there are no numbers at all in the file. To work around that, you should use a boolean variable (initialized to false), and set it to true inside your while loop. Thus, after the loop exits, you will be able to tell whether the values of min and max are real, or just the initial values that haven't been changed (another way to do that would be to test, after the loop, whether min is bigger than max - if it is, then the file was empty).

Also notice that it seems that the for loop is redundant, and doesn't even execute for the first number.

Upvotes: 0

asalic
asalic

Reputation: 949

Since your numbers are int, try to init min to INT_MAX and max to INT_MIN. You can find these macros in limits.h.

http://www.cplusplus.com/reference/climits/

Upvotes: 0

Related Questions