0

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;
}
2
  • "by the looks of things the program simply runs straight over it without looking at it" I don't understand why students keep jumping to this conclusion. Commented May 27, 2015 at 16:45
  • @LightnessRacesinOrbit I'm wondering how his program "ran fine" Commented May 27, 2015 at 16:46

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Additionally, you are not returning a value from your array function, which should return a float, and you are taking an argument of an int array type into your array function, when the array you supply it with in your function call is of a float type. (float array(int arr[], int size) -> void array (float arr[], int size))
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.