【问题标题】:How to output the lowest and highest values from a data file using dynamic arrays?如何使用动态数组从数据文件中输出最小值和最大值?
【发布时间】:2020-10-02 23:43:14
【问题描述】:

我无法输出包含在 txt 文件中的整数的最小值和最大值。

我的方向是建立一个动态数组来保存数组中值的确切数量,并将文件中的值读入数组中。

值在数组中后,使用冒泡排序对它们进行排序。然后输出数组中的最小值和最大值。

================================================ =============

不幸的是,我不允许使用向量。 :(


这是我编写的代码,感谢您的帮助!

#include <iostream>
#include <fstream>
using namespace std;

void bubbleSort(int[], int);
void printArray(int[], int);

int main()
{
    // open the file
    ifstream file;
    file.open("4253512.txt");

    // program reads file and counts how many numbers in array.
    cout << "Reading from the file..." << endl;
    float i;
    int nElements = 0;
    while (file >> i) // checks whether it's good or not
    {
        nElements++;
    }
    cout << "The amount of numbers in the file are: " << nElements << endl;

    //put numbers into dynamic array

    int *arr = new int[nElements];
    for (i = 0; i < nElements; i++)
    {
        file >> arr[nElements];
    }

    // bubble sort the numbers

    bubbleSort(arr, nElements);

    // printing out the highest and lowest values

    printArray(arr, nElements);

    delete[] arr;
    file.close();

    return 0;
}

void bubbleSort(int arr[], int n)
{
    for(int j = 0; j < n; j++)
    {
        for(int i = j + 1; i < n; i++)
        {
            if(arr[j] > arr[i]) // swap if bigger
               swap(arr[j], arr[i]);
        }
    }
}


void printArray(int arr[], int n)
{
    cout << "The lowest value of the array is " << arr[0] <<
            " and the highest value is " << arr[n] << endl;
}

顺便说一句,构建和运行时没有运行时错误。

【问题讨论】:

  • your rubber duck 讨论for (i = 0; i &lt; nElements; i++) { file &gt;&gt; arr[nElements]; }。重点讨论正在读入的数组元素。
  • 禁止使用向量,那么使用std::list怎么样?
  • @MikeCAT std::deque 在这里可能会更高效。
  • 还讨论完成while (file &gt;&gt; i) { nElements++; }后文件中还有多少元素需要读取。 Duckie 可能会建议在尝试再次读取之前返回文件的开头。
  • arr[n] in printArray 超出范围。

标签: c++ ifstream bubble-sort dynamic-arrays


【解决方案1】:

代码很少有问题。 不转到文件流的开头,使用 nElements 而不是 i 将值传递给数组,访问 arr[n] 而不是 n-1。

#include <iostream>
#include <fstream>
using namespace std;

void bubbleSort(int[], int);
void printArray(int[], int);

int main()
{
    // open the file
    ifstream file;
    file.open("4253512.txt");

    // program reads file and counts how many numbers in array.
    cout << "Reading from the file..." << endl;
    float i;
    int nElements = 0;
    while (file >> i) // checks whether it's good or not
    {
        nElements++;
    }
    cout << "The amount of numbers in the file are: " << nElements << endl;

    //seek to beginning of file stream
    file.clear();
    file.seekg(0);
    //put numbers into dynamic array
    int* arr = new int[nElements];
    for (i = 0; i < nElements; i++)
    {
        //pass to i not nElements
        file >> arr[(int)i];
    }

    // bubble sort the numbers
    bubbleSort(arr, nElements);

    // printing out the highest and lowest values

    printArray(arr, nElements);

    delete[] arr;
    file.close();

    return 0;
}

void bubbleSort(int arr[], int n)
{
    for (int j = 0; j < n; j++)
    {
        for (int i = j + 1; i < n; i++)
        {
            if (arr[j] > arr[i]) // swap if bigger
                swap(arr[j], arr[i]);
        }
    }
}


void printArray(int arr[], int n)
{
    cout << "The lowest value of the array is " << arr[0] <<
        //                                  n-1 not n
        " and the highest value is " << arr[n-1] << endl;
}

【讨论】:

  • 与其将i 转换为int,不如将​​其定义为int
  • 是的,只是不想过多更改代码。我想这不是一个很大的机会。
  • 同意这种观点。提问者不太可能看到错误,除非他们得到一个包含 800 万左右条目的文件。
  • 谢谢你!是的,我的错误有几个错误:/我认为 n-1 的事情是脑残,但我仍然感谢您的帮助!
  • 随时!很高兴解决了这个问题并帮助澄清了 istream 的使用。
猜你喜欢
  • 2017-06-26
  • 2013-04-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-30
  • 1970-01-01
  • 2022-10-23
  • 1970-01-01
  • 2016-08-12
相关资源
最近更新 更多