I am trying to call a void function that sorts a random array from smallest to highest, then reuse that void function to sort an array that is pulled from a notes file.
I am having trouble with getting the sort function to work when I try to print from the print()
function.
We just learned about recalling today, and I was trying to find a way to make that work, but couldn't wrap my head around it.
Here is the code (I apologize for all the comments, it is for a class and I'm struggling):
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
#define LOWER_BOUND 1.0
#define UPPER_BOUND 250.0
#define MIN_VALUE 25
#define MAX_VALUE 75
#define PRINT_MAX_VALUE 12
//prototypes
double randDouble();
int buildRandom(double array[]);
int buildFile(double fileArray[]);
void print(string title, double array[], int numValues);
void sort(double array[], int numValues);
int main()
{
//declare variables
int numValues, output, fileValues;
double array[MAX_VALUE];
double fileArray[MAX_VALUE];
//seed the RNG
srand(52);
//filling numValues
numValues = buildRandom(array);
//showing numValues
cout << "You have " << numValues << " values in your array." << endl << endl;
//initial print
print("Array of Random values: ", array, numValues);
cout << endl << endl;
//sorting array
sort(array, numValues);
//sorted print
print("Array of sorted values: ", array, numValues);
//store into new array
fileValues = buildFile(fileArray);
//display fileValues
cout << endl << endl << "You have " << fileValues << " values in your second array." << endl << endl;
//print file array
print("File values: ", fileArray, fileValues);
cout << endl << endl;
//sort the file array
sort(fileArray, fileValues);
//print the sorted file array
print("Sorted file values: ", fileArray, fileValues);
return 0;
}
/*
Function: randDouble
Use: Generates a random double value between 1.0 and 250.0
Arguments: This takes no arguments
Returns: a double value that is a random number
*/
double randDouble()
{
//initialize values
double value;
int randNum = rand();
//creates value
value = LOWER_BOUND (randNum / (RAND_MAX / (UPPER_BOUND - LOWER_BOUND)));
return value;
}
/*
Function: buildRandom
Use: Fills an array of doubles with random numbers and values
Arguments: doubley array[] - an array that gets filled with numbers
Returns: The number of values placed in the array
*/
int buildRandom(double array[])
{
//initialize values
int randNum = rand();
int value, counter;
//set counter
counter = 0;
//creates number
value = MIN_VALUE (randNum % (MAX_VALUE - MIN_VALUE 1));
//put value into array based on counter number
while(counter < value)
{
array[counter] = randDouble();
counter ;
}
return value;
}
/*
Function: print
Use: displays numeric information inside of an array then skips to
the next line after 12 outputs
Arguments: string title - prints string
double array [] - gets numbers to print
int numValues - gets amount of numbers to print
Returns: Nothing
*/
void print(string title, double array[], int numValues)
{
//initialize value
int counter = 1;
int extraCounter = 0;
//print prompt
cout << title << endl << endl;
//print value of array based on where it is
while(numValues != extraCounter)
{
cout << fixed << setprecision(2) << setw(8) << right << array[extraCounter];
//skips line at 12, resets counter
if(counter == PRINT_MAX_VALUE)
{
cout << endl;
counter = 0;
}
counter ;
extraCounter ;
}
}
/*
Function: sort
Use: Sorts numbers based on an algorithm
Arguments: double array[] - pulls numbers to sort
int numValues - pulls amount of numbers to sort
Returns: Nothing
*/
void sort(double array[], int numValues)
{
//initialize values
int top, ssf, ptr;
int last = numValues;
//finds smallest value and replaces it
for(top = 0; top < last; top )
{
for(ptr = top, ssf = top; ptr < last; ptr )
{
if(array[ptr] < array[ssf])
{
ssf = ptr;
}
}
}
}
/*
Function: buildFile
Use: Opens the file and fills the array
Arguments: double fileArray[] - The array that gets filled
Returns: subscript - amount of values inside the array
*/
int buildFile(double fileArray[])
{
//initialize values
int subscript;
double value;
ifstream input;
//sets subscript to 0
subscript = 0;
//opens file
input.open("nums.txt");
if(input.fail())
{
cout << "nums.txt did not open."<< endl;
exit(-1);
}
//priming read
input >> value;
//fills array
while(input)
{
fileArray[subscript] = value;
subscript ;
input >> value;
}
//closes file
input.close();
return subscript;
}
CodePudding user response:
In sort()
at the line ssf = ptr;
you are just assigning one local variable to another. The array is unaffected. You need to swap the items in the array.
void sort(double array[], int numValues)
{
//initialize values
int top, ssf, ptr;
int last = numValues;
//finds smallest value and replaces it
for(top = 0; top < last; top )
{
for(ptr = top, ssf = top; ptr < last; ptr )
{
if(array[ptr] < array[ssf])
{
double temp = array[ptr];
array[ptr] = array[ssf];
array[ssf] = temp;
}
}
}
}