我正在嘗試呼叫一個 void 函式,該函式將隨機陣列從最小到最高排序,然后重用該 void 函式對從筆記檔案中提取的陣列進行排序。
當我嘗試從該print()功能進行列印時,我無法使排序功能正常作業。
我們今天剛剛學會了回憶,我試圖找到一種方法來實作它,但無法理解它。
這是代碼(我為所有評論道歉,這是一個班級,我很掙扎):
#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;
}
uj5u.com熱心網友回復:
在sort()這一行中,ssf = ptr;您只是將一個區域變數分配給另一個。陣列不受影響。您需要交換陣列中的專案。
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;
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/339865.html
