I wrote a function that calculates the highest value in the array but I am having trouble using pointers. Here is my code:
The errors are double highest = a; and return highest;
You would have to run through the loop for every element in the array to make sure that they are all compared against eachother and sorted correctly.
Are you sure you just don't want to pass the array into maximum? It might be easier for you to understand.
Your first error is because you are trying to assign a variable to a pointer, not to the value pointed to by the pointer.
Your second error is because you're trying to return a double data type when the application is expecting a constant double* data type, the difference matters in this case.
Just curious but why is "maximum(...)" a pointer function?
EDIT: Also the variable "highest" will cease to exist once that function exits so you should make sure that you are returning a copy of its value and not just a pointer to it. It may work this way but doing this is not a good habit to get into.
It is a pointer function because that is what we are learning right now so I had to make it a pointer... Are you saying I should loop through the array or pass the array into my maximum function?
Sorry, I didn't realize this was a school assignment.
Either way, but looping through the array by reference would be the better way to do it. You just need to make sure you are comparing every element against every other one. Generally people use embedded for loops for this; I didn't actually "unroll" your code yet though.
Isn't a nested loop the same thing as what my while loop is doing? It's saying if the value its pointing at is the highest assign it that value then it increments p and count so it checks each value against every other one.
Edit:
Notice that cout << highest;
displays 8.9, the highest number in the array. Then you output the value that the ptr you returned points to, but you never returned a pointer, so you get the garbage 6.955 E -315...
Your function isn't returning anything. It should be returning a pointer to the highest value, which you aren't saving in the while loop. Try using constdouble* highest = a; then test for
1 2
if (*p > *highest)// comparing the values pointed to
highest = p;// this would capture a pointer to the highest value
Then return highest, which is now a pointer. Also, why is a_size a double?
I tried this on your code and it works.
#include <iostream>
usingnamespace std;
constdouble* maximum(constdouble* a, int a_size)
{
if (a_size == 0)
return NULL; //use NULL for pointers, even though NULL == 0
constdouble *highest = a; //here you want a double*, the location of the highest value
constdouble* p = a + 1;
int count = a_size - 1;
while (count > 0)
{
if (*p > *highest) //the value at p > value at highest
highest = p; //copy pointer (location)
p++;
count--;
}
return highest; //return the location of the highest value
}
int main()
{
double a[] = {
0.1,-1.2,2.3,-3.4,4.5,-5.6,6.7,-7.8,8.9,-9.0
};
cout << *maximum(a, 10) << "\n";
}