How to read input from a file with alternating data and store to two arrays

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer 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)

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  //need this by default for cin
#include  //includes math functions
#include  //includes basic math 
#include  //includes floating point numbers
#include  //includes setprecision for decimal places
#include  //needed for rand and srand functions
#include  //needed for time function used to seed generator
#include  
#include 
#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;
profilepic.png
manpreet 2 years ago

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;
}
 

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.