Page 1 of 1
Pointers - help!
Posted: 20 Nov 2005, 00:18
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...
Posted: 20 Nov 2005, 06:04
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.
Posted: 20 Nov 2005, 06:17
by krogothe

silly me! thanks!
Posted: 20 Nov 2005, 06:52
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.
Posted: 20 Nov 2005, 07:18
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

Posted: 20 Nov 2005, 21:31
by sp2danny72
learn to use the debugger...
stepping thru the code wold have revealed the problem
Posted: 20 Nov 2005, 21:56
by jcnossen
And also learn to delete[] your fancy dynamically allocated array

Posted: 21 Nov 2005, 00:09
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

Posted: 21 Nov 2005, 00:10
by renrutal
Learn how to avoid divisions by zero too.
BTW, are these new int[] arrays allocated dynamically?
Posted: 21 Nov 2005, 00:52
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??
Posted: 21 Nov 2005, 02:02
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...
Posted: 26 Nov 2005, 23:08
by el_muchacho
krogothe wrote:
Soon ill be tH3 1337 Pr0 H4x0r pr09r4mm0r 0f d00m

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.
Posted: 27 Nov 2005, 15:54
by krogothe
... as i have lately found out
