elements greater than the array max

im trying to write a program that finds the max in an array and divedes it by two than oututs a modfied list with all the elements greater than the max/2.

I got the first part but just not sure how to find the elements greater than the max/2 and output them correctly into the modfied list.


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


const int ARRAY_SIZE(10);


// Function Prototypes

// Read_list Function Prototype
void read_list(int a[], int & num_elements);

// Print_array Function Prototype
void print_array(const int a[], const int num_elements);

// Find_max Function Prototype
double find_max(const int a[], const int num_elements);

// Array_greater Function Prototype
void array_greater( const int max, int a[], const int num_elements);


int main()
{
    int array[ARRAY_SIZE];
    int num_elements(0);
    int max(0);
    
    
    read_list(array, num_elements);
	
    
    if (num_elements > 0)
    {
        // Display original list
        cout << endl << "Original List (" << num_elements << " values): " << endl;
        print_array(array, num_elements);
        cout<<endl;
        cout<<endl;
        
        // Find maximum value in array
        max = find_max(array, num_elements);
        cout << "The maximum value is " << max << endl;
        
        // Display the array once the maximum value has been added to it
        cout << endl << "Modified list (" << num_elements << " values): " << endl;
        array_greater(max, array, num_elements);
        print_array(array, num_elements);
        cout<<endl;
    }
    
    
    else
    {
        cout << "The list is empty." << endl;
  	}
    
    return 0;
}



//Define Functions

// Defining read_list Function
void read_list(int a[], int & num_elements)
{
	cout<<"Enter non-negative numbers (ints) terminated by -100"<<endl;
    
	int i = 0;
	cin >> i;
	while (i != -100 && num_elements < ARRAY_SIZE)
	{
		if(i > - 1)
		{
            a[num_elements] = i;
            num_elements++;
            cin >> i;
		}
		else
		{
            cin >> i;
		}
	}
}



// Defining print_array Function
void print_array(const int a[], const int num_elements)
{
	int i = 0;
	for(i = 0; i <= num_elements - 1; i++) //i is counter variable
	{
		if(i==num_elements - 1)
		{
			cout << a[i] <<". ";
		}
		else
		{
			cout << a[i] <<", ";
		}
	}
}



// Defining find_max Function
double find_max(const int a[], const int num_elements)
{
	int max(0);
	for(int j = 0; j <= num_elements - 1; j++)  //j is counter variable
	{
        if(a[j] > max)
        {
            max = a[j] / 2.0;
        }
	}
	return(max);
}



// Defining array_greater Function
void array_greater( const int max, int a[], const int num_elements)
{
   ??????????????
}
Last edited on
Excessive operations at:
for(int j = 0; j <= num_elements - 1; j++)
Can be trimmed down it:
for(int j = 0; j < num_elements; j++)

For the last part:

From my understanding, you need to compare the list with max / 2, and print any element larger than max / 2?

If so, then you could skim through the array's element again, and do comparison.

1
2
3
4
5
void array_greater( const int max, int a[], const int size) {
  for (int i(0); i < size; i++)
    if (a[i] > max / 2)  //compare the list with max / 2
      cout << a[i];      //print any element larger than max / 2
}
that worked perfectly thanks a lot.


maybe you could help me on my other problem im having haha http://www.cplusplus.com/forum/general/119580/
Topic archived. No new replies allowed.