0

this is the function that's coming up with the most errors

double getHighestRainfall(double arrayOfNumbers, double SIZE)
{
    double highest;
    highest = arrayOfNumbers[0];

    for(int count = 0; count > highest ; count++)
    {
        if(arrayOfNumbers[count] > highest)
        {
            highest = arrayOfNumbers[count];
        }
    }
    return highest;
}

#include <iostream>

using namespace std;

//function prototypes

double getLowestValue(double arrayOfNumbers, double SIZE);

double takeAverage(double arrayOfNumbers, double average);

double getLowestValue(double arrayOfNumbers, double SIZE);

double getHighestRainfall(double arrayOfNumbers, double SIZE);

double getTotalRainfall(double arrayOfNumbers[], double SIZE);



//global variables and constants

double average;

const double SIZE = 12;

double arrayOfNumbers[12];

double TOTAL = 0;

string myMonths[];


//main function

int main ()

{
 //declare the string for months

string myMonths[12] = { "January", "February", "March", 

                        "April", "May", "June", "July", 

                        "August", "September", "October",

                        "November", "December"};

double arrayOfNumbers[12];

//get values to store in array

for(int count = 0; count < SIZE; count++)

{
    cout << "Enter values to store in " << myMonths[count] << ": ";

    cin >> arrayOfNumbers[count];

}




//print the total rainfall

 cout << "The total rainfall is: " << getTotalRainfall(arrayOfNumbers, 

SIZE);


 //print the average rainfall

 cout << "The average rainfall is: " << takeAverage(average, SIZE);




//print the least rainfall 

 cout << "The least rainfall is: " << getLowestValue(arrayOfNumbers, SIZE);


    return 0;

}

the errors:

test2.cpp:66:39: error: no matching function for call to 'getLowestValue'
 cout << "The least rainfall is: " << getLowestValue(arrayOfNumbers, SIZE);
BenedictionsMBP:MyCppFiles benedictionbora$ g++ test2.cpp
test2.cpp:16:8: error: definition of variable with array type needs an explicit size or an initializer
string myMonths[];
       ^
test2.cpp:47:39: error: no matching function for call to 'getLowestValue'
 cout << "The least rainfall is: " << getLowestValue(arrayOfNumbers, SIZE);
                                      ^~~~~~~~~~~~~~
test2.cpp:6:8: note: candidate function not viable: no known conversion from 'double [12]' to 'double' for 1st argument
double getLowestValue(double arrayOfNumbers, double SIZE);
       ^
test2.cpp:102:29: error: subscripted value is not an array, pointer, or vector
    highest = arrayOfNumbers[0];
              ~~~~~~~~~~~~~~^~
test2.cpp:105:22: error: subscripted value is not an array, pointer, or vector
    if(arrayOfNumbers[count] > highest)
       ~~~~~~~~~~~~~~^~~~~~
test2.cpp:107:29: error: subscripted value is not an array, pointer, or vector
    highest = arrayOfNumbers[count];
              ~~~~~~~~~~~~~~^~~~~~
mch
  • 9,424
  • 2
  • 28
  • 42

3 Answers3

1

A double is a single value and you cannot index a double. If you want an array (since the question is tagged C++) use std::vector or std::array:

double getHighestRainfall(const std::vector<double> &arrayOfNumbers)

arrayOfNumbers.size() then holds the number of elements in the vector, so you don't need to pass this number along as parameter. Then the loop will be for(int count = 0; count < numberOfArrays.size(); count++)

If you are allowed to use std::vector then use it and skip the rest of answer. If you are for some reason not allowed to use std::vector you need to use a pointer:

double getHighestRainfall(const double *arrayOfNumbers, size_t size)

Probably SIZE was meant to be number of elements in the array? Then you use size_t as type, because double is a floating point number, which makes no sense here. Then the loop needs to be for(int count = 0; count < size; count++)

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
0

The issue is that arrayOfNumbers is not a pointer but a double, indexing it is a non-sense.

Moreover, your for loop condition is wrong. You need to iterate over all elements of the array.


What you probably wanted to write:

double getHighestRainfall(double * arrayOfNumbers, std::size_t SIZE)
{
    if(SIZE > 0)
    {
        double highest = arrayOfNumbers[0];

        for(std::size_t count = 0; count < SIZE ; ++count)
        {
            if(arrayOfNumbers[count] > highest)
                highest = arrayOfNumbers[count];
        }
        return highest;
    }
    else
    {
        return 0; // Default value if the array is empty. You can also raise an exception instead.
    }
}

I have added a checking over the array size in order to avoid to explicitly access unreserved memory location.


Note: You probably have to use std::array instead of raw arrays. If you need a dynamic array, then std::vector would suit your needs.

Fareanor
  • 5,900
  • 2
  • 11
  • 37
0

You are getting that error because you have to pass a pointer to your array instead. Here is the correct one


double getHighestRainfall(double * arrayOfNumbers, int SIZE)
{
    if(!SIZE) return 0; // default 0 if SIZE is zero

    double highest = arrayOfNumbers[0];

    for(int count = 0; count < SIZE; ++count){
        if(arrayOfNumbers[count] > highest)
        highest = arrayOfNumbers[count];
    }

    return highest;
}

int main(){

  double testcase[3] = {100.1 , 102.3 , 99.8};
  cout << "Highest Rain Fall: " << getHighestRainfall(testcase,3);
  return 0;

}

As you said you did not cover pointers yet you can define your function like this. But it's the same as passing the pointer under the hood and does not make a big difference.

double getHighestRainfall(double arrayOfNumbers[], int SIZE)

Some hints:

  • SIZE should be int because array length is an integer ( actually it should be unsigned int)

  • use ++count in for-loop for performance reasons. (it's compiler-specific if it has performance benefits or not. Use ++counter to be in the safe side and get used to it)

  • check if SIZE has a value other than 0 and return a default value ( here I chose 0)

By the way, if this is an exercise (which looks to be true) that is ok to use pointers and arrays in their raw form. But it is better to use std::vector for such implementation.

Soheil Armin
  • 2,725
  • 1
  • 6
  • 18