Alex Farrell
Alex Farrell

Reputation: 3

Using a function and an array to get the maximum and minimum value

I am having some trouble getting my program to find the maximum and minimum values for a set of students GPA's. I can do this easily without using a function but when I include the function my program still runs fine but it does not calculate the maximum and minimum GPA's. I have debugged it as well and by the looks of things the program simply runs straight over it without looking at it. Your help would be greatly appreciated. This is what my code currently looks like:

#include <iostream>
using namespace std;

float array(int arr[], int size)
{
    float max = 0;
    float min = 4;
    int i;

    for (i = 0; i <= 2; i++)
    {
        if (arr[i] > max)
            max = arr[i];

        if (arr[i] < min)
            min = arr[i];
    }
    cout << "Maximum GPA: " << max;
    cout << "Minimum GPA: " << min;

}
int main(int argc, char** argv)
{
    int ID[3];
    float GPA[3];
    int i;

    for (i = 0; i <= 2; i++)
    {
        cout << "Please enter student ID and GPA: " << endl;
        cin >> ID[i] >> GPA[i];
        array[GPA, i];
    }
    for (i = 0; i <= 2; i++)
    {
        cout << endl << "Student ID: " << ID[i] << endl << "Student GPA: " << GPA[i] << endl;
    }
    return 0;
}

Upvotes: 0

Views: 1708

Answers (2)

frendall
frendall

Reputation: 63

Hi I am quite new to programming but I think your function call array[GPA,i] should be array(GPA,i); and move it further down to just above return. its my first attempt at answering a question hope it helps.

Upvotes: 0

dwcanillas
dwcanillas

Reputation: 3651

You are calling your function wrong:

array[GPA, i];

should be

array(GPA, i);

Your code shouldn't have even compiled, since I don't see an array variable.

There are also a few other things wrong with your code. array is probably a bad name for your function - meaningful function names are good practice, and std::array's existence can cause problems. size in your array function is unused, and as @jaggedSpire pointed out. Your array function should probably be

void array(float arr[], int size)

assumming you start using size.

Upvotes: 2

Related Questions