我在排序我的數組時遇到問題,並且找不到輸入文件中數據的計算代碼。任何幫助將不勝感激。我搜索了互聯網,並且仍然有很多麻煩。初學者需要幫助排序數組並計算
該數組正在讀取一個包含1,000個數字的data.txt文件。該陣列應該只讀取這些數字中的100個,並對該試驗結果進行計算。
任何幫助將不勝感激!
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
// Declare global constants
const char* IN_FILE_NAME = "stats.txt";
const char* OUT_FILE_NAME = "results.txt";
const int ELEMENTS = 100;
// Function Prototypes
double findMean(double* elementArray, int ELEMENTS); // sum of numbers/how many numbers
double findMedian(double* elementArray, int ELEMENTS); // middle number
double findMinimum(double* elementArray, int ELEMENTS); //smallest number in the array
double findMaximum(double* elementArray, int ELEMENTS); //largest number in the array
double findStdDev(double* elementArray, int ELEMENTS); // sqrt(Sum * | x - mean |^2)/number of arrays
bool getTrialElements(std::ifstream& inFile, double* elementArray, int ELEMENTS);
double printTrialResults(std::ofstream& outFile, int trialNumber, double elementArray[], int ELEMENTS); //print to output file
void sortArray();
// This program performs basic statistics on a large set of data points
int main()
{
// Declare variables
ifstream inFile;
ofstream outFile;
int trialNumber = 0;
double elementArray[ELEMENTS];
// Open input and output files
inFile.open(IN_FILE_NAME);
outFile.open(OUT_FILE_NAME);
// Loop through all of the data sets in the input file
while(getTrialElements(inFile, elementArray, ELEMENTS))
{
// Keep track of the number of data sets processed
trialNumber++;
// Output the results to the output file
printTrialResults(outFile, trialNumber, elementArray, ELEMENTS);
}
// Close input and output files
outFile.close();
inFile.close();
return 0;
}
// Function definitions
double findMean(double elementArray[], int ELEMENTS)
//Find the mean of the array
{
double sum = 0;
for(int ELEMENT = 0; ELEMENT < 100, ELEMENT++)
{
sum = sum + elementArray[ELEMENT];
}
return sum/ELEMENTS;
}
double findMedian(double elementArray[], int ELEMENTS)
{
}
double findMinimum(double elementArray[], int ELEMENTS)
{
}
double findMaximum(double elementArray[], int ELEMENTS)
{
}
double findStdDev(double elementArray[], int ELEMENTS)
{
}
bool getTrialElements(std::ifstream& inFile, double* elementArray, int ELEMENTS)
{
//Read inFile in to elementArray
//for ELEMENTS
//if can't read or no more elements to read, return false, else return true
while(getTrialElements(inFile, elementArray, ELEMENTS))
{
if(ELEMENTS < 99)
{
cout << "No more elements available." << endl;
}
return 0;
}
}
double printTrialResults(std::ofstream& outFile, int trialNumber, double elementArray[], int ELEMENTS)
{
//
}
void sortArray()
{
}
所以你搜索了整個互聯網,發現沒有辦法排序原始數字的數組?好的:D – xander
你的問題太廣泛了。刪除/註釋掉代碼,直到您編譯並運行一些代碼,然後開始添加代碼。一次處理一個問題。 – Svaberg