dimanche 29 octobre 2023

How to make this function return the highest value in the array

I was trying to make this function return the highest element in the array. I input elements in the array using pointer. After the code run, it only displays the first element in the array. I tried using this code int SIZE = sizeof(ptr) / sizeof(ptr[0]); in the main function, but i got a variable redefinition error. please help

 #include <iostream>


   using namespace std;

   double gethighest(const double ptr[], int SIZE)
     {
double highest = ptr[0];
for (int count = 0; count < SIZE; count++)
   {

    if (ptr[count] > highest)
        highest = ptr[count];

    return highest;
    }
      }
   int main()
          {
double* ptr = nullptr;
int SIZE;
double value;
cout << "Enter the size of the array" << endl;
cin >> SIZE;
ptr = new double[SIZE];

  for (int count = 0; count < SIZE; count++)
         {
      cout << "Enter the values of the array: ";
      cin >> ptr[count];
      while (ptr[count] < 0)
       {
          cout << "Invalid entry, enter a positive value: ";
          cin >> ptr[count];
       }
             }
 
  double highest = gethighest(ptr, SIZE);
  


    cout << "The highest value of the array is: " << highest;

          }
    

Aucun commentaire:

Enregistrer un commentaire