c++ array max.

im trying to write a program that reads an array on non negative numbers and outputs the orginal array, then finds the max/2 and than out puts an modfied array that consist of all the numbers that are greater than the array max/2

this is what i have so far

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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    const int ARRAY_SIZE(10);
    int a[ARRAY_SIZE];
    int num_elements(0);
    double max(0);
    
    
    
    cout<<"Enter non-negative numbers (ints) terminated by -100"<<endl;
    

   
        // Display original list
        int i = 0;
        while (i != -100 && num_elements < ARRAY_SIZE)
        {
            if(i > - 1)
            {
                num_elements++;
                cin >> i;
            }
            else
            {
                cin >> i;
            }
        }
        cout << endl << "Original List (" << num_elements << " values): "  << endl;
        
        cout << endl;
        cout << endl;
        
        
        
        // Find maximum value in array
        for(int j = 0; j <= num_elements - 1; j++)  //j is counter variable
        {
            if(a[j] > max)
            {
                max = a[j] / 2.0;
            }
        }

        cout << "The maximum value is " << max << endl;
        
        
        // Display the array of numbers greater than max
        for (int i(0); i < ARRAY_SIZE; i++)
            if (a[i] > max)  //compare the list with max / 2
                
                if(i== ARRAY_SIZE - 1)
                {
                    cout << a[i] <<". ";
                }
                else
                {
                    cout << a[i] <<", ";
                }
        cout << endl << "Modified list (" << num_elements << " values): " << endl;
        
        cout << endl;
    
    

    return 0;
}
You are on right track, but you are not filling the numbers in the array.
The code
1
2
3
4
5
6
7
8
9
10
11
12
13
int i = 0;
        while (i != -100 && num_elements < ARRAY_SIZE)
        {
            if(i > - 1)
            {
                num_elements++;
                cin >> i;
            }
            else
            {
                cin >> i;
            }
        }


can be written as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while( (num_elements < ARRAY_SIZE) && (cin >> a[num_elements]) && (-100 != a[num_elements]) )
{
    if( a[num_elements] > 0)
    {
         //Entered number was positive increment count so that next number 
        //    is input in next element
         num_elements++;
    }
    else
    {
        //Entered number was negative, don't increment the count so that the next
        //    number will overwrite currently input negative number
        cout << "Enter positive numbers only" << endl;
    }
}


Then for printing elements of array, instead of
cout << endl << "Original List (" << num_elements << " values): " << endl;
you need loop something like
1
2
3
4
5
cout << "Original list" <<  endl;
for(auto i& : a)
{
    cout << i << endl;
}


if(a[j] > max) needs to be if((a[j]/2.0) > max)
then again for printing modified list you need loop similar to loop above
Last edited on
Topic archived. No new replies allowed.