Reputation: 15
I keep getting "undefined reference to 'x'" where x is the function prototypes. I have the functions mapped out but the main still needs work just fyi. I just want to fix the ld return error 1 before pressing on but I can't seem to pin point the issue.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//Symbolic Constants
const int MAX=11;
//Function Prototypes
int buildQuizArray(int);
void printArray(string,int,int);
double calcQuizAverage(int,int);
void sortArray(int,int);
int main ()
{
int quizScores[MAX];
int compQuiz;
int tempArray[MAX];
int average;
compQuiz = buildQuizArray(quizScores[MAX]);
quizScores[MAX]=tempArray[MAX];
average = calcQuizAverage(quizScores[MAX], compQuiz);
cout<<endl<<"Your quiz average is "<<average<<endl;
printArray ("Quiz Scores", tempArray[MAX], compQuiz);
sortArray(tempArray[MAX], compQuiz);
}
int buildQuizArray(int quizArray[])
{
int numQuiz, input, a;
a=0;
numQuiz=1;
cout << "Enter your score for quiz "<<numQuiz<<" (-1 to quit): ";
cin >> input;
while (input != -1)
{
quizArray[a] = input;
a++;
numQuiz++;
cout<< "Enter your score for quiz "<<numQuiz<<" (-1 to quit): ";
cin >> input;
}
return a+1;
}
void printArray(string reportTitle, int quizArray[], int numberOfQuizzes)
{
int a;
cout<< reportTitle <<endl<<"-----------"<<endl;
for (a=0; a<numberOfQuizzes; a++)
{
cout<< "Quiz " << a <<": " << setw(2) <<quizArray[a] <<"/10"<<endl;
}
}
double calcQuizAverage(int quizArray[], int numberOfQuizzes)
{
int sum, lowSum, avg, a;
a = 0;
sum = 0;
lowSum = quizArray[0] + quizArray[1];
for (a=0; a< numberOfQuizzes; a++)
{
sum += quizArray[a];
}
if (numberOfQuizzes <= 2)
{
avg = sum / (10 * numberOfQuizzes) * 100;
}
else
{
(sum - lowSum) / (10 * (numberOfQuizzes - 2)) * 100;
}
return avg;
}
void sortArray(int quizArray[], int numberOfQuizzes)
{
int min, a, b, temp;
for (a=0; a<numberOfQuizzes; a++)
{
min = a;
}
for(b=a+1; b<numberOfQuizzes; a++)
{
if (quizArray[a] < quizArray[min])
{
min = b;
}
}
temp = quizArray[a];
quizArray[a]=quizArray[min];
quizArray[min]=temp;
}
Upvotes: 1
Views: 284
Reputation: 40315
You declare:
int buildQuizArray(int);
But you define:
int buildQuizArray(int quizArray[]) ...
int[]
is not the same as int
.
Also: You are passing an int
to the functions when you call them, though; note that e.g. quizScores[MAX]
is the MAXth element of quizScores
and is an int
, which is actually beyond the end of that array, and really isn't what you want to be doing.
If I had to guess how you got here I'd guess that you had just about everything right, but you had unknowingly declared your prototypes incorrectly (int
instead of int[]
), so you then tacked [MAX]
on to the arrays you were passing to functions just to get it to compile, then ran into the inevitable linker problem that led you here. If that's what you did, it wasn't quite the right approach.
What you really mean to do is:
For your functions that take arrays, declare them properly:
int buildQuizArray (int[]);
Pass the array pointer itself to the function when calling it:
buildQuizArray(quizScores);
Leave your declarations as-is, they look fine (syntax-wise).
Upvotes: 1