0

I tried to make a calculator for midterm and final grades of 5 students. 40% of Midterm and 60% of finals in an array a[5][3]. a[5][3] because 5 students, 3 lines because 1 for midterm another for finals and last one for overall grade(40% of Mid. + 60% of Finals). I get the "error lnk2019". What is wrong with that code? Thanks..

#include "stdafx.h"
#include <iostream>
using namespace std;

float a[5][3];
float data(float x);
float calc(float y);
float HL(float z);

int main()
{
    data(a[5][3]);
    calc(a[5][3]);
    HL(a[5][3]);

    system("pause");
    return 0;
}

float data(float x[5][3])
{
    for (int i = 0; i < 5; i++)//Getting the Grades
    {
        cout << "Enter midterm for St" << i + 1 << " : ";
        cin >> x[i][0];
        cout << "Enter final for St" << i + 1 << " : ";
        cin >> x[i][1];
    }
    return x[5][3];
} 

float calc(float y[5][3])
{
    for (int i = 0; i < 5; i++)//Calc. Overall Grades
    {
        y[i][2] = y[i][0] * 0,4 + y[i][1] * 0,6;
    }
    return y[5][3];
}

float HL(float z[5][3])
{
    float max = 0, min = 0;
    for (int i = 0; i < 5; i++)//Finding Highest and Lowest
    {
        if (z[i][2]>max)
        {
            max = z[i][2];
        }
        if (z[i][2] < min)
        {
            min = z[i][2];
        }
    }
    cout << "The Lowest Grade : " << min << "\nThe Highest Grade : " << max;
    return z[5][3];
}
Dux
  • 13
  • 2
  • 1
    In the future when posting questions about compiler or linker errors, please include the *complete* and *unedited* error log in the question. If you also post source, please point out where in the source the errors are about, – Some programmer dude Dec 15 '13 at 16:23
  • Nevertheless, I find the question interesting because the error is not from the compiler, but from the linker. A C compiler should have complained about wrong type when using a float as argument to a function that expects a matrix (well, a pointer to it actually). Something like "Error: cannot make pointer from integer...". Instead, the compiler assumes that the three functions are overloaded and leave the linker the task of finding the actual implementations of the three functions that better suit the arguments they have been called with. – mcleod_ideafix Dec 15 '13 at 16:39

3 Answers3

1

I suggest you go through a tutorial to brush up on your array basics. http://www.cplusplus.com/doc/tutorial/arrays/

You syntax for calling the functions is incorrect. Also, your functions prototypes do not match.

float data(float x);

and

float data(float x[5][3])
{

Also, when calling the function, don't specify the dimensions.

float someFloat = data( a );
Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
0

Also, fix these calls:

data(a[5][3]);
calc(a[5][3]);
HL(a[5][3]);

Because you are referencing an inexistent element in matrix a. Either you'll pass an undefined value, or you will get a segfault.

Remember that the last element of your amatrix is a[4][2]. If your intention was to pass all the matrix to your function, and not a single element, you have to redefine your prototypes and use only the matrix name a as the argument.

The "undefined symbol" the linker refers to are your functions data, calc and HL. The three of them are used in your main() function as functions that expects a single float value to work with. So are your prototypes.

But your implementation uses matrices as arguments, not floats. A C compiler should complain about wrong type argument used in functions data, calc and HL, but a C++ compiler will interpret it as data, calc and HL being overloaded functions, so they may have more than one implementation. You provide one of them (with a matrix as argument), but the compiler needs the other one (with a float as argument). The linker is responsible to find all the used implementations of a overloaded function. As it cannot find them, it throws that error.

mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32
0

You declared two functions

float data(float x);
float calc(float y);

... and used them a float argument, too (otherwise the compiler would have complained). However, you then defined them with an entirely different signature:

float data(float x[5][3]) { ... }
float calc(float y[5][3]) { ... }

The declarations have entirely different types. While the functions you declared take one float as argument, the functions you defined take a pointer to an array of floats as argument. More precisely, the functions you define take a pointer to an array of 3 floats as arguement as they are equivalent to this declaration:

float data(float (*x)[3]);
float calc(float (*y)[3]);
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380