0

I am getting an error when i run this code. The error code is 0xC00000FD and I can't seem to find a fix for what i need. Can someone please help me? Here's the code:

#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
    int n;
    int arr[n];
    int i;
    int sum=0, avg=0;

    cout<<"array lengh: ";

    cin>>n;
    cout<<"Enter "<<n<<" array elements: ";
    for(i=0; i<n; i++)
    {
        cin>>arr[i];
        sum = sum + arr[i];
    }
    cout<<"\nThe array elements are: \n";
    for(i=0; i<n; i++)
    {
        cout<<arr[i]<<"  ";
    }
    cout<<"\n\nSum of all elements is: "<<sum;
    avg = sum/10;
    cout<<"\nAnd average is: "<<avg;
    getch();

    return 0;
}

  • 1
    `0xC00000FD` is a stack overflow. – drescherjm Dec 14 '20 at 19:18
  • 3
    Use `std::vector`. – dedObed Dec 14 '20 at 19:19
  • 1
    Variable length arrays are not part of the C++ Standard (they may be available via a compiler extension). Use `std::vector` instead. – Richard Critten Dec 14 '20 at 19:19
  • `int n;` then `int arr[n];` note that this is illegal in standard `c++` however on compilers that support what is the value of `n` here? Maybe you want to read the value of `n` first. – drescherjm Dec 14 '20 at 19:19
  • 5
    Even if your compiler supports VLA, n is undefined when you try to use it as size to the array. Turn your compiler’s warnings to the max – Sami Kuhmonen Dec 14 '20 at 19:20
  • 3
    `int arr[n];` is undefined behavior. Even if VLS's were standard, accessing an uninitilized variable is _UB_. Voting to close this quesiton as typo. – πάντα ῥεῖ Dec 14 '20 at 19:20
  • Numbers in `[]` brackets in a array declarations need to be constant expressions. I wonder why your compiler does not complain about this. If you really need to allocate a array of a size determined at runtime, you'll need to use `int* arr; /* read n */ arr = new int[n]; /* use arr ... */ delete[] arr;` – fabian Dec 14 '20 at 19:26
  • 1
    To improve your question itself, please insert the complete and exact error message. Not everyone is willing to look up error codes particularly when s/he has to guess the compiler. – bjhend Dec 14 '20 at 19:33
  • use std::vector – Jacob Dec 14 '20 at 20:22

2 Answers2

2

There are two major problems in your code. First, C++ does not have "variable length arrays" (VLAs), although some compilers support them as an extension.

More importantly, even if your compiler does support them, you are attempting to define the size of your arr before you have read the value of n.

To fix these issue you should: (a) use the std::vector container, rather than a plain array; and (b) only declare that after you have read in the value of n.

Here's a 'quick fix':

#include <iostream>
#include <conio.h>
#include <vector> // For the "std::vector" definition

using namespace std;

int main()
{
    int n;
//    int arr[n]; // Here, the value of "n" is undefined!
    int i;
    int sum=0, avg=0;

    cout<<"array lengh: ";

    cin>>n;
    std::vector<int> arr(n); // This can now be used 'almost' like a plain array
    cout<<"Enter "<<n<<" array elements: ";
    for(i=0; i<n; i++)
    {
        cin>>arr[i];
        sum = sum + arr[i];
    }
    cout<<"\nThe array elements are: \n";
    for(i=0; i<n; i++)
    {
        cout<<arr[i]<<"  ";
    }
    cout<<"\n\nSum of all elements is: "<<sum;
    avg = sum/10;
    cout<<"\nAnd average is: "<<avg;
    getch();

    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Also, your `avg = sum/10;` would be more 'correct' as `avg = sum/n;` (although integer arithmetic is probably not ideal, here, and declaring `avg` and `sum` as `double` types would be better). – Adrian Mole Dec 14 '20 at 19:30
0

Standard C++ does not support variable length arrays. std::vector can accomplish what you want. It is also possible to allocate memory of the desired size with new int[size] and use a pointer to reference the data rather than an array. However, this doesn't have any bounds checking, so there is the possibility of memory corruption. And the allocated memory must be explicitly freed with delete[]. So std::vector is my preferred solution.