0

I am a newbie with C++. I am trying to make a program that can find the average, max, min, and median. I am not sure what I did wrong in my code, I am getting an error with double scores[n];:

#include <iostream>
#include<iomanip>
#include "ArrayFunction.h"

using namespace std;

int main() 
{
    int n;

    double scores[n];

    cout << "Enter the number of your scores: ";
    cin >> n;
    // creat the input array
     
    // read the elements into the array 
    for (int i = 0; i < n; i++)
    {
        cout << "Enter " << n << " scores: ";
        cin >> scores[i];
    }
    
    // call the functions and display for the result

    cout << "Average of Score is: " << calcAverage(scores, n) << endl;
    cout << "Min     : " << calcMin(scores, n) << endl;
    cout << "Max     : " << calcMax(scores, n) << endl;
    cout << "Median  : " << calcMedian(scores, n) << endl;
}

ArrayFunction.h

template <typename T>
long double calcAverage(T* arr, int size) {
    long double total = 0;
    //Compute the sum
    for (int i = 0; i < size; i++)
        total = total + *(arr + i);
    //return the average 
    return total / size;
}

template <typename T>
T calcMax(T* arr, int size) {
    //Initilize the max
    T max = arr[0];
    //Find the max out of the remaining elements
    for (int i = 1; i < size; i++)
        if (max < arr[i])
            max = arr[i];
    //Return the max element
    return max;
}

template <typename T>
T calcMin(T* arr, int size) {
    //Initilize the min element     
    T min = arr[0];
    //Find the min out of the remaining elements
    for (int i = 1; i < size; i++)
        if (min > arr[i])
            min = arr[i];
    //Return the max element
    return min;
}

template <typename T>
long double calcMedian(T* arr, int size) {
    T temp;
    //Create a temp array
    T arr2[size];
    //Copy the array
    for (int i = 0;i < size;i++)
        arr2[i] = arr[i];
    //Sort the array
    for (int i = 0;i < size - 1;i++) {
        for (int j = i + 1;j < size;j++) {
            if (arr2[i] > arr2[j]) {
                temp = arr2[i];
                arr2[i] = arr[j];
                arr2[j] = temp;
            }
        }
    }
    //IF the size of array is even then we add the mid and min+1 elements
    //compute the average
    if (size % 2 == 0) {
        return (arr2[size / 2] + arr2[size / 2 + 1]) / 2.0;
    }
    //If the size is odd the we return the middle element
    return arr2[size / 2];
}

I tried to put a number instead and got a different error:

array type T size is not assignable

I've searched and C++ does not support variable-length arrays, and that I should use std::vector instead.

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
Peter J
  • 3
  • 3
  • 1
    *c++ does not support variable-length arrays and telling me to use vector instead* [This is correct](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Here is some [good documentation on `std::vector`](https://en.cppreference.com/w/cpp/container/vector). The short version is `double scores[n];` should be `std::vector scores(n);` and all of the `T* arr`s should be `std::vector & arr`. – user4581301 Jan 16 '21 at 22:59
  • In addition, since `std::vector` knows its size, the `size` parameter can be discarded. – user4581301 Jan 16 '21 at 23:06
  • 1
    Problems are often easier to understand after you toss the fluff and reduce to a [mre]. How much of your code is *required* to reproduce your error message (as the first error reported by your compiler)? – JaMiT Jan 16 '21 at 23:59
  • Even with VLA, `n` should be initialized/set before used. – Jarod42 Jan 17 '21 at 08:51

1 Answers1

0

You can declare an array with known size at compile time! so the size must be constant!

int values[10];
int values2[] ={1,2,3};

constexpr int size = 10;
int values3[size];

const int size2 =5;
int values4[size2];

All of above examples are defined at compile time with const size!

for runtime initialization you can use pointers!

int* values = new int[1]; 
Ali Razmkhah
  • 245
  • 1
  • 8
  • 1
    I recommend adding a discussion of `std::vector` to this answer as manual dynamic allocation is [one of the last tools a C++ programmer should reach for](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new). – user4581301 Jan 16 '21 at 22:56
  • @user4581301 yes! vector or list both are good for dynamic allocation! but the problem is caused by bad c style array initiating! – Ali Razmkhah Jan 16 '21 at 22:59