【发布时间】:2017-08-17 04:17:47
【问题描述】:
我在对数组进行排序和使用输入文件中的数据查找计算代码时遇到问题。任何帮助将不胜感激。我已经搜索了互联网,但仍然遇到很多麻烦。
数组正在读取一个包含 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
-
你的问题太宽泛了。删除/注释掉代码,直到你有一些可以编译和运行的东西,然后开始添加东西。一次处理一个问题。
标签: c++ arrays definition