Pointers - help!

Pointers - help!

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

Post Reply
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Pointers - help!

Post by krogothe »

I have read on pointers and even made a program as described in the book that uses them. The program was a failure and I have no idea why!
The program is supposed to show how you can send an array into a function without making a loop to run the function once per value...

Code: Select all

#include <iostream>

//Program to calculate the average of 5 elements, as per copied from the book

double Average (int *ListValues, int Elements);

void main ()
{
	int Values[5];
	int NumberValues;
	for(NumberValues=0;NumberValues<5;NumberValues++)
	{
		std::cout<<"Enter Value\n";
		std::cin>>Values[NumberValues];
	}
	double AverageValues;
	AverageValues = Average (Values, 5);
	std::cout<<"The average of all values is "<<AverageValues<<"\n";
}

double Average (int *ListValues, int Elements)
{
	double Total = 0;
	int NumberElement;
	for(NumberElement=0 ; NumberElement < Elements;NumberElement++);
	{
		Total += ListValues[NumberElement];
	}
	return Total / Elements;
}
Unless i made an obscure spelling error somewhere i have no clue whats up (except that the book is for an older Visual Studio, but they should have backwards compatibility). It compiles and runs as expected but the average is wrong!
My skills are almost up to scratch to start heavy coding, just need to brush up on pointers and classes, since i know most operators, variable and array syntax etc...
User avatar
ILMTitan
Spring Developer
Posts: 410
Joined: 13 Nov 2004, 08:35

Post by ILMTitan »

The problem is the semicolin at the end of your for loop.
Because of the semicolin, the for loop thinks it has no body.
Thus, the for loop itererates NumberElement up to 5, and then adds ListValues[5] to Total, which is beond the range of ListValues, and could return anything.
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

:shock: silly me! thanks!
User avatar
ILMTitan
Spring Developer
Posts: 410
Joined: 13 Nov 2004, 08:35

Post by ILMTitan »

Stylelisticly, I tend to like to put the opening curly brace at the end of the line. It uses less space and it tends to make these types of errors more obvious. Then again, it my just be my experiance with Java Style.
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

I have been learning to code all day! Just today i have read over 450 pages in two different c++ books, made some 10 simple programs, 2 huge spreadsheets and many A4 pages full of algorithm planning...
In fact, Ive been at it for 18 hours straight, from midday till 6AM (current time), so i'm doing some really silly things!
At least now i know about most of the c++ syntax, being able to actually read the AIs and see how they work. One or two more days at this rate and i should have mastered those damn pointers and evil classes (i still think a bit too procedurally)...
I'm off to bed now, thanks for pointing me (pun intended) in the right direction!



EDIT: see? im getting better now! a dynamically allocated array, with the use of the std namespace:

Code: Select all

#include <iostream> 
using namespace std;
//Program to calculate the average of X elements

double Average (int *ListValues, int Elements); 

void main () 
{ 
   int ArraySize;
	int NumberValues; 
	cout<<"Enter Array Size\n"; 
    cin>>ArraySize; 
	int *PointArray = new int[ArraySize];

   for(NumberValues=0;NumberValues<ArraySize;NumberValues++) 
   { 
      cout<<"Enter Value\n"; 
      cin>>PointArray[NumberValues]; 
   } 
   double AverageValues; 
   AverageValues = Average (PointArray, ArraySize); 
   cout<<"The average of all values is "<<AverageValues<<"\n"; 
} 

double Average (int *ListValues, int Elements) 
{ 
   double Total = 0; 
   int NumberElement; 
   for(NumberElement=0 ; NumberElement < Elements;NumberElement++)
   { 
      Total += ListValues[NumberElement]; 
   } 
   return Total / Elements; 
}
Soon ill be tH3 1337 Pr0 H4x0r pr09r4mm0r 0f d00m :lol:
User avatar
sp2danny72
Posts: 60
Joined: 09 Jan 2005, 04:52

Post by sp2danny72 »

learn to use the debugger...
stepping thru the code wold have revealed the problem
User avatar
jcnossen
Former Engine Dev
Posts: 2440
Joined: 05 Jun 2005, 19:13

Post by jcnossen »

And also learn to delete[] your fancy dynamically allocated array :wink:
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

I knew about the delete[] bit but it thought VS 2005 had automatic garbage collection or something...
Once i learn the basics of the language fully Ill start learning how to debug!
you guys just wait :twisted:
renrutal
Posts: 84
Joined: 28 Apr 2005, 16:45

Post by renrutal »

Learn how to avoid divisions by zero too.

BTW, are these new int[] arrays allocated dynamically?
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

Yeah they must be since the user can specify the size of the array!
this brings me to another point!

Suppose i want to do a type check on this bit of code:

Code: Select all

   for(NumberValues=0;NumberValues<ArraySize;NumberValues++) 
   { 
      cout<<"Enter Value\n"; 
      cin>>PointArray[NumberValues]; 
   } 
So the user wont type negative numbers or a number too big for an int. Do i have to do this:

cin>>variable
if (variable is type safe)
PointArray[NumberValues] = variable
else
error message and ask for retype


or is there a simpler workaround??
User avatar
jcnossen
Former Engine Dev
Posts: 2440
Joined: 05 Jun 2005, 19:13

Post by jcnossen »

I knew about the delete[] bit but it thought VS 2005 had automatic garbage collection or something...
Only if you would actually use the .NET framework, which you are not. I'm not very familiar with it, I only know that managed C++ is something sensible people who want to make portable applications should not touch :)

And no, that's about the simplest way to check a number...
el_muchacho
Posts: 201
Joined: 30 Apr 2005, 01:06

Post by el_muchacho »

krogothe wrote: Soon ill be tH3 1337 Pr0 H4x0r pr09r4mm0r 0f d00m :lol:
I'm affraid before becoming the H4x0r pr09r4mm3r in C++, you'll need to practice for a few months to gain experience as C++ is packed with subtle traps and pitfalls.
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

... as i have lately found out :P
Post Reply

Return to “AI”