Hi,
I am supposed to create a project with two different cpp files, the first cpp file arrayallocator.cpp is supposed to dynamically allocate an array of integers and return a pointer to the array. The program should ask the user how many test scores they want to enter. So I think I got that part right, but here's the next part. In the second cpp file, I am supposed to take that information that the user entered and use that to get the test scores, have the test scores sorted in ascending order, display the sorted array, and then average and display the average. I think I have it mostly figured out but the thing I have a problem with, is the values that arrayallocator returns. Can a function in one cpp file return values to a function or functions in another cpp file? Here is the code for arrayallocator.cpp
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int arrayAllocate();
int main ()
{
arrayAllocate();
return (*array,*numElem);
}
int arrayAllocate()
{
int *numElem;
int *array;
array = new int;
numElem = new int;
cout << "How many test scores do you wish to enter?:\n";
cin >> *numElem;
array = new int[*numElem];
if (array ==NULL)
{
cout << "Error allocating memory\n";
}
return (*array,*numElem);
}
testaveraging.cpp
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int getScores(int *[], int);
void showArrayPtr(int *[], int);
void arrSelectSort(int *[], int);
int main()
{
getScores(array, numElem);
return 0;
}
int getScores(int *array[], int size)
{
double total = 0, average;
cout << "Enter the test scores below\n";
for (int count = 0; count < numElem; count++)
{
cout << "Test "<< (count + 1) << ": ";
cin >> array[count];
}
arrSelectSort(array, numElem);
showArrayPtr(array, numElem);
for (int count = 0; count < numElem; count++)
{
total += static_cast<double>(array[count]);
}
average = total / numElem;
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: "<< average << endl;
return 0;
}
void arrSelectSort(int *array[], int size)
{
int startScan, minIndex;
int *minElem;
for (startScan = 0; startScan < (size -1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (*(array[index]) < *minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
void showArrayPtr(int *array[], int size)
{
cout << "The test scores sorted in ascending order are:\n";
for (int count = 0; count < size; count++)
{
cout << *(array[count]) << " ";
}
cout << endl;
}
The difficulty I am facing is that most of the examples in my book, can have variable declarrations in main, but I can only call functions in main, and I cannot use global variables.
Could someone please help me cuz I am going cross eyed from trying to figure out where i went wrong?