2015-09-25 55 views
-2

所以我的程序的重点是从文本文件中读取数字并将其放入数组中。然后我必须使用插入排序对数组进行排序并找到中值。我也必须计算平均值,并显示平均值和中位数。我使用的语言是C++。这是我的设计:混淆关于传递变量到函数

Pseudocode for main: 

CAll the fileReadArray(intArray, MAX_ELEMS) function and assign return value to numElems 
If the numElems <=0, then print an error message and return 
Call the doCalculations(average, median, intArray, numElems) function 
Call the printOutput(average, median) function 
END main 


fileReadArray 
Return Value: numElems (int) 
Reference parameters: intArray (array of ints) 
Receives: maxElems (int) 
Preconditions: maxElems indicates the physical size of intArray (already declared) 
Logic: Open the input file (up to you if you want to read the filename). If the file doesn't open, return -1. If it opens, read one number at a time and store in the intArray until the index of the array reaches maxElems or the end of file. Close the file and return the index after the last one read. 

doCalculations 
Return Value: (none) 
Reference parameters: average (double), median (int) 
Receives: intArray (array of ints), numElems (int) 
Preconditions: intArray has been populated, numElems indicates how many elements are used in intArray 
Logic: Calculate the mean average. Calculate the median. 

calcAverage 
Return Value: average (double) 
Reference parameters: (none) 
Receives: intArray (array of ints), numElems (int) 
Preconditions: intArray has been populated, numElems indicates how many elements are used in intArray 
Logic: Calculate the mean average by finding the sum of the elements, then dividing by numElems 

calcMedian 
Return Value: median (int) 
Reference parameters: (none) 
Receives: intArray (array of ints), numElems (int) 
Preconditions: intArray has been populated, numElems indicates how many , elements are used in intArray 
Logic: Sort the array, return the middle element or the average (ruonded) of the 2 middle elements 


insertionSortArray 
Return Value: (none) 
Reference parameters: data (array of ints) 
Receives: data (array of ints), numElems (int) 
Preconditions: data has been populated, numElems indicates how many elements are used in data array 
Logic: Sort the array using the insertion sort. 



printOutput 
Return Value: (none) 
Reference parameters: (none) 
Receives: average (double), median (int) 
Preconditions: average and median have been calculated 
Logic: Print the average and median with labels 

我混淆了哪些变量传递给我的函数。我使用的语言是C++

+3

请指定语言。 –

+0

非常抱歉。我正在使用的语言是C++ –

+0

你可以让这个问题变小吗? – ergonaut

回答

0

我建议你使用std :: vector来保存从输入文件读取的值。那么它应该很容易。使用数组不是C++,应该避免。

这可以让您开始:

// Pass the filename as a const string reference (i.e. wont be changed) 
// Pass the vector as a reference (i.e. change needed) 
void fileReadArray(const string& filename, vector<double>& data) 
{ 
    // Read doubles from file and do 
    // data.push_back(double_value) for each value read from file 
} 

// Pass the vector as a const reference (i.e. wont changed) 
// Pass average and median as reference (i.e. change needed) 
void doCalculations(const vector<double>& data, int& average, int& median) 
{ 
    double sum = 0; 
    for (auto i : data) 
    { 
     sum += i; 
    } 
    average = sum/data.size(); 

    // calculate median 
} 

// Pass average and median as const reference (i.e. wont be changed) 
void printOutput(const double& average, const double& median) 
{ 
    cout << "Average: " << average << endl; 

    // Print median as well 
} 

int main() 
{ 
    vector<double> data; 
    string filename = "my_data_file"; 
    fileReadArray(filename, data); 

    // Did we get any data? 
    if (data.size() == 0) 
    { 
     cout << "No data in file" << endl; 
     return -1; 
    } 

    // Sort the vector 
    std::sort (data.begin(), data.end()); 

    double average; 
    double median; 
    doCalculations(data, average, median); 

    printOutput(average, median); 
} 

只需添加评论是否需要澄清。

BTW:

建议的排序不是插入排序。为什么不自己写的东西时,是在一个更好的形式已经可用它....

但是,如果你必须自己做,那么它可能是:

vector<double> myInsertionSort(const vector<double>& data) 
{ 
    vector<double> result; 

    // Build result vector from data vector 

    return result; 
} 

,并用它喜欢:

vector<double> sortedData = myInsertionSort(data); 

,最后....

如果你真的有使用数组,因为这是从老师仍微升作业ives in c-world ...

// Pass the filename as a const string reference (i.e. wont be changed) 
// Pass a pointer to the array (i.e. change needed) 
// Pass maximum by value 
int fileReadArray(const string& filename, double* data, int max_doubles) 
{ 
    int numberRead = 0; 
    while (numberRead < max_doubles) 
    { 
     // Read double from file 
     if (end_of_file) return numberRead ; // end_of_file is pseudo code 
     data[numberRead] = double_from_file; 
     numberRead++; 
    } 
    return numberRead; 
} 

// Pass the data as pointer 
// Pass size by value 
// Pass average and median as reference (i.e. change needed) 
void doCalculations(double* data, int size, int& average, int& median) 
{ 
    double sum = 0; 
    for (int i=0; i < size; i++) 
    { 
     sum += data[i]; 
    } 
    average = sum/size; 

    // calculate median 
} 

// Pass average and median as const reference (i.e. wont be changed) 
void printOutput(const double& average, const double& median) 
{ 
    cout << "Average: " << average << endl; 

    // Print median as well 
} 

int main() 
{ 
    double data[MAX_ELEMS]; 
    string filename = "my_data_file"; 
    fileReadArray(filename, data, MAX_ELEMS); 

    // Did we get any data? 
    if (data.size() == 0) 
    { 
     cout << "No data in file" << endl; 
     return -1; 
    } 

    // Sort the vector 
    myInsertionSort(data); 

    double average; 
    double median; 
    doCalculations(data, average, median); 

    printOutput(average, median); 
}