繁体   English   中英

在 C++ 中调用 void 函数对数组进行排序

[英]Calling a void function to sort an array in C++

我正在尝试调用一个 void 函数,该函数将随机数组从最小到最高排序,然后重用该 void 函数对从笔记文件中提取的数组进行排序。

当我尝试从print()函数打印时,我无法让 sort 函数工作。

我们今天刚刚学会了回忆,我试图找到一种方法来实现它,但无法理解它。

这是代码(我为所有评论道歉,这是一个班级,我很挣扎):

#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;
}

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;
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM