-1

I am making a simple program, please take a look

#include<iostream>

using namespace std;

int main(int argc,char* argv[])
{

    int op=0;
    int v[20]=[1, 0];   
    float Ma=0;

    if (argv[1]==1)
    {
        float S=0;
        for(int i=0;i<=20;i++)
        {
            S=S+v[i];
        }
        Ma=(double)(S/20);
    }


    cout<<"Media aritmetica pentru elementele din vector este "<<Ma<<endl;
    return 0;
}

I get this error Program.cpp:10:13: error: expected identifier before numeric constant

int v[20]=[1, 0];
         ^

I am using the gcc from ubuntu for compiling and I'm not really sure if there's anything to it that may cause this. I am a bit new into this.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 3
    It's not valid syntax. Where did you learn that this is how you initialize arrays? – UnholySheep Jul 14 '18 at 21:21
  • I have learned C++ in highschool and some of it in my uni but I forgot a lot of it. I may have made a few mistakes or more :/ – Radu Bojica Jul 14 '18 at 21:22
  • 3
    In that case you should consider [picking up a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), programming by guessing is not a good way to write code – UnholySheep Jul 14 '18 at 21:23
  • 1
    `argv[1]` is not an int, it's a C-style string (pointer to a null-terminated sequence of characters). It makes no sense to compare it to 1 and your compiler should have warned you about that. It's a good idea to turn on all compiler warnings and read them every time you compile. – eesiraed Jul 14 '18 at 22:56
  • Ok. Thanks. I will look more into it. – Radu Bojica Jul 14 '18 at 23:39

2 Answers2

1

You probably meant to define an array of 20 int's and initialize its first 2 elements to 1 and 0 respectively.

Well, instead of writing:

int v[20] = [1, 0];

you should have written:

int v[20] = {1, 0};

which means what you want it to mean. Note, however, that the term "vector" has a different meaning typically in C++ - the name of the std::vector container class in the standard library.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
0

Use curly braces instead.

int v[20]={1,0};
aaronm2112
  • 27
  • 4