第一位决议:我正在尝试编写一个程序来计算2个数组的点积。将从文件中读取阵列的数据集。我目前遇到的麻烦是,文件配置为读入“数据集数量” - >“元素数量” - >“第一个数组中的所有元素” - >“第二个数组中的所有元素”。我目前有程序读取适当数量的数据集,每个数组中适当数量的元素,麻烦的是它是读取数组中元素交替的输入。例如,如果我的文件读取“.1,.2,3.0,1.0”并且它要求2个元素,我希望数组1为.1和.2,数组2为3.0和1.0。目前它正在读入,因为数组1是.1和3.0,而数组2是.2和1.0。下面是输入文件的设置(如果格式显示为奇数,则每行一个数据,因此格式为列。有5行fileinfo):
文件的第一个元素给出文件中的数据集数量(整数)然后,每个要处理的数据集按顺序列出,并包含以下内容:每个数组中的元素数量(整数)后跟所有元素在第一个数组中(实数值)后跟第二个数组中的所有元素(实际值)
3 6 0.1 0.2 0.1 0.2 0.1 0.2 3.0 1.0 3.0 1.0 3.0 1.0 11 1.0 -.2 .5 .75 .9 -1.1 1.5 1.8 2.25 2.75 -3. 101.0 80.4 -20.5 -30.0 31.2 32.8 34.7 36.1 0.0 38.4 39.12 12 1.05 2.05 3.05 4.05 5.05 6.05 6.05 7.05 8.05 9.05 10.05 11.05 -11.05 -10.05 -9.05 -8.05 -7.05 -6.05 6.05 5.05 4.05 3.05 2.05 1.05
它当前正在从文件中错误地读取数组的输入。您可以看到我的输入文件显示3(对于数据集)和6(对于元素)它有0.1 0.2 0.1 0.2 0.1 0.2 3.0 1.0 3.0 1.0 3.0 1.0。它应该将阵列1打印为.1 .2 .1 .2 .1 .2,将阵列2打印为3.0 1.0 3.0 1.0 3.0 1.0,但它当前打印阵列1为.1 .1 .1 3.0 3.0 3.0和阵列2为.2 .2 .2 1.0 1.0 1.0
编辑现在数据工作正常,我的dotProduct计算似乎没有问题。我已更新代码以反映点积计算。它意味着通过诸如dotproduct =(x0 * y0)+(x1 * y1)+(x2 * y2)之类的公式计算,一直到数组中的所有元素。我试图使用嵌套的for循环,但是答案不会像我手工计算的那样出现。我试图以多种方式编辑嵌套for循环,但仍然没有运气。
#include "pch.h"
#include <iostream> //need this by default for cin
#include <math.h> //includes math functions
#include <cmath> //includes basic math
#include <cfloat> //includes floating point numbers
#include <iomanip> //includes setprecision for decimal places
#include <cstdlib> //needed for rand and srand functions
#include <ctime> //needed for time function used to seed generator
#include <climits>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile;
string fileinfo1, fileinfo2, fileinfo3, fileinfo4, fileinfo5;
string filename;
float array1[50];
float array2[50];
int count = 0;
int datasets;
int elements;
int a = 0;
int b = 0;
float x;
float y;
cout << "Please enter the input file name: ";
cin >> filename; //allows user to input filename
cout << endl;
cout << "The input file is " << filename << endl << endl;
inputFile.open(filename); //opens file
getline(inputFile, fileinfo1);
getline(inputFile, fileinfo2);
getline(inputFile, fileinfo3);
getline(inputFile, fileinfo4);
getline(inputFile, fileinfo5);
cout << fileinfo1 << endl;
cout << fileinfo2 << endl;
cout << fileinfo3 << endl;
cout << fileinfo4 << endl;
cout << fileinfo5 << endl << endl;
inputFile >> datasets;
while (count != datasets)
{
inputFile >> elements;
cout << "Element\t\t" << "Array 1\t\t" << "Array 2" << endl;
for (int i = 0; i < elements; i++)
{
inputFile >> array1[i];
}
for (int i = 0; i < elements; i++)
{
inputFile >> array2[i];
}
for (int i = 0; i < elements; i++)
{
cout << setprecision(2) << fixed << i << "\t\t" << array1[i] <<
"\t\t" << array2[i] << endl;
}
for (int i = 0; i < elements; i++)
{
dotProduct = 0;
for (int j = 0; j < elements; j++)
{
dotProduct += (array1[i] * array2[i]);
}
}
cout << endl;
cout << "The dot product of the two arrays is " << dotProduct << "." <<
endl << endl;
count++;
}
}
程序应按顺序读取文件中的数组值,打印到数组1,数组2等的屏幕值。
目前,您正在交替写入哪个数组。你需要做的是将几个元素写入第一个数组,然后是第二个数组。然后在读入每个数据集后输出字符串。
此外,代码中的a和b没有被更改,因此它总是写入第0个数组元素。
这样的事情应该有效:
for (int i = 0; i < elements; i++)
{
inputFile >> array0[i];
}
for (int i = 0; i < elements; i++)
{
inputFile >> array1[i];
}
for (int i = 0; i < elements; i++)
{
cout << setprecision(2) << fixed << i <<
"\t\t" << array0[i] << "\t\t" << array1[i] << endl;
}