A group of 50 people have gone apple picking at Shady Brook Farm. Each person has collected a number of apples. The fewest apples collected is 0, and the most appleas collected is 100. Write a program to perform the following:
a. Declare an array to the store 50 numbers (each corresponds to the number of apples that someone picked).
b. Input the 50 numbers into the array from data file c:\apple_trip.txt. Create the data file yourself, using a short C++ program and the rand function.
c. Print out how many of the people picked less than 25 apples.
d. Print out how many of the numbers are between 80 and 100 apples.
e. Figure out and print the average number of apples picked.
f. If the average is greater than 25, change all apple numbers less than 10 to 0.
g. Print out the contents of the array, 10 numbers on each line.
#include<iostream>
#include<ctime>
#include<fstream>
usingnamespace std;
int main()
{
int applesPicked[50];
int apples;
int counter = 0;
int counter2 = 0;
int sum = 0;
int i = 0;
int y = 0;
srand(time(0));
ofstream oData;
oData.open("apple_trip.txt");
for (i = 0; i < 50; i++)
{
apples = rand() % 100;
oData << apples << endl;
}
oData.close(); //close the file
ifstream iData;
iData.open("apple_trip.txt"); //reopen as an input file
//read the 50 numbers from file into the array
for (i = 0; i < 50; i++)
{
iData >> applesPicked[i];
cout << applesPicked[i] << endl;
}
iData.close(); //close the file
for (i = 0; i < 50; i++)
if (applesPicked[i] < 25)
counter++;
cout << "The amount of people that picked less than 25 apples is: "
<< counter << endl;
for (i = 0; i < 50; i++)
if (applesPicked[i] > 79)
counter2++;
cout << "The amount of numbers between 80 and 100 is: "
<< counter2 << endl;
for (i = 0; i < 50; i++)
{
sum += applesPicked[i];
}
int average = sum / 50;
cout << "The average numbers of apples picked is: "
<< average << endl;
}
You have the average here. All you have to do is check if average is bigger than 25. If it is, Run through all your apples, and the ones that are less than 10, set them to 0.
After that, just print your array out. It's 50 large, int applesPicked[50]; And they want 10 on each line. So print out 10, then use std::endl; then do it 4 more times.
if (average > 25)
for (i = 0; i < 50; i++)
{
if (applesPicked[i] < 10)
applesPicked[i] = 0;
oData << applesPicked[i] << endl;
}
iData.open("apple_trip.txt"); //reopen as an input file
//read the 50 numbers from file into the array
for (i = 0; i < 50; i++)
{
iData >> applesPicked[i];
cout << applesPicked[i] << endl;
}
oData.close(); //close the file
iData.close(); //close the file
}
1. Based on your assignment, lines 27 - 43 are supposed to be in a separate program. This program is supposed to start by opening the file.
2. The assignment says that the maximum number of apples picked is 100. You are using apples = rand() % 100; which produces a number from 0 - 99 (inclusive). You need to use apples = rand() % 101; to meet your assignment's requirements.
3. I don't know why you need to read the numbers in again. They are already in the array, and the array has been properly modified. Maybe you should post your entire program again so we can see how things fit together now.
#include<iostream>
#include<ctime>
#include<fstream>
usingnamespace std;
int main()
{
int applesPicked[50];
int apples;
int counter = 0;
int counter2 = 0;
int sum = 0;
int i = 0;
int y = 0;
srand(time(0));
ofstream oData;
oData.open("apple_trip.txt");
for (i = 0; i < 50; i++)
{
apples = rand() % 100;
oData << apples << endl;
}
ifstream iData;
iData.open("apple_trip.txt"); //reopen as an input file
//read the 50 numbers from file into the array
for (i = 0; i < 50; i++)
{
iData >> applesPicked[i];
}
for (i = 0; i < 50; i++)
if (applesPicked[i] < 25)
counter++;
cout << "The amount of people that picked less than 25 apples is: "
<< counter << endl;
for (i = 0; i < 50; i++)
if (applesPicked[i] > 79)
counter2++;
cout << "The amount of numbers between 80 and 100 is: "
<< counter2 << endl;
for (i = 0; i < 50; i++)
{
sum += applesPicked[i];
}
int average = sum / 50;
cout << "The average numbers of apples picked is: "
<< average << endl;
if (average > 25)
for (i = 0; i < 50; i++)
{
if (applesPicked[i] < 10)
applesPicked[i] = 0;
}
//read the 50 numbers from file into the array
for (i = 0; i < 50; i++)
{
cout << applesPicked[i] << ' ';
if ((i + 1) % 10 == 0)
cout << endl;
}
oData.close(); //close the file
iData.close(); //close the file
}
/*The amount of people that picked less than 25 apples is : 13
The amount of numbers between 80 and 100 is : 9
The average numbers of apples picked is : 46
60 92 0 0 32 54 33 39 32 0
11 62 90 54 82 17 32 57 35 25
0 15 54 96 48 14 99 97 83 49
12 65 67 85 13 24 45 23 51 91
25 58 47 32 59 75 74 35 50 12
Press any key to continue . . .*/