I am on a project where we have to read in from a file, temporarily store them in dynamically allocated memory, do sorting and stuff, and deallocate the memory.
As per the project is testing our knowledge over dynamic memory and memory leak, one of the instructions is do not use VLA.
I am not sure what our instructor means by we should not use VLA, are we not allowed use [] bracket syntax? or we can use them as long as we use memories from heap and deallocate them properly once they are of no use anymore.
Here is my main.cpp, it is not complete yet, so please excuse some typos and possible errors, but if you have something to suggest or correct, those are more than welcome as well.
Thank you and do have a good weekend y'all.
#include "proj2-arrayFunctions.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
ifstream file;
int size = 0;
int* numberArray;
int counter = 0;
file.open("arrays.txt");
if (!file)
{
cout << "error: file is not opened! " << endl;
return 1;
}
while(file.good())
{
file >> size;
numberArray = new int[size];
for (int i = 0; i < size; i++)
{
file >> numberArray[i];
}
bubbleSort(numberArray, size);
cout << "the largest value from this array is: " << largestValue(numberArray, size) << endl;
cout << "the smallest value from this array is: " << smallestValue(numberArray, size) << endl;
cout << "the average value of this array is: " << averageValue(numberArray, size) << endl;
cout << "the median value of this array is: " << medianValue(numberArray, size) << endl;
delete[] numberArray;
}
return 0;
}