我對C++編程還很陌生,我需要編碼幫助來將文本文件中的數字按升序排序,這樣我可以將它的中位數,但我不知道該怎麼做。如何從文本文件中按升序對數據進行排序?
這裏是我到目前爲止的代碼:提前
//Create a Vector to hold a set of exam scores.Write a program to do the following tasks: 1. Read exam scores into a vector from Scores.txt
//2. Display scores in rows of five(5) scores.
//3. Calculate average score and display.
//4. Find the median score and display.
//5. Compute the Standard Deviation and display
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int main()
{ const int array_size = 36; // array size
int numbers[array_size]; //array with 36 elements
int count = 0;
int column_count = 5;
ifstream inputfile; //input file into stream object
//open file
inputfile.open("Scores.txt");
//read file
while (count < array_size && inputfile >> numbers[count])
count++;
//close file
inputfile.close();
//display numbers read
for (count = 0; count < array_size; count++) {
cout << numbers[count] << " ";
if (count % column_count == column_count - 1) {
cout << "\n";
}
}
//find the average
double average; //average
double total = 0; //initialize accumulator
cout << "\nAverage:\n";
for (count = 0; count < array_size; count++)
total += numbers[count];
average = total/array_size;
cout << average << " ";
cout << endl;
//find the median
std::sort(numbers.begin(), numbers.end(), std::greater<int>());
system ("pause");
return 0;
}
謝謝!
您是否必須自己編寫排序算法? – evanmcdonnal
請發表您試過的內容嗎? – user93353