Error :invalid types 'Double[int]' for array subscript

Hello, I am a beginner.I am getting 2 errors.Please help to solve them.
The program first takes the readings ..then gives the average .After that it subtracts the average of the readings with the readings and then finally gives that out.

The error is in the 39th and 47th line .It is :
Error :invalid types 'Double[int]' for array subscript


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cmath>

using namespace std;

int main()

{

double r[11];
double ravg;
double sum = 0;
int i;
double dravg;
double drsum = 0;
double dr;

cout << "Enter the readings :\n" << endl;

for(i = 0; i < 10 ; i ++)
{
    cin >> r[i];      // 1. lets the user enter readings.
}

for(i = 0; i < 10; i++)
{
    sum = (sum + r[i]); // 2. for the average of the readings.
}

ravg = sum /10;

cout << "The average of the readings is " << ravg << endl;
cout << " ....\a\a\a\a\n";
cin.get();

for(i = 0; i<10; i++)
{

abs(dravg[i]) = ravg - r[i];  // Line 39

//3.(the average of the readings) - every reading = dr

}

for(i = 0; i < 10 ; i++)
{
    drsum = drsum + dravg[i];  // average of dr see comment no.3  ( line 47)
}

dravg = drsum /10;

cout << " The dravg = " << dravg << endl; //outputs dravg.
cin.get();


return 0;
}


Thank You.
Last edited on
First of all you declared array r as having 11 elements but everywhere you are using only 10 elements of the array.

It would be much better if you would declare a corresponding constant. For example

1
2
3
4
5
6
7
const unsigned int N = 10;
double r[N];

for( unsigned int i = 0; i < N ; i++ )
{
   cin >> r[i]; // 1. lets the user enter readings.
}


As for the errors then there was not declared such an array as dravg so you may not use the subscript syntax dravg[i]

abs(dravg[i]) = ravg - r[i]; // Line 39

Also expression abs(dravg[i]) is an rvalue (that is a temporary expression). You may not assign a value to it.


Last edited on
Also there is code tags (looks like <> on right of post creating/editing field)
If you edit your post and include your code inside code tags, ut would be easier to read.
Last edited on
Topic archived. No new replies allowed.