0

I am wondering why the following program produces this error. I have tried on the online gdb compiler and it works fine. I have searched it online and some recommended to use vector instead of array, but i can't since i am required to use array here. Could anyone suggest some advices for my program in order for it to work?

I have to submit this program via VPL, in which it is run on linux and has a -pedantic errors flag.

The program will output the variance (var) from the user input.

#include <iostream>
using namespace std;

double jum(double arrayz[], int y) {
   double total=0, jumlah;
   for (int i=0; i<y; ++i) {
       total+=arrayz[i];
   }
   jumlah = total/y;
   return jumlah;
}
double rata2(double arrayz[], int y) {
   double total=0,average;
   for (int i=0; i<y; ++i) {
       total+=arrayz[i];
   }
   average = total/y;
   return average;
}
int main() {
    double data,average,jumlah;
    int y;
    cin >> y;
    double arrayz[y]={};
    for (int i=0; i<y; ++i) {
        cin >> data;
        arrayz[i]=data;
    }
    average=rata2(arrayz, y);
    for (int i=0; i<y; ++i) {
        arrayz[i]=(arrayz[i]-average)*(arrayz[i]-average);
    }
    jumlah=jum(arrayz, y);
    cout << "Variance : " << jumlah;
    return 0;
}

Any answer will be deeply appreciated!

Blaze
  • 16,736
  • 2
  • 25
  • 44
piacevole
  • 13
  • 1
  • 5
    Not sure how the error is unclear. `double arrayz[y]` is a VLA, and those are forbidden in standard C++ – NathanOliver Mar 05 '19 at 15:02
  • Yeah, GCC accept them as an extension. It doesn't mean it's standard nor you should use them. – Guillaume Racicot Mar 05 '19 at 15:02
  • 2
    What do you mean, you are required to use an array? At worst, allocate with `new double[y]` and then delete the data. That's what your teacher probably wants you to learn. He doesn't want you to learn VLA, but memory manual management. – Matthieu Brucher Mar 05 '19 at 15:03
  • related: https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard/1887178#1887178 – 463035818_is_not_an_ai Mar 05 '19 at 15:30

1 Answers1

1

If you really can't use std::vector, then just do this instead:

double *arrayz = new double[y];

And then, at the end of the program, free the memory again:

delete[] arrayz;

I am wondering why the following program produces this error. I have tried on the online gdb compiler and it works fine.

It's true, VLAs aren't actually part of C++, but some compilers will allow it nevertheless, as a compiler extension. And those that do allow it can usually be configured to give warnings when you do that, as you learned with -pedantic.

But can you really not use std::vector, though? It seems weird to be required to write a C++ program, but not be allowed to use such a basic container. In that case the program might as well be in C, where VLAs are allowed. You would make a vector like this:

std::vector<int> arrayz(y);

And change the function signatures to take a reference to one like this:

double jum(std::vector<int> &arrayz, int y) {
double rata2(std::vector<int> &arrayz, int y) {
Blaze
  • 16,736
  • 2
  • 25
  • 44
  • 2
    Formally, a compiler is required to "issue a diagnostic" for ill-formed code. Having done that, it is free to do pretty much anything. That's the hook for compiler extensions. – Pete Becker Mar 05 '19 at 15:43