1

So I have these 2 methods: the first is meant to take in 5 integer values, sum them up, then call the findLowest() method that returns the lowest found value, and subtracts that from the sum (then divides by 4 and prints results to screen).

I think the issue I'm having is the values it's returning are memory addresses. I've tried to understand how to dereference integers to get the value, but truth be told all of the online resources I've looked up have gone over my head.

Below is what I have so far:

void calcAverage(int* arr[5])
{
   int sum = 0;

   for (int a = 0; a < 5; a++)
      sum += *arr[a];

   sum -= findLowest(*arr);

   cout << "AVERAGE " << (sum / 4) << endl;
}


int findLowest(int* arr[5])
{
   int lowest = *arr[0];

   for (int a = 1; a < 5; a++)
   {
      if ((int)arr[a] < lowest)
         lowest = *arr[a];
   }


   return lowest;
}

On top of that, on the line that says

sum -=findLowest(*arr);

it pops up an error that reads

argument of type "int *" is incompatible with parameter of type "int **"

Thanks for the help in advance, and sorry if the post's a bit scattershot: I'm not really confident with this pointer/reference stuff to begin with.

  • what you want is `findLowest(arr)` – Borgleader Nov 12 '20 at 22:05
  • Does this answer your question? [Passing Arrays to Function in C++](https://stackoverflow.com/questions/14309136/passing-arrays-to-function-in-c) – Borgleader Nov 12 '20 at 22:06
  • Prefer to use `std::vector` instead of arrays, easier to pass around. Also, consider placing an array in a structure and passing the structure; easier than passing an array. – Thomas Matthews Nov 12 '20 at 22:06
  • @Borgleader it's similar, but whenever i attempt to pass a regular array through my methods, the compiler always gives an "argument of type int is incompatible with parameter of type int*". Also, i've made sure to remove all */& from my program so i don't think the error is coming from syntax mistakes on my vars. – Caleb Ernst Nov 12 '20 at 22:16
  • Also, I was under the impression that you couldn't pass straight-up arrays in C++, only pointers to those arrays... (but maybe i learned it wrong) – Caleb Ernst Nov 12 '20 at 22:18
  • @CalebErnst You can pass arrays. They decay to pointers. – Anonymous1847 Nov 12 '20 at 22:19

1 Answers1

1

You are almost there, but you should not dereference on this line:

sum -= findLowest(*arr);

Instead, just pass in the array, because that is what the function expects:

sum -= findLowest(arr);

Unrelated:

If you can, use a std::vector or std::array, the former for dynamically sized arrays, the latter for a compile time array. These both are safer, have more functions, and are easier to pass around.

Also use smart pointers for ownership of pointers instead of raw pointers. Smart pointers manage the ownership of an object so that you do not need to manually call new and delete.

Lily
  • 1,386
  • 2
  • 13
  • 16
  • thanks, the issue is i hadn't removed the [x] from all of my method calls, the solution you and borg gave works perfectly fine now. – Caleb Ernst Nov 12 '20 at 22:22