Skip to main content
GameDev.net gamedev.net
🔒 Locked

What's wrong with my code?

Started by David McCaleb Oct 21, 2000 at 12:44 PM 11 replies 900+ views
Original Post
David McCaleb
David McCaleb
My code is suppost to return point that has the largest value, instead it always returns 0 why?
        
//Here is the point class:

class Point
{
public:
	Point(unsigned short x = 0, unsigned short y = 0, short value = 1): itsx(x), itsy(y), itsvalue(value)  {}
	~Point() {}
	unsigned short GetItsX() {return itsx;}
	void SetItsX(unsigned short x) {itsx = x;}
	unsigned short GetItsY() {return itsy;}
	void SetItsY(unsigned short y) {itsy = y;}
	short GetItsValue() {return itsvalue;}
	void SetItsValue(short value) {itsvalue = value;}
private:
	unsigned short itsx;
	unsigned short itsy;
	short itsvalue;
};

//Here is the function:

Point * Largest(Point * pointlist)
{
	int n = sizeof pointlist/sizeof pointlist[0];
	int max = 0;
	
	for(int i = 0; i < n; i++)
	{
		if(pointlist<i>.GetItsValue() == -1)
			max = i;
		if(pointlist[i].GetItsValue() > pointlist[max].GetItsValue())
			max = i;
	}
	return &pointlist[max];
}
        
Any help would be appreciated. Edited by - David McCaleb on 10/21/00 1:10:36 PM
Shannon Barber
Shannon Barber
close VC, delete the debug directory, reopen and do a full recompile


I have problems like this all the time
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
David McCaleb
David McCaleb
I did that but now it returns the point with the value -1 instead of the largest point(?)

Edited by - David McCaleb on October 21, 2000 2:04:18 PM
Phorisc
Phorisc
pointlist.GetItsValue() == -1

what are these angle brackets?
shouldnt they be [ ] brackets?

-Phorisc
David McCaleb
David McCaleb
I typed in square brackets, the post got screwed.
Bruce
Bruce
Hi David,

I''ve seen two things in your code:
1. pointlist should be an array:

Point* Largest(Point* pointlist[])
...
int n=sizeof pointlist[]/...
...
if(pointlist... should be:
if pointlist... five lines lower.

2. Largest is declared as getting a pointer as parameter and returning a parameter. Since pointlist is a pointer, you should
return pointlist[max]; instead of
return &pointlist[max];

Please don''t mind if I overlooked something, it''s some time ago since I''ve last seen C++ code.

Yours

Bruce
David McCaleb
David McCaleb
That doesn''t work, now I tried returning pointlist + max. That doesn''t work either.
Megahertz
Megahertz
Have you tried stepping thu the code?
-=[Megahertz]=-
djsteffey
djsteffey
you put
    
int n = sizeof pointlist/sizeof pointlist[0];


sizeof pointlist is always 4. because pointlist is a pointer and a pointer is 4 bytes


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Xai
Xai
Ok, I see one absolutely obvious C++ error - what do you think this line does?

    
int n = sizeof pointlist/sizeof pointlist[0];


the sizeof operator cannot do what you want it to do. sizeof(pointlist) returns the size of a pointer ... which will be 4, it does NOT return the size of the allocated array. C++ arrays do NOT contain any embedded size information. So you need to pass the size seperately.


I will post in a seperate post (due to the one source block rule), the way i think the function should read.
Xai
Xai
I see ncsu beat me to the punch

here's my version of the function

        
Point* Largest(unsigned numPoints, Point pointList[])
{
short maxValue = SHRT_MIN; // this constant comes from limits.h

unsigned maxIndex = 0;

if(numPoints == 0)
return NULL;
else
{
for(unsigned i=1;i<numPoints;++i)
{
if(pointList<i>.GetItsValue() > maxValue)
{
maxIndex = i;
maxValue = pointList[maxIndex].GetItsValue();
}
}

return &(pointList[maxIndex]);
}
}

Edited by - Xai on October 21, 2000 4:49:04 PM

Edited by - Xai on October 21, 2000 4:51:53 PM
Zipster
Zipster
quote:
Stepping though the code?


In MSVC, or maybe other compilers, under the build menu, you can chose to step through your program (under Debug Build submenu). F11 is the hot-key.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.