I've been working on a program as a practice for my CSC 101 class. We're done with work, but I felt that I should practice some more on my own. I wrote a program that reads 10 decimals into an array, the calculates the sum and average, displays it on screen in reverse order and then displays the largest and smallest number in the array. I tried to do the largest and smallest with functions, but every time I try to compile it get the following errors
In function `main':
[Linker error] undefined reference to `Largest(float, int)'
[Linker error] undefined reference to `Smallest(float, int)'
I'm not sure what could be causing this, because I'm relatively new to C++. Here is the code for my program.
CODE
#include <iostream>
#include <iomanip>
using namespace std;
float Largest(float, int);
float Smallest(float, int);
const int ArraySize = 10; ///sets ArraySize
float nums[ArraySize];
int main()
{
float sum = 0.0;
float avg = 0.0;
cout << "Please enter " << ArraySize << " decimal numbers: ";
for (int i = 0; i < ArraySize; i++)
cin >> nums[i];
//Calculate sum
for (int j = 0; j < ArraySize; j++)
sum += nums[j];
//Calculate average
avg = sum / float(ArraySize);
//Display results
cout << "\n\nThe array in reverse order:\n";
for (int i = ArraySize - 1; i >= 0; i--) //Reads array in reverse
cout << setw(8) << nums[i];
cout << endl << endl;
//Sum & Average
cout << "Sum: " << sum << endl;
cout << "Average: " << avg << endl << endl;
//Largest and Smallest numbers
cout << "Largest Number: " << Largest(nums[ArraySize], ArraySize) << endl;
cout << "Smallest Number: " << Smallest(nums[ArraySize], ArraySize) << endl;
cout << endl;
system("pause");
return 0;
}
float Largest(float nums[ArraySize], int ArraySize)
{
int index = 0;
//compare numbers
for (int i = 1; i < ArraySize; i++)
if (nums[index] < nums[i])
index = i;
return nums[index];
}
float Smallest(float nums[ArraySize], int ArraySize)
{
int index = 0;
//compare numbers
for (int j = 1; j < ArraySize; j++)
if (nums[index] > nums[j])
index = j;
return nums[index];
}