# Thread: Need help with program using pointers !!!

1. ## Need help with program using pointers !!!

Code:
```//This program asks the user to pick the number of test
//scores they wish to average, drops the lowest(returning
//'lowestscore'and showing it) and then it calculates the
//average minus the lowestscore).  Could someone please
//take a look and let me know how to fix it.  THANKS !!!

#include <iostream>
using namespace std;

int calcaverage(int *[], int *[]);
int findlowest(int *[], int *[]);

int main()
{
int HowMany;
cout<<"How many scores do you wish to input?";
cin>>HowMany;
int *Scores = new int[HowMany];
cout<<"Enter your "<<HowMany<<" scores.\n";
for (int i=0; i<HowMany; i++)
{
cout<<"\t"<<i+1<<": ";
cin>>Scores[i];
}
return 0;
}

//This function finds the average.
int calcaverage(int HowMany, int *Scores)

{
int a=findlowest(int HowMany, int *Scores);

int total=0;
int average=0;
{
for(int i=1; i<HowMany; i++)
total += Scores[i];
}

total -= a;
HowMany--;
average = total/HowMany;
cout<<"Your test average is: "<<average;
}

//This function finds the lowest score and returns it.
int findlowest(int HowMany, int *Scores)
{
int lowestscore = Scores[0];
{
for(int i=1; i<HowMany; i++)

if(Scores[i]<lowestscore) lowestscore=Scores[i];
}
return lowestscore;
}```

2. You declared functions to take arrays of pointers...
That is not what you want since in main() you allocated array of integers.
Then when calling function you shouldn't use name types.
Rewrite prototypes and use correct way to call functions adn then we'll se what to do next...

3. 1) The type of an array includes it's dimension:
Code:
```int calcaverage(int *[], int *[]);
int findlowest(int *[], int *[]);```
Each of your parameters is an array of pointers to type integer, i.e. several pointers to type integer stored in an array. First, I don't think that's what you want, and if it was, you have to specify the dimension of the array. The dimension is part of the type of the array.

2)The types of the parameters in a function declaration have to match the parameter types in the definition:
Code:
```int calcaverage(int *[], int *[]);
int calcaverage(int HowMany, int *Scores)

{
int a=findlowest(int HowMany, int *Scores);
...
...```
The types of the function parameters in the declaration do not match the types of the function parameters in the definition. The first parameter was incorrectly declared an an array of pointers, but in the definition, you list the type as int. The second parameter was incorrectly declared as an array of pointers, and in the definition, you list the type as a single pointer to type int.

4. ## What about now??????????????????????

Code:
```//I changed it.  I'm new to pointers and need a bit of help with this.  This was programmed with MS C++ 6.0, if that helps.

//This program asks the user to pick the number of test
//scores they wish to average, drops the lowest(returning
//'lowestscore'and showing it) and then it calculates the
//average minus the lowestscore).  Could someone please
//take a look and let me know how to fix it.  THANKS !!!

#include <iostream>
using namespace std;

int calcaverage(int *, int *);
int findlowest(int *, int *);

int main()
{
int HowMany;
cout<<"How many scores do you wish to input?";
cin>>HowMany;
int *Scores = new int[HowMany];
cout<<"Enter your "<<HowMany<<" scores.\n";
for (int i=0; i<HowMany; i++)
{
cout<<"\t"<<i+1<<": ";
cin>>Scores[i];
}
return 0;
}

//This function finds the average.
int calcaverage(int HowMany, int *Scores)

{
int a=findlowest(int HowMany, int *Scores);

int total=0;
int average=0;
{
for(int i=1; i<HowMany; i++)
total += Scores[i];
}

total -= a;
HowMany--;
average = total/HowMany;
cout<<"Your test average is: "<<average;
}

//This function finds the lowest score and returns it.
int findlowest(int HowMany, int *Scores)
{
int lowestscore = Scores[0];
{
for(int i=1; i<HowMany; i++)

if(Scores[i]<lowestscore) lowestscore=Scores[i];
}
return lowestscore;
}```

5. Again, look at your function call int a=findlowest(int HowMany, int *Scores);

Read this: http://www.cplusplus.com/doc/tutorial/tut2-2.html

6. Code:
```int calcaverage(int *, int *);  //declaration
int calcaverage(int HowMany, int *Scores)  //definition
{
...
...
}```
Do the types in red match? Look at the types in blue--they do match. I suggest you do this:

1) Do not declare your functions
2) Write your functions
3) After you write your functions, copy the function header and paste it where you would place the function declaration.
4) Type a semi colon after the function header you pasted.

Here is an example:

Step 1) and 2):
Code:
```#include <iostream>
using namespace std;

int main()
{
int result = someFunc(1, 2);
cout<<result<<endl;

return 0;
}

int someFunc(int a, int b)
{
return a+b;
}```
Steps 3) and 4):
Code:
```#include <iostream>
using namespace std;

int someFunc(int a, int b);

int main()
{
int result = someFunc(1, 2);
cout<<result<<endl;

return 0;
}

int someFunc(int a, int b)
{
return a+b;
}```
Here is some basic information on pointers:

All variable names and their corresponding values are stored in memory somewhere. The location in memory where they each reside is denoted with an address. An address is somewhat scary looking:

006BFDF4

but you can just think of it as a mailbox number. If you look inside the mailbox with that address, you'll find the variable and its value. A pointer is a variable that stores the address of another variable.

If you declare an int like so:

int num = 10;

that line stores the variable num with a value of 10 somewhere in memory. We don't know where it is in memory yet. However, there is an "address of" operator which will get the address of num, e.g.

cout<<&num<<endl;

You can store that address in a special variable called a pointer:

int* p;

That declares a variable named p, and p is a variable of type "pointer to int". Essentially, you read the line in reverse. I suggest you adopt the notation where the * is adjacent to int--not adjacent to p:

int *p;

The first notation will make it clear to you that the type is int*, which is "pointer to int", and p is the variable name.

Now, you can assign the address of any int to p:

p = &num;

which reads: "p equals the address of num".

Finally, you can also use a pointer to get the value stored at that address by putting a little star in front of the pointer name:

cout<<*p<<endl;

7. I hope the explanation is useful.

In your case you don't need to return values so why even bother?
Code:
```//This program asks the user to pick the number of test
//scores they wish to average, drops the lowest(returning
//'lowestscore'and showing it) and then it calculates the
//average minus the lowestscore).  Could someone please
//take a look and let me know how to fix it.  THANKS !!!

/* You don't necessarily need to pass anything back to main.
It is probably better to calculate the lowest value first
and then call the function calculate average.
So no values are returned to main
*/

#include <iostream>

//functions
//The inside of the brackets tells you
//how many variables are being passed and
//if they are integers floats or arrays.
//Don't forget the ';'

int calcaverage(float [],int,float);
int findlowest(float [],int );

using namespace std;

int main()
{
float Scores[81];
int HowMany;
cout<<"How many scores do you wish to input?";
cin>>HowMany;

cout<<"Enter your "<<HowMany<<" scores.\n";
for (int i=0; i<HowMany; i++)
{

cin>>Scores[i];
}
//Calls the function 'findlowest' passing the array
//'Scores' into it and the variable 'howmany'
//When calling functions you don't need to declare
//if they are integers or if they are arrays or their size
//Don't forget the ';' at the end of the bracket
findlowest(Scores,HowMany);

int stop;
cin>>stop;
return 0;
}

//This function finds the lowest score and returns it.
//Here you must declare what type they are
//For an array such as Scores you need to write float Scores[]
//The square brackets are needed/
//Note here you don't need the ';' at the end of the bracket
int findlowest(float Scores[],int HowMany)
{
float lowest=10000;
for(int a=0; a<HowMany; a++)
{
if(Scores[a]<lowest)
{
lowest=Scores[a];
}
}
cout<<"Your lowest score was "<<lowest<<endl;
//call function 'calaverage' passing the array
//'Scores'into it, 'howmany' and 'lowest'
calcaverage(Scores,HowMany,lowest);
//note it passes int three variables to 'calcaverage'
//Therefore the function declaration should
//have three variables as shown below

}

int calcaverage(float Scores[],int HowMany,float lowest)
{   float average;
float total=0;
for (int a=0; a<HowMany; a++)
{
if (Scores[a]!= lowest)//ignore the lowest score
{
total=total+Scores[a];

}
}

average=total/(HowMany-1);//-1 to ignore the lowest score
//therefore the array has shrunk by one.

cout<<"The average was "<<average<<endl;
}```

Popular pages Recent additions