At the moment, you are alternating which array you write to. What you need to be doing is writing several elements to the first array, then the second array afterwards. Then output the string after each dataset has been read in.
Also, a and b aren't being changed in your code, so it's always writing to the 0th array element.
Something like this should work:
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;
}
manpreet
Best Answer
2 years ago
FIRST BIT RESOLVED: I am attempting to write a program that calculates dot product of 2 arrays. The data sets for the arrays will be read in from a file. The trouble I am currently running into, is the file is configured to read-in as "number of data sets" -> "number of elements" -> "all elements in first array" -> "all elements in second array". I currently have the program reading in the appropriate amount of data sets, the appropriate amount of elements in each array, the trouble is it is reading the input for the elements in the arrays alternating. Example would be if my file read ".1, .2, 3.0, 1.0" and it asked for 2 elements I would want array 1 to be .1 and .2 and array 2 to be 3.0 and 1.0. Currently it is reading in as array 1 is .1 and 3.0 and array 2 is .2 and 1.0. Below is the input file set up (if formatting shows odd, it is one piece of data per line, so it is formatted like a column. There are 5 lines of fileinfo):
The first element of the file gives the number of data sets in the file (integer) Then each data set to be processed is listed sequentially, and contains the following: the number of elements in each array (integer) followed by all of the elements in the first array (real values) followed by all of the elements in the second array (real values)
It is currently reading the inputs for the arrays from the file incorrectly. You can see for my input file it shows after 3 (for data sets) and 6 (for elements) it has 0.1 0.2 0.1 0.2 0.1 0.2 3.0 1.0 3.0 1.0 3.0 1.0. It should be printing Array 1 as .1 .2 .1 .2 .1 .2 and Array 2 as 3.0 1.0 3.0 1.0 3.0 1.0 but it is currently printing Array 1 as .1 .1 .1 3.0 3.0 3.0 and Array 2 as .2 .2 .2 1.0 1.0 1.0
EDIT Now that the data is working properly, my dotProduct calculation seems to be out of whack. I have updated the code to reflect the dot product calculation. It is meant to be calculated by a formula such as dotproduct = (x0 * y0) + (x1 * y1) + (x2 * y2) and so forth all the way through all elements in the array. I was attempting to use a nested for loop, but the answers are not coming up as I calculated by hand. I have attempted editing the nested for loop in multiple ways, but still am having no luck.