GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Sep-2005, 01:49
rpgaction rpgaction is offline
New Member
 
Join Date: Sep 2005
Posts: 3
rpgaction is on a distinguished road

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:
#include <iostream>
using std::cout;
using std::cin;

class Average
{
public:
	double numbers;
	int x;
	int numsmone;

	Average	(int nums)
	{
		numsmone = nums - 1;	
	
		for (x = 0; x < nums; ++x)
		{
			if (x == 0)
				cout << "Enter the 1st number: ";
			else if (x == 1)
				cout << "Enter the 2nd number: ";
			else if (x == 2)
				cout << "Enter the 3rd number: ";
			else
				cout << "Enter the " << x << "th number: ";
			cin >> numbers[x];
		}
	}

	double CalculateMean ()
	{
		double mean;
		for (x = 0; x < numsmone; ++x)
			mean = numbers[x] + numbers[x+1];
		mean = mean / (numsmone + 1)
		return mean;
	}
};

int main ()
{
	int nums;
	cout << "Enter the number of numbers you wish to average: ";
	cin >> nums;
	Average Avgone(nums);
	cout << "The mean is " << Avgone::CalculateMean;
	cin.get();
	return 0;
}
  #2  
Old 01-Sep-2005, 02:11
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
You will have to make numbers an array of double, like:

CPP / C++ / C Code:
double numbers[size];

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:
	double * numbers;
	Average  (int nums) : numbers(new double[nums])
	{
		numsmone = nums - 1;
		//...
	}

In the constructor initialization list that is. That is similar (no, it's not) to:

CPP / C++ / C Code:
		numbers = new double[nums];

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:
cout << "The mean is " << Avgone::CalculateMean;

Don't use the resolution operator (::) for non-static member functions; use . instead. And call the function with the parenthesis:

CPP / C++ / C Code:
	cout << "The mean is " << Avgone.CalculateMean();

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  
Old 01-Sep-2005, 02:24
rpgaction rpgaction is offline
New Member
 
Join Date: Sep 2005
Posts: 3
rpgaction is on a distinguished road
Thanks, it compiles now... However, there is a minor problem... The mean always, no matter what input, comes out as 2. :-?
  #4  
Old 01-Sep-2005, 02:40
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by rpgaction
Thanks, it compiles now... However, there is a minor problem... The mean always, no matter what input, comes out as 2. :-?

Not for me...
But the algorithm is flawed:

CPP / C++ / C Code:
		numsmone = nums - 1;

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:
for (x = 0; x < numsmone; ++x)
	mean = numbers[x] + numbers[x+1];
mean = mean / (numsmone + 1);

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:
for (x = 0; x < numsmone + 1; ++x)
	mean += numbers[x];
mean = mean / (numsmone + 1);

Regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #5  
Old 01-Sep-2005, 02:44
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
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:
Enter the 3rd number: 5 Enter the 3th number:

I suggest you change the printing:

CPP / C++ / C Code:
//...
			else
				cout << "Enter the " << x + 1 << "th number: ";


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  
Old 01-Sep-2005, 03:25
rpgaction rpgaction is offline
New Member
 
Join Date: Sep 2005
Posts: 3
rpgaction is on a distinguished road
Thanks
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.