![]() |
|
#1
|
|||
|
|||
C++ Syntax Problem (Arrays and Loops)I'm doing exercises out of a book for self-learning purposes, and one of the exercises is:
Design, implement and test class Average. An object of type Average should accept values that are put into it. At any point in time it should be possible to retrieve their cumulative arithmetic average. Well, I think (or, at least, hope) that my math is right in the sense that this is what I would have to do in order to complete the calculations, but my compiler (Dev-C++) is giving me a ton of errors saying "invalid types double[int] for array subscript". I know this is referring to the loops where I have numbers[x], but it just gives me the error "invalid types double[double] for array subscript" when I try changing x to the type double. I really have no clue as to how to get around this: CPP / C++ / C Code:
|
|||
|
#2
|
||||
|
||||
|
You will have to make numbers an array of double, like:
CPP / C++ / C Code:
But I see you want to dynamically allocate it to the required size (which is a very good practice for member data). So make it a pointer and dynamically allocate it in the constructor: CPP / C++ / C Code:
In the constructor initialization list that is. That is similar (no, it's not) to: CPP / C++ / C Code:
But the proper way to do it is in the initialization list, like in my previous example. Also, pay attention to this line: CPP / C++ / C Code:
Don't use the resolution operator (::) for non-static member functions; use . instead. And call the function with the parenthesis: CPP / C++ / C Code:
Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#3
|
|||
|
|||
|
Thanks, it compiles now... However, there is a minor problem... The mean always, no matter what input, comes out as 2. :-?
|
|
#4
|
||||
|
||||
|
Quote:
Not for me... But the algorithm is flawed: CPP / C++ / C Code:
You discard the last number; why? The function you use for computing the mean adds numbers in the array 2 times: CPP / C++ / C Code:
You will add numbers[0] + numbers[1], and at the next cycle, numbers[1] + numbers[2]. I rewrote it and added 1 to numsmone, so the algo will also use the last value in the array: CPP / C++ / C Code:
Regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#5
|
||||
|
||||
|
Also, note that you have an error in your printing, because in C++ the array subscript starts with 0, and your function will print
Code:
I suggest you change the printing: CPP / C++ / C Code:
Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#6
|
|||
|
|||
|
Thanks
![]() |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The