Almost done with program need a little bit of help finding max! (Functions/Arrays)

Hi everyone, was just hoping I could get a little bit of help on this problem! The goal of the program is to take a user-generated array and find different statistics for it using functions. I have almost all of it down however it came to my attention that my max function does not seem to be operating properly. It seems to only print out what the final inputted # is and I can't seem to figure out why this is, I would greatly appreciate some 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
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
  // Example program
#include <iostream>
#include <string>
#include <math.h>

using namespace std; 
int getInput (int N);
int N;
int sum = 0; 
int i = 0;
double standarddev;
double difference;
int Average(int arr[], int N, int i);
int theMaximum (int arr[], int max, int i);
int theMinimum (int arr[], int min, int i, int N); 
double stdDev (int N, int arr[], int average);
int theRange ( int max , int min);

int main()
{
  int arr[N];
  int max = 0; 
  int min;
  int avg;
  int thehighest;
  int thelowest; 
  double stdDeviation;
  getInput(N);
  for ( int i=0; i < N;i++){
        cin >> arr[i];
       avg = Average(arr,N, i);
        thehighest = theMaximum(arr, max, i);
        min = arr[N-1];
        thelowest = theMinimum (arr, min, i, N);
        stdDeviation = stdDev (N, arr, avg); 
        
    }
    stdDeviation = sqrt(stdDeviation);
    cout << "The average is!"<< endl << avg << endl; 
    cout << "The max is!" << endl << thehighest << endl;
    cout << "The Minimum is!" << endl << thelowest << endl; 
    cout << "The Standard Deviation is!" << stdDeviation << endl;
}

int getInput (int a){
    
    cout << "How many numbers would you like in your set?" << endl; 
    cin >> N; 
    return (N); 
}

int Average (int arr[], int N, int i){
    int average;
    //cout << i << endl;
    //cout << arr[i] << endl ;
    //for (int i = 0; i < N ; i++){
    sum = sum + arr[i]; 
    //}
    //cout << sum << endl ;
    average = sum/N;
    
    //cout << average << endl;;
    return average;
}

int theMaximum (int arr[], int max, int i){ 
    if (arr[i] > max){
        max = arr[i];
    }
    return max;
}

int theMinimum (int arr[], int min, int i, int N){
    for (i= 0; i < N ; i++){
        if (arr[i] < min){
            min = arr[i];
        }
    }
    return min;
}
    
double stdDev (int N, int arr[], int average){
    int totaldiff = 0;
    for( int i = 0; i < N; i++){
    difference = (arr[i] - average)*(arr[i] - average);
    totaldiff += difference; 
    standarddev = totaldiff/N;
    }
    return standarddev; 
}

int theRange ( int max , int min){
    int range; 
    
    range = max + min; 
    return range;
}
    
Last edited on
Hi,

Some questions:

Where do you set the value of N? it is not initialised anywhere.

The size of an array must be known at compile time. How does this fact impact your program?

What happens when a function returns a value? How is that value used?

What do you know about scope? For example what is the difference between a global variable and a variable declared in main() ?

Edit: What does it mean to have a local variable in a function?

Why are global variables bad?

theMinimum function is almost right (although there are some unnecessary arguments), just need to set the min to the first element in the array. Can you apply the same thinking to the theMaximum function? Hint: using a loop

The stdDev function also needs a loop, and a sum of difference squared. The difference variable is misleading here.

Well there is a start for things to look at - Good luck, and I look forward to seeing your new code. :+)
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
int theMaximum ( int ??, int N )
{
   double max = arr[?];

   for(int j = 1; j < N; ??)
   {
       if ( arr[j] > max;
          max = arr[?];
   }
    return ???;
}
you can use a binary tree , every data insert will get sorted ...
@Ericool

I imagine a BST is a bit much for the OP at the moment, and overkill if one just wants the max / min.

Regards
closed account (48T7M4Gy)
I thought the point of mentioning a binary tree is a worthwhile addition if min and max are calculated as the data goes in. Binary might be a slightly grandiose description but it is nevertheless accurate and a diligent newbie wouldn't have too much trouble implementing it.

Of course separate functions are the 'safe' way to do it probably depending on the school.
Cheers
Last edited on
Topic archived. No new replies allowed.