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)
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
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.
#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++;
}
}
The program should read in array values from file in order, print to screen values for array 1, array 2, etc.