Arrays

In the beginning of the program a set of 50 numbers is randomly generated to populate an array. The array is then organized by a selection sort. The sum of the numbers is taken and the average of them. The final step is for the array to be evaluated so that any number that is higher than the average is put into a new array, and then displayed.

My function I have been working on is
with threshold as the average calculated, ar[] as the original array, int n as length of the first array, foundElements[] as the array that is populated with the numbers above the average, and the count as the length of the new array.

void findElementsGreaterThan(double threshold, const int ar[], const int n, int foundElements[], int& count) {
int a = 0;
int sum = 0;

for (a = 0; a < n; a++)
{
sum+=ar[a];
}

double average = sum/n;
threshold = average;

for(int i = 0; i < n; i++) {
if (ar[i] > threshold) {
foundElements[count + 1] = ar[i];
}
}
}

After compiling, this function doesn't evaluate
closed account (SECMoG1T)
Hi there you can do this easily by assigning different roles to different function

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
int ave(int ar [], int size)
   {
      int sum=0, average;
      for (int j=0; j <size; j++)
          sum+=ar [j];

       average=sum/size;
       return average; 
   }

  int greater_than_ave (int ar [], int size)
    {
        int average=ave (ar, size);
        int count=0;
       
        for (int j=0; j <size;  j++)
             {
                if (ar [j]> average)
                       ++count; 
             }

           return count; 
      }

     void fill_new_array (int old [], int oldsize, int newarr []) /// newarr size is calculated by 
               {                                                                                    /// greatet_than_ave on old array
                                                                                                  /// add will not need pass it here 
                  int index=0;
                 int average=ave (old, oldsize);
                  for (int j=0; j <oldsize;  j++)
                     {
                         if (old [j]> average)
                             { newarr [index]=old [j] ; ++index; }
                     }
                }

   int main ()
  {
     int newsize=greater_than_ave (oldarray, oldarraysize);//oldarray is the array you had ealier. 
     int New_array[newsize];

     fill_new_array(oldarray, oldsize, New_array); /// functions make it easier
    
Topic archived. No new replies allowed.