Comparing Values in Array program problem

Hi,
I need to write a program that adds up the prices in the array and then finds the average. I have that part handled and it works. But now i need to display all values under five dollars and all values greater than the average. i am not sure how to do this, i know i need to use a for loop, but i am not sure how to comapre the numbers on the array to see if they are less than or greater than something and then either display them or put them in another array.


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
#include <iostream>
#include <conio.h>
#include  <iomanip>

using std::cout;
using std::endl;
using std::setw;
using std::setprecision;
using std::fixed;

int main()
{
    const int priceSize = 20;
    const int under5Size = 20;

    float prices[ priceSize ] = { 2.34, 7.89, 1.34, 9.99, 3.24, 4.25, 5.68, 1.21, 8.45, 9.85, 2.75, 3.68, 5.68, 8.26, 9.10, 1.02, 6.30, 5.32, 7.99, 2.35 };
    
    float under5[ under5Size ] = { 0 };
    
    float total = 0;
    float average;
    float minprice = 5.00;

    for ( int i = 0; i < priceSize; i++ )
        total += prices[ i ];

        if (prices[ i ] < minprice) {
        under5[i] = arrayValue;
        
        }
    average = total / 20;

    
    
    cout << "Total of prices: " << total << endl;
    cout << endl << "The average of all the prices is: "<< setprecision(2) << fixed << average << endl;
    cout << under5[ i ];

    char pause = getchar();  // program pauses before ending

    getchar();


    return 0;

}
1
2
3
4
5
6
7
8
9
for(int i = 0; i < priceSize; i++ )
{
    if(prices[i]<5)
    {
        int k = 0;
        under5[k] = prices[i];
        k++;
    }
}

you can deal with value greater than average in the same way
I took your advise and this what i changed my code too. But when i run it prints the first value 2.34 and then it prints the rest of numbers as 0's. any ideas?

thanks for the 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
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
#include <iostream>
#include <conio.h>
#include  <iomanip>

using std::cout;
using std::endl;
using std::setw;
using std::setprecision;
using std::fixed;

int main()
{
	const int priceSize = 20;
	const int under5Size = 20;

	float prices[ priceSize ] = { 2.34, 7.89, 1.34, 9.99, 3.24, 4.25, 5.68, 1.21, 8.45, 9.85, 2.75, 3.68, 5.68, 8.26, 9.10, 1.02, 6.30, 5.32, 7.99, 2.35 };
	
	float under5[ under5Size ] = { 0 };
	
	float total = 0;
	float average;

	for ( int i = 0; i < priceSize; i++ ){
		total += prices[ i ];
	}
	
	
	for ( int j = 0; j < priceSize; j++){
	if(prices[ j ] < 5)
    	{
        int k = 0;
        under5[ k ] = prices[ j ];
        k++;
    	}
	cout << under5[ j ] << endl;
	}



	average = total / 20;

	
	
	cout << "Total of prices: " << total << endl;
	cout << endl << "The average of all the prices is: "<< setprecision(2) << fixed << average << endl;

	char pause = getchar();  // program pauses before ending

	getchar();


	return 0;

}
1
2
3
4
5
6
7
8
9
  for ( int i = 0; i < priceSize; i++ )
        total += prices[ i ];

       
        if (prices[ i ] < minprice) {
        //*1
        under5[i] = arrayValue;
        
        }


*1 - you're using i to iterate through prices array AND under5 array. If the price isn't under 5, then that same position in under5 remains 0 (because you initialized the entire array to 0).

Yang provided the proper fix for *1.

*2 - Most recent post - kind of the same thing even after the fix. Basically, you're printing out values through the entire length of the under5 array even though not all values are under5 and thus are represented as 0.

*3 - I see a conflict of ideas from the OP original post and current post. Yang based his/her solution on the fact that the OP wanted to store the under5 values , however the most recent update of the OP suggests that this isn't necessary.

If you're planning to simply print out the under5 values in place then write the cout below if(prices[j] < 5).

1
2
if(prices[j] < 5)
  cout << prices[j];
that solved it. thanks guys.
Topic archived. No new replies allowed.