0

I can't understand how one can assign some sort of sequence (eg number sequence) from an external file into a vector/array. If it's just a couple of numbers then you just write them in brackets. What if you have 100000 numbers? I hope someone will be qualified enough to answer that.

I need to write a script which would sum particular members of an array. The array is being extracted from one file and put in the other. However I need it more to be a vector, but I can't understand how it works. After I assign values and somehow initialise that vector, I also need to specify that vector members are numbers and then add them up.

Adding procedure in main.

These 2 for cycles should search for particular members of vector. So the first cycle as f = 0, i should add:

x = 1 / AC * ( A * ( grader[4th member] + grader[4 + 3 = 7th member] + grader[4 + 6 = 10th member] + grader[4 + 9 = 13th member] ) + C * grader[4 - 3 = 1st member] )

then print it in the file + " " field separator, then

x = 1 / AC * ( A * ( grader[5th member] + grader[5 + 3 = 8th member] + grader[5 + 6 = 11th member] + grader[5 + 9 = 14th member] ) + C * grader[5 - 3 = 2nd member] )

and finally

x = 1 / AC * ( A * ( grader[6th member] + grader[6 + 3 = 9th member] + grader[6 + 6 = 12th member] + grader[6 + 9 = 15th member] ) + C * grader[6 - 3 = 3nd member] )

cout << endl;

Then the first for cycle should do the same just f should be 15 members up like this: f = 15, so i = 15 + 4 = 19

x = 1 / AC * ( A * ( grader[19th member] + grader[19 + 3 = 22th member] + grader[4 + 6 = 10th member] + grader[4 + 9 = 13th member] ) + C * grader[4 - 3 = 1st member] )

and so on...

P.S. I don't show input file because extracting procedure works. Somebody already helped me with that. Hopefully the code and the array will be sufficient.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
#include <set>

using namespace std;
const double A = 2.66E-23, B = 4.65E-23, AC = 1.53E-22, c = 0;
const int SIZE = 360;
double grades[SIZE];
void readData()
{
    ifstream ifs;
    ofstream ofs;
    ifstream inFile("out.txt", std::ifstream::in);
    ofstream of("out1.txt", std::ofstream::out);
    streambuf* origBuf = cout.rdbuf();
    cout.rdbuf(of.rdbuf());
    //inFile.open(inFileName.c_str());
    if (inFile.is_open())
    {
        std::string s; // to read 2 letters like Ti in your input after   edition

        for (int i = 0; i < SIZE; i++)
        {
            if ((i % 3) == 0)
            {
                if (!(inFile >> s))
                {
                    std::cerr << std::endl
                              << "error when reading letter(s) line "
                              << (i / 3) + 1 << std::endl;
                    return;
                }
            }

            if (!(inFile >> grades[i]))
            {
                std::cerr << std::endl << "error when reading the " << i + 1
                          << " nth double" << std::endl;
                return;
            }
            std::cout << grades[i] << " ";
        }
        cout << endl;
        // std::cout << grades[i] << " ";

    }
    else
    { //Error message   
        std::cerr << "Can't find input file " << inFile << std::endl;
    }
    cout.rdbuf(origBuf);
    ifs.close();
    ofs.close();
    return;
}
int main()
{
    readData();
    std::vector<int> grader[SIZE];
    double x;
    int f = SIZE;
    for (f = 0; f < SIZE; f += 15)
    {
        for (int i = f + 4; i < i + 3; i++)
        {
            x = 1 / AC * (A * (grader[i] + grader[i + 3] + grader[i + 6] + 
                               grader[i + 9]) + C * grader[i - 3]);
            cout << x << " ";
        }
    }
    return 0;
}

Array that I want to be a vector:

3.07097 5.31908 5.50539 -3.37566 6.15709 4.24421 4.97241 6.00196 6.76656 -1.53792 5.17956 5.60283 4.9001 3.92165 5.40794 3.07098 4.6307 1.83512 3.64437 3.94783 0.573947 3.37567 3.7927 3.0963 3.71667 6.02814 1.93257 1.53791 4.77024 1.7377 1.23741 2.14326 5.50539 0.664021 1.46039 4.24421 0.932722 1.30525 6.76656 0.591709 3.5407 5.60283 2.77048 2.28278 5.40794 3.07098 0.344186 1.83512 3.37567 1.18219 0.573947 3.64437 1.02706 3.0963 1.53791 0.204662 1.93257 -0.591718 6.4091 1.7377 6.14195 0 5.50539 2.71164 7.30721 4.24421 7.02003 0.155135 6.76656 0.946195 6.20444 5.60283 5.25459 1.25791 5.40794 6.78321 2.48745 1.83512 5.90513 2.33231 0.573947 5.90513 2.64258 3.0963 7.67057 1.22954 1.93257 -0.946203 3.74536 1.7377 -2.47483 4.9749 20.1864 -1.59675 4.81976 18.9252 -1.59675 5.13003 21.4476 5.25459 3.71699 20.2838 -3.36219 6.2328 20.089 2.47482 4.9749 9.17565 1.59674 5.13003 10.4368 1.59674 4.81976 7.91448 3.36219 6.2328 9.0782 3.36219 3.71699 9.27307 1.23741 7.11816 20.1864 0.664017 6.43529 18.9252 0.932718 6.28015 21.4476 4.9001 1.05324 20.2838 2.77047 7.25768 20.089 -1.23742 2.83164 9.17565 -0.664026 3.51451 10.4368 -0.932727 3.66965 7.91448 -0.591714 1.4342 9.0782 5.8463 2.69211 9.27307 1.23741 2.83164 20.1864 0.932718 3.66965 18.9252 0.664017 3.51451 21.4476 2.77048 2.69211 20.2838 0.591714 1.4342 20.089 -1.23742 7.11816 9.17565 -0.932727 6.28015 10.4368 -0.664026 6.43529 7.91448 -2.77047 7.25768 9.0782 3.71668 1.05324 9.27307 5.5458 0.344186 12.8459 4.97241 1.02706 14.1071 5.24111 1.18219 11.5847 0.591709 6.4091 12.7485 7.07886 0.204662 12.9433 5.5458 4.6307 12.8459 5.24111 3.7927 14.1071 4.97241 3.94783 11.5847 -1.53792 4.77024 12.7485 4.9001 6.02814 12.9433 1.83357 2.48745 12.8459 2.71165 2.64258 14.1071 2.71165 2.33231 11.5847 0.946195 3.74536 12.7485 0.946199 1.22954 12.9433 2.47482 0 16.5162 -2.71164 7.30721 15.255 1.59674 0.155135 17.7773 -0.946203 6.20444 16.6136 3.36219 1.25791 16.4187 3.07097 5.31908 16.5162 3.37566 6.15709 15.255 3.64437 6.00196 17.7773 1.53791 5.17956 16.6136 3.71668 3.92165 16.4187 -1.23741 2.14326 16.5162 -0.664021 1.46039 15.255 7.68405 1.30525 17.7773 -0.591718 3.5407 16.6136 5.8463 2.28278 16.4187

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 2
    dont do all at once. if you need to read numbers from a file and fill a vector then first do that until you know it works, only then move to the next point. Its not really clear what is the problem with your code btw. Do you get errors? – 463035818_is_not_an_ai Jun 05 '19 at 19:09
  • readData() function works well and prints the array. I just want to know how to perfect operations with the array/vector that results or how for real assign the array to vector. I get errors all the time, the last one was that cpp didnt like + and * operations in the for loop. – kazimieras tamoliunas Jun 05 '19 at 19:35
  • @kazimierastamoliunas you declared your vector incorrectly, most of the errors will go away once you've fixed that. – john Jun 05 '19 at 19:39

1 Answers1

1

This is wrong

std::vector<int> grader[SIZE];

that's an array of vectors which I'm sure is not what you want

Try this

std::vector<int> grader(SIZE);

That's a vector of size SIZE. Once you've declared your vector in the correct way you can use it exactly like an array.

This is wrong

for (f = 0; f < SIZE; f += 15)
{
    for (int i = f + 4; i < i + 3; i++)
    {
        x = 1 / AC * (A * (grader[i] + grader[i + 3] + grader[i + 6] + 
                           grader[i + 9]) + C * grader[i - 3]);
        cout << x << " ";
    }
}

when f equals SIZE - 1, i equals f + 4 or SIZE + 3, so grader[i] will be an out-of-bounds access. This would be true whether grader was a vector or an array.

This is wrong

for (int i = f + 4; i < i + 3; i++)

i < i + 3 is always true. So that's clearly not what you intended.

Vectors are easy, your mistakes are either in the syntax, or completely unrelated to vectors.

john
  • 85,011
  • 4
  • 57
  • 81