Having trouble with sections f &g

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream>

#include<ctime>

#include<fstream>

using namespace 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;

}
Last edited on
int average = sum / 50;

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.
I'm having a problem writing the code to change the numbers in the array < 10 to 0
if (average > 25)
????
You want to go through the array and do
1
2
3
if (applesPicked[i] < 10) {
    applesPicked[i] = 0;
}

Last edited on
Got it. Had to reopen the files. Now working on printing out 10 each line. Thank you for your help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
	
}

Last edited on
Just a couple of notes.

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.
You're right. Thank you. I think I finally got it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include<iostream>

#include<ctime>

#include<fstream>

using namespace 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 . . .*/
Topic archived. No new replies allowed.