1
#include<iostream>
#include<fstream>

using namespace std;

int main()
{
    ifstream inFile;
    ofstream outFile;
    int sum;
    double average;
    int apple[50];
    int b1;
    int i;

    outFile.open("apple_trip.txt");

    for (i = 0; i < 50; i++)
    {
        b1 = rand() % 100;
        outFile << b1 << endl;
    }

    outFile.close();

    inFile.open("apple_trip.txt");
    for (i = 0; i < 50; i++)   // This loop ensures you don't keep overwriting
        // the same value, which would be off by 1
    {
        inFile >> apple[i];
    }

    for (i = 0; i < 50; i++) // The loop variable is i, not apple[i], and we stop at 50
    {
        if (apple[i] < 25)
        {
            cout << apple[i] << " people picked less than 25 apples" << endl;
        }
    }

    for (i = 0; i < 50; i++)
    {
        if (apple[i] > 80 && apple[i] < 100) // change < to >
        {
            cout << "The number of apples that is between 80 and 100 is : " << apple[i] << endl;
        }

    }

    for (i = 0; i < 50; i++)
    {
        sum = 0;
        sum += apple[i];
        average = sum / 50.0;
        cout << average;
    }
    return 0;
}

basically the last for loop wont print out the sum correctly. It gives some weird decimals thingy. Been trying to see wheres the error but im just a beginner in c++ and im not really smart in computer science. Any help will be appreciated!

3 Answers 3

3
for (i = 0; i < 50; i++)
{
    sum = 0;
    sum += apple[i];
    average = sum / 50.0;
    cout << average;
}

Yes, this won't calculate the average. You will have to initialize sum and print average outside the loop.

Try this:

sum = 0;
for (i = 0; i < 50; i++)
{
    sum += apple[i];
}
average = sum / 50.0;
cout << average;

average = sum / 50.0; can be either inside or outside of the loop, but outside is better.

Sign up to request clarification or add additional context in comments.

Comments

1

If I understand correctly, what you want to do with that last loop is print the average of apples per trip, for all 50 trips.

If that is the case, the reason it doesn't work right now is because you are setting the sum to 0 at the beginning of the loop, and printing out the average at the end. What you would want to do is take the initialization of sum out of the loop, and do the same thing for the average calculation as well as the printing instruction. This would look like this:

sum = 0;
for (i = 0; i < 50; i++)
{
    sum += apple[i];
}
average = sum / 50.0;
cout << average;

And at this point, you can simplify it a big to look like this:

sum = 0;
for (i = 0; i < 50; i++)
    sum += apple[i];

average = sum / 50.0;
cout << average;

Hope that helps! (if that is what you were trying to achieve ;D )

Comments

0

As Mike said, intitlize sum above the loop, and calculate the average outside of the loop. The only thing in the loop should be the addition.

One thing I'd like to add is that you'd be better off using vector rather than a raw array of ints. So instead of:

int apple[50];
// ...
for (int i = 0; i < 50; i++)
{
    inFile >> apple[i];
}

I think you might want to do:

vector<int> apple;
int temp;
// ...
for (int i = 0; i < 50; i++)
{
    inFile >> temp;
    apple.push_back(temp);
}

1 Comment

How would you change all the apple numbers in the file to 0 if they are less than 10 and print it out. I know the if statement for it but im a little bit clueless on how to print the contents of the array with 10 numbers on each line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.