2

My program needs to use local variables instead of global. However, when I try to do this, I can't seem to find the right parameters to pass the data back and forth from main and functions. I keep getting an error saying "argument type of int is incompatible with parameter of type float". Please help me understand what to do here. Thanks for your time, I appreciate it.

I've tried googling the error code but only find answers/questions regarding issues with pointers which I haven't learned yet. I've worked on it for hours just to make the variables work within "int main" but to no avail.

//This program asks user how many grades there are, 
//inputs grades, and displays median of said grades.

//"int main" is at the bottom of the program, preceded by
//variables, function headers, and a single array.
#include <iostream>
using namespace std;

void grdTaker(float [], int);
void sortArray(float[], int);
void median(float[], int);

//Main
int main()
{
    //Variables
//int grdsCounted; //Number of grades from user.
    const int arraySize = 20;
    int grdsCounted; //Number of grades from user.
    float grades[arraySize]; //Max grades that can be entered.

    grdTaker(grdsCounted, grades[]);
    sortArray(grades, grdsCounted);
    median(grades, grdsCounted);

    system("pause");
}

void grdTaker(float array[], int size) //Function gathers grades.
{
    //const int arraySize = 20;
    //int grdsCounted; //Number of grades from user.
    //float grades[arraySize]; //Max grades that can be entered.

    cout << "You may input up to 20 grades. \n";
    cout << "First enter the number of grades you have: ";
    cin >> grdsCounted;

    while (grdsCounted > arraySize)
    {
        cout << "That is more than 20 grades, try again: \n";
        cin >> grdsCounted;
    }

    cout << "Enter each grade: \n";

    //requests how many grades there are and stores them in array
    for (int grdCount = 0; grdCount < grdsCounted; grdCount++)
    {
        cin >> grades[grdCount];
    }
};

void sortArray(float array[], int size) //Function sorts array values.
{
    bool swap;
    float temp;

    do
    {
        swap = false;
        for (int count = 0; count < (size - 1); count++)
        {
            if (array[count] > array[count + 1])
            {
                temp = array[count];
                array[count] = array[count + 1];
                array[count + 1] = temp;
                swap = true;
            }
        }
    } while (swap);
}

void median(float array[], int size) //Outputs the median of entered grades.
{
    int med = size / 2;
    int odd = med - 1;

    cout << "The median grade is: ";

    if (size % 2 == 1)
    {
        cout << array[med] << endl;
    }
    else
    {
        cout << (array[med] + array[odd]) / 2 << endl;
    }

}
  • 1
    You transposed your parameters to grdTaker method. – Dakshinamurthy Karra Jan 28 '19 at 05:16
  • 1
    Look carefully at which variables you are passing as what parameters for each of the functions. – Galik Jan 28 '19 at 05:41
  • 1
    Regarding `grdTaker(grdsCounted, grades[]);`, you only need the `[]` if you want a specific array element, and in that case you need to specify the index of the element. `grades[4]` for example. If you want to pass the whole array ([decayed to a pointer, mind you](https://stackoverflow.com/questions/1461432/what-is-array-decaying), so watch that you don't lose track of the array's length) leave the `[]`s out. – user4581301 Jan 28 '19 at 05:47
  • 1
    Regarding `cin >> grdsCounted;`, [read up on scope](https://en.wikipedia.org/wiki/Scope_(computer_science)). It does a lot more than give you clean breath. If a variable is declared in another scope, the rest of the program can't use it. In this case `grdsCounted` was passed in as `size` (well it should have been. See above comments) But because `size` is [passed by value](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value), there is no way to get whatever value you set inside the function back out of the function. – user4581301 Jan 28 '19 at 05:50

2 Answers2

2

Your problem of assigning to floats is probably due to the creation of the array in that way in C++. Try declaring arrays as de c++ way, with new.

Have you thought of returning the values! Take a look at this! For example, divide the first function into 2! Programming is always about dividing problems into little ones.

int numberGradesFromUser() {

    int grdsCounted;
    int arraySize = 20;

    cout << "You may input up to 20 grades. \n";
    cout << "First enter the number of grades you have: ";
    cin >> grdsCounted;

    while (grdsCounted > arraySize)
    {
        cout << "That is more than 20 grades, try again: \n";
        cin >> grdsCounted;
    }
    return grdsCounted;
}

float* grdTaker(int grdsCounted) //Function gathers grades.
{

    float * grades = new float[grdsCounted];

    cout << "Enter each grade: \n";

    //requests how many grades there are and stores them in array
    for (int grdCount = 0; grdCount < grdsCounted; grdCount++)
    {
        cin >> grades[grdCount];
    }
    return grades;

};

int main()
{
    //Variables

    int grdsCounted; //Number of grades from user.      

    grdsCounted = numberGradesFromUser();
    float *gradess = new float[grdsCounted];

    sortArray(gradess, grdsCounted);
    median(gradess, grdsCounted);

    system("pause");
}

Having this, I guess the rest of the functions should work. Adjust them in your way!

Also, it is good practice to declare the functions in a header, or at least on top of the main, not below!

M.K
  • 1,464
  • 2
  • 24
  • 46
1

Try this out:

//This program asks user how many grades there are, 
//inputs grades, and displays median of said grades.

//"int main" is at the bottom of the program, preceded by
//variables, function headers, and a single array.
#include <iostream>
using namespace std;

void grdTaker(float [], int, const int);
void sortArray(float[], int);
void median(float[], int);

//Main
int main()
{
    //Variables
//int grdsCounted; //Number of grades from user.
    const int arraySize = 20;
    int grdsCounted; //Number of grades from user.
    float grades[arraySize]; //Max grades that can be entered.

    grdTaker(grades,arraySize);
    sortArray(grades, grdsCounted);
    median(grades, grdsCounted);

    system("pause");
}

void grdTaker(float array[], const int arraySize) //Function gathers grades.
{
    //const int arraySize = 20;
    //int grdsCounted; //Number of grades from user.
    //float grades[arraySize]; //Max grades that can be entered.
    int grdsCounted;
    cout << "You may input up to 20 grades. \n";
    cout << "First enter the number of grades you have: ";
    cin >> grdsCounted;

    while (grdsCounted > arraySize)
    {
        cout << "That is more than 20 grades, try again: \n";
        cin >> grdsCounted;
    }

    cout << "Enter each grade: \n";

    //requests how many grades there are and stores them in array
    for (int grdCount = 0; grdCount < grdsCounted; grdCount++)
    {
        cin >> array[grdCount];
    }
};

void sortArray(float array[], int size) //Function sorts array values.
{
    bool swap;
    float temp;

    do
    {
        swap = false;
        for (int count = 0; count < (size - 1); count++)
        {
            if (array[count] > array[count + 1])
            {
                temp = array[count];
                array[count] = array[count + 1];
                array[count + 1] = temp;
                swap = true;
            }
        }
    } while (swap);
}

void median(float array[], int size) //Outputs the median of entered grades.
{
    int med = size / 2;
    int odd = med - 1;

    cout << "The median grade is: ";

    if (size % 2 == 1)
    {
        cout << array[med] << endl;
    }
    else
    {
        cout << (array[med] + array[odd]) / 2 << endl;
    }

}

Description: I added the arraySize to the grdTaker function and declared grdsCounted in there.

Mostafa Ayaz
  • 480
  • 1
  • 7
  • 16