如何使用动态数组从数据文件中输出最低值和最高值?
How to output the lowest and highest values from a data file using dynamic arrays?
我在输出 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;
}
我构建时没有 运行时间错误 运行 顺便说一句。
代码几乎没有问题。
不会到达文件流的开头,使用 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;
}
我在输出 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;
}
我构建时没有 运行时间错误 运行 顺便说一句。
代码几乎没有问题。 不会到达文件流的开头,使用 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;
}