[英]How can I read numbers from an file and use them in an array?
I am making a program that asked the user to open and existing txt file.我正在制作一个程序,要求用户打开现有的 txt 文件。 The program should read the numbers from the file and storing them into arrays.
程序应该从文件中读取数字并将它们存储到 arrays 中。 I am supposed to create different functions with those numbers in the array, such as getting the largest number, the lowest number, the sum, and the average.
我应该用数组中的这些数字创建不同的函数,例如获取最大数字、最小数字、总和和平均值。 I have done the functions already but I do not know how to extract the numbers from the array.
我已经完成了这些功能,但我不知道如何从数组中提取数字。
Here is an example of numbers but instead of being separated by space, they're separated by a new line.这是一个数字示例,但不是用空格分隔,而是用新行分隔。
53 22 87 103 -3 75 220 1 64 543 98 44 53 22 87 103 -3 75 220 1 64 543 98 44
int getLowest(int num[], int size);
int getHighest(int num[], int size);
int getSum(int num[], int size);
int getAverage(int num[], int size);
int main()
{
string fileName;
ifstream inputFile;
const int ARRAY_SIZE = 12;
int numbers[ARRAY_SIZE];
cout << "Enter the name of imput file: ";
cin >> fileName;
inputFile.open(fileName);
if (inputFile)
{
cout << " numbers read from input file.\n"
<< "The lowest value is " << getLowest(numbers, ARRAY_SIZE) << endl;
}
else
{
//Display error message
cout << "Error, this file does not exist.";
}
system("pause");
}
int getLowest(int num[], int size)
{
int temp = num[0];
for (int i = 0; i < size; i++)
{
if (temp < num[i])
{
temp = num[i];
}
}
return temp;
}
int getHighest(int num[], int size)
{
int temp = num[0];
for (int i = 0; i > size; i++)
{
if (temp > num[i])
{
temp = num[i];
}
}
return temp;
}
int getSum(int num[], int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += num[i];
}
return sum;
}
double getAverage(int num[], int size)
{
int sum = 0;
double average;
for (int i = 0; i < size; i++)
{
sum += num[i];
}
average = sum / size;
return average;
}
When I open the file with the numbers shown above, the result I get does not work.当我用上面显示的数字打开文件时,我得到的结果不起作用。 I get:
我得到:
010FFCE0 numbers read from input file. 010FFCE0 从输入文件中读取的数字。 The lowest value is 1968178332
最低值为 1968178332
Where it says "010FFCE0" i would like to get something that says the numbers of values, and the lowest value I would like it to be the actual lowest value.在它说“010FFCE0”的地方,我想得到一些说明值的数量和最低值的东西,我希望它是实际的最低值。
Here is an example of reading numbers from a file into std::vector<int>
:这是从文件中读取数字到
std::vector<int>
的示例:
int number = 0;
std::vector<int> database;
while (inputFile >> number)
{
database.push_back(number);
}
The std::vector
is a good choice here because it expands dynamically as necessary (performing dynamic memory allocation as necessary). std::vector
在这里是一个不错的选择,因为它会根据需要动态扩展(根据需要执行动态 memory 分配)。
However, you don't need to store the numbers in order to determine the highest, lowest and average:但是,您不需要存储数字来确定最高、最低和平均值:
int sum = 0;
int highest;
int lowest;
inputFile >> highest;
lowest = highest;
sum = highest;
int number;
int quantity = 1;
while (inputFile >> number)
{
if (number > highest)
{
highest = number;
}
if (number < lowest)
{
lowest = number;
}
sum += number;
++quantity;
}
double average = (sum * 1.0) / quantity;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.