输出文件中显示极大的数字

时间:2014-04-18 04:59:49

标签: c++

我有一个程序,它接收一个数字文件(5位数 - 1位数),对其进行排序,然后计算平均值。

问题是,由于一些奇怪的原因,我似乎得到了随机数字。例如,这是输出文件的一个示例(由于某种原因,它每次都会更改):

-1634367306
-1461109043
-542664683
-542664639
-542664491
-2
-1
-1
0
0

到最后......

2003150324
2003165000
2003165000
2003165011
2003165011
2003165090
2003195799
2003196010
2003196054
2003284685
2003834952
2006176524
2006176524
2006221796
2006221796

输入文件中的数字是0 - 99999,所以我不知道为什么会出现这些数字。

这是我的代码:

#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;


void getData(int[], int);
void outputData(int[], int);
double calcAverage(int[], int);
int findHighest(int[], int);
int findLowest(int[], int);
void removeDuplicates(int[], int);
void selectionSort (int[], int);

int main() {
    const int SIZE = 1000;
    int table[SIZE];

    getData(table, SIZE);
    selectionSort(table, SIZE);

    cout << "Highest number: " << findHighest(table, SIZE) << endl;
    cout << "Lowest number: " << findLowest(table, SIZE) << endl;
    cout << "Average: " << calcAverage(table, SIZE) << endl;

    outputData(table, SIZE);
}

/** selectionSort
**  - Sorts an array of numbers
**/
void selectionSort(int array[], int size) {
    int startScan, minIndex, minValue;
        for (startScan = 0; startScan < (size - 1); startScan++) {
        minIndex = startScan;
        minValue = array[startScan];
        for (int index = startScan + 1; index < size; index++) {
            if (array[index] < minValue) {
                minValue = array[index];
                minIndex = index;
            }
        }
        array[minIndex] = array[startScan];
        array[startScan] = minValue;
    }
}


/** getData
** - Opens a file of a set of numbers
** - Reads data from file into an array of numbers
**/
void getData(int table[], int size) {
    ifstream iFile;
    iFile.open("numbers.txt");

    if (!iFile) {
        cout << "File failed to load, please try again." << endl;
        return;
    }

    for (int i = 0; i < size; i++) {
        iFile >> table[i];
    }
    iFile.close();
}

/** outputData
** - outputs a sorted array of numbers to a text file
**/
void outputData(int table[], int size) {
    ofstream oFile;
    oFile.open("entry.txt");

    for (int i = 0; i < size; i++) {
        oFile << table[i] << endl;
    }
    oFile.close();
}

/** calcAverage
** - Calculate and return average of all data entries
**/
double calcAverage(int table[], int size)  {
    double total = 0;
    for (int i = 0; i < size; i++) {
        total += table[i];
    }
    return total / size;
}

/** findHighest
** - return highest number from array
**/
int findHighest(int table[], int size) {
    int high = 0;
    for (int i = 0; i < size; i++) {
        if (table[i] > high)
            high = table[i];
    }
    return high;
}

/** findLowest
** - return lowest number from array
**/
int findLowest(int table[], int size) {
    int low = findHighest(table, size);
    for (int i = 1; i < size; i++) {
        if (table[i] < low)
            low = table[i];
    }
    return low;
}

最高,最低,平均的典型结果显示:

  

最高分:2006221796最低号码:2006221796平均次数:2.71055e   + 007

不知道我在这里犯错了什么。没有编译器错误,我很确定所有内容都已正确初始化。

2 个答案:

答案 0 :(得分:4)

&#39;输入文件是无序的,有921行数字&#39; - 那么你的程序如何知道有多少项被读入数组?您声明const int SIZE = 1000;并将该值用作一系列排序 - 因此您需要对数组中未初始化的79个项目进行排序,这些数据不在您的输入中,

答案 1 :(得分:1)

尝试编写这样的输入例程,以确定实际读入的元素数量:

int getData(int table[], int maxsize) {

    ifstream iFile("numbers.txt");
    if (!iFile) {
        cerr << "Can't open number.txt\n";
        return 0;
    }

    int n, i = 0;
    while (iFile >> n) {
        if (i >= maxsize) {
            cerr << "Table overflow\n";
            break;
        }
        table[i++] = n;
    }

    iFile.close();

    return i; // return the number of elements read
}