0

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.

  • What is the problem with the code you show? For some specified input (from user and from file), what output does it show, what output did you expect? And please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Apr 11 '19 at 08:13
  • It is currently reading the inputs for the arrays from the file incorrectly. I apologize for not explaining well above, I will try again. 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 – Scott Johnson Apr 11 '19 at 08:16
  • 1
    @ScottJohnson Please [edit](https://stackoverflow.com/posts/55627648/edit) your revised explanation into the question itself, not in the comments :) – Max Langhof Apr 11 '19 at 08:17
  • Perhaps it's time to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? Use a debugger to step through the code line by line while monitoring variables and their values. – Some programmer dude Apr 11 '19 at 08:22
  • 1
    By the way, what are you really using `array1` and `array2` for? You assign to `array1[a]` and `array2[b]` but then you don't use those values. And you never update `a` or `b`, so you will always be writing to the first element of the arrays. – Some programmer dude Apr 11 '19 at 08:24
  • This should probably show my lack of experience. I understand the idea of arrays, they store larger amounts of data that can then be called upon. Is there anything you can direct me to for reference on how to assign multiple values to an array via input? I attempted to user inputFile >> array1[a] and when I print that it is always 0. Any kind of example or additional information would be incredibly helpful, as the book I have clearly isn't cutting it for me. – Scott Johnson Apr 11 '19 at 08:33
  • Note for the future: You did well to include the data as well as the program, but try and minimize everything for SO. You only needed two datasets, each containing 4 pairs to show the problem, and that makes the data much easier to read. – Martin Bonner supports Monica Apr 11 '19 at 09:06

1 Answers1

0

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;
}
  • Thank you for this detailed explanation. So that I know for the future; Would there have been a way to do this utilizing nested for loops, or are separate loops the only real option? – Scott Johnson Apr 11 '19 at 08:43
  • You might be able to use nested loops if array0 and array1 were combined into a 2D array. In that case, you could loop over each array, then each element. For the given example of just two arrays, I think separate loops is cleaner and easier to understand though. – Tom Marsland Apr 11 '19 at 08:48
  • @ScottJohnson Avoid using arrays as a beginner. You are much better off using `std::vector` (which among other things doesn't have a maximum size). You could have an array of vectors, and then you could have a loop (running from 0 to 1) to select which vector, and an embedded loop (running from 0 to elements -1) to read the values. – Martin Bonner supports Monica Apr 11 '19 at 08:49
  • This is very much a stylistic matter on which reasonable people can disagree, but unlike Tom Marsland, I think the nested loops is cleaner (at least for production code, at your level of understanding, two separate loops may be easier to get right). – Martin Bonner supports Monica Apr 11 '19 at 08:51
  • @MartinBonner agreed with the first point. If you get a dataset that has more than 50 elements, you will be in trouble. Using std::vector with push_back will be very useful, and means you don't have to worry about freeing dynamically allocated memory. – Tom Marsland Apr 11 '19 at 08:54
  • @MartinBonner I hope it is kosher to add on to my question here as you two have been incredibly helpful. I have managed to get the data I want to properly store by what I can see. Now I am attempting to calculate the dot product of the two arrays using a formula like dotproduct= (x0 * y0) + (x1 * y1) + (x2 * y2) and so forth. It is very obvious I will need another loop for this. I am getting janky answers no matter how I form the loop. I will update the question and code above. – Scott Johnson Apr 11 '19 at 09:14
  • @ScottJohnson NO NO NO! – Martin Bonner supports Monica Apr 11 '19 at 09:15
  • If you have another question, ask another question. However, I suggest you read [this question](https://stackoverflow.com/q/388242/771073) first. We can't really teach you how to program. – Martin Bonner supports Monica Apr 11 '19 at 09:15
  • @MartinBonner I will certainly check out one of those reads thank you. I apologize for editing this code, I wasn't sure if that was easier due to already working through it and also the restriction on amount of questions asked per day on this site (which I understand). – Scott Johnson Apr 11 '19 at 09:21