-2

I've been trying for a while to do a specific problem that our teacher gave us at school if we wanted to earn a good grade but I couldn't do it. But now, I'm actually very interested in solving this.


{
int i,n,a[100],c;
cout <<"n=";
cin >>n;
  for(i=1;i<=n;i++)
  {
    cout <<"a["<<i<<"]=";
    cin>> a[i];
  }
  for (i=2;i<=n-1;i++)
    if (a[i]/10==0)
  {
      c=a[i];
      a[i]=a[i-1]+a[i+1];
      a[i-1]+a[i+1]=c;
  }
    for (i=1;i<=n;i++)
    {
        cout<<"a["<<i<<"]="<<a[i]<<endl;
    }
    return 0;
}

ERROR AT: a[i-1]+a[i+1]=c; lvalue required as left operand of assignment.

The questions I have to solve with this once it's done are, for example: What will be shown for n=5 and the vector a=(22,4,10,5,16)?

Hi, Richard. I actually don't want anything to happen on that line but the reason for why that error occurs. What I'm doing with that part of the code is to apply the interchange algorithm, so that, for example, when the code is finished and I'll have to press F9 in the CodeBlocks Application, there will be a chosen n, which the problem tells me that it's 5, and then I'll have to pick the 5 numbers shown in the a vector which are 22,4,10,5,16 ex: a[1]=22, a[2]=4, a[3]=10, a[4]=5, a[5]=16.. When all of that is done, the application has to change my a[1] with a[2], so a[1] will have the value of a[2] and so on.

skZTM
  • 1
  • 1
  • It's not a `1value`, it's an `lvalue`. Make sure you *copy* the compiler message, not just re-type it – Justin May 25 '17 at 21:40
  • Please explain what you want to happen on this line: `a[i-1]+a[i+1]=c;` Then we can suggest how to do it, at the moment it is just nonsense. – Richard Critten May 25 '17 at 21:43
  • 1
    Think of it like this: 4 + 9 = 62. Take 4 and 9, add them, and assign to the result 62. Doesn't make much sense to turn 13 into 62 before you even do anything with 13. Helpful reading: [What are rvalues, lvalues, xvalues, glvalues, and prvalues?](https://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues) – user4581301 May 25 '17 at 21:44
  • Off topic: give variables readable, descriptive names. It makes it a lot easier to debug even your own code. They don't have to be long-winded names, but they should give a casual reader some idea of what the variable contains and how it should be used. – user4581301 May 25 '17 at 21:46
  • Your code is imperative and mostly non-descriptive. It would be far more easier if you start with describing the actual problem before we take a look at the code. No way for us to spot a semantic error if we don't know what this is supposed to solve. – Aziuth May 25 '17 at 21:48
  • Given that you have `a[i] = a[i-1] + a[i+1];` (only you've been unnecessarily parsimonious with the spaces), maybe the next line should be `a[i-1] = a[i+1] = c;`? It makes some sort of sense, at any rate, and the code would then compile. – Jonathan Leffler May 25 '17 at 21:51
  • An `lvalue` is short for 'left value' and is simply something that can appear on the left side of an assignment. It is something that identifies a series of memory locations which can be written to. Expressions like `x + y` cannot be assigned to; neither can `a[i-1] + a[i-1]`, though each of `x`, `y`, `a[i-1]` and `a[i+1]` is an lvalue (making some not very surprising choices for the definitions of the variables). – Jonathan Leffler May 25 '17 at 21:57
  • What if `n` is greater than `100` then welcome to UB. – Raindrop7 May 25 '17 at 22:40
  • Try again to specify what your program is supposed to do. For example what output do you expect to see for n=5, a=22,4,10,5,16 ? – M.M May 26 '17 at 00:22

1 Answers1

1

You are assigning a value to a constant: a[i-1]+a[i+1]=c; Because the statement above doesn't return an address of the element but the value in that address which is of course constant. It is like you write:

5 = n;
  • To solve your problem then:

     (a[i-1]+a[i+1])=c; becomes: 
     c = (a[i-1]+a[i+1]); // because c is not a constant
    
Raindrop7
  • 3,805
  • 3
  • 14
  • 27
  • That would compile, I do not think it solves the problem though (value of `c` is never used which suggests the algorithm does not do what is intended) – M.M May 26 '17 at 00:20