【问题标题】:Having issues eliminating duplicates and sorting from C++ array outfile从 C++ 数组输出文件中消除重复和排序时遇到问题
【发布时间】:2017-11-09 01:21:49
【问题描述】:

尝试从文本文件创建唯一成绩列表。输出有问题,消除重复。目前,我正在尝试将每个前一个数组条目的值与下一个数组条目的值进行比较,如果它们不同,则将结果输出到 outfile,但只是输出一个空文件。

我也很好奇是否有一个简单的解决方法可以将排序从“低到高”更改为“高到低”。先感谢您。

#include <iostream>
#include <string>
#include <limits>
#include <cmath>
#include <iomanip>
#include <fstream>

using namespace std;

int testScoreArray[100];
void selectSort(int testScoreArray[], int n);
void fileOutput(int testScoreArray[]);

int main()

{
    int n = 100;

    ifstream infile;
    infile.open("testscoresarrayhomework.txt");

    for (int i = 0; i < 100; i++) {

        infile >> testScoreArray[i];
    }

    selectSort(testScoreArray, n);
    fileOutput(testScoreArray);

    infile.close();

    return 0;
}

void selectSort(int testScoreArray[], int n)
{
    //pos_min is short for position of min
    int pos_min, temp;

    for (int i = 0; i < n - 1; i++) {
        pos_min = i; //set pos_min to the current index of array

        for (int j = i + 1; j < n; j++) {

            if (testScoreArray[j] < testScoreArray[pos_min])
                pos_min = j;
            //pos_min will keep track of the index that min is in, this is needed when a swap happens
        }

        //if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
        if (pos_min != i) {
            temp = testScoreArray[i];
            testScoreArray[i] = testScoreArray[pos_min];
            testScoreArray[pos_min] = temp;
        }
    }
};

void fileOutput(int testScoreArray[])
{

    ofstream outfile;
    int gradeEvent = 0;
    int previousGrade = 0;

    outfile.open("testscoresoutput.txt");
    outfile << "Test Score Breakdown: ";
    outfile << endl
            << "Score / Occurance";

    for (int i = 0; i < 100; i++) {
        previousGrade = i;

        if (previousGrade && previousGrade != i) {

            outfile << '\n' << testScoreArray[i] << " / " << gradeEvent;
        }
    }

    outfile.close();
};

【问题讨论】:

标签: c++ arrays file-io


【解决方案1】:

您已经声明了一个全局变量testScoreArray,并且函数名称对其参数使用相同的变量名称。最好尽可能避免使用全局变量。您可以删除全局声明,然后在 main 中声明 testScoreArray,并将其传递给您的函数。示例:

//int testScoreArray[100]; <=== comment out
void selectSort(int *testScoreArray, int n);
void fileOutput(int *testScoreArray, int n); //add array size

int main()
{
    int testScoreArray[100]; //<== add testScoreArray in here
    int n = sizeof(testScoreArray) / sizeof(testScoreArray[0]);
    selectSort(testScoreArray, n);
    fileOutput(testScoreArray, n);
    ...
}

fileOutput 中,您基本上是在检查i != i,您需要检查数组,而不是在循环中索引:

void fileOutput(int *testScoreArray, int n)
{
    ofstream outfile("testscoresoutput.txt");
    for(int i = 0; i < n; i++) 
        if(i && testScoreArray[i] != testScoreArray[i-1])
            outfile << testScoreArray[i] << "\n";
};

要反转排序,只需更改此比较中的条件

if (testScoreArray[j] < testScoreArray[pos_min])
    pos_min = j;

收件人:

if(testScoreArray[j] > testScoreArray[pos_min])
    pos_min = j;

从技术上讲,您可以将变量重命名为 pos_max

【讨论】:

    猜你喜欢
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多