简体   繁体   中英

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;
}

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM