2015-09-25 19 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); 
}