Vector with arguments declared in for loop not acting right

This code compiles correctly, but the las cout statement returns "(0,2.42092e-322)" The input file I used is

2
4
(0,0) (0,1) (0,2) (0,3)
(1,0) (1,1) (1,2) (1,3)
(2,0) (2,1) (2,2) (2,3)
(3,0) (3,1) (3,2) (3,3)

(0,0) (0,1) (0,2) (0,3)
(1,0) (1,1) (1,2) (1,3)
(2,0) (2,1) (2,2) (2,3)
(3,0) (3,1) (3,2) (3,3)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <boost/mpi/communicator.hpp>
#include <boost/mpi/environment.hpp>
#include <complex>
#include <vector>
#include <fstream>
#include <sstream>

using namespace std;

int main(int argc, char* argv[])
{
   typedef vector<vector<vector<complex<double> > > > chmatrix; //define ~common types (for fun, honestly)
   typedef vector<vector<complex<double> > > cmatrix;

   vector<complex<double> > input_vector;

   int matrix_size, matrix_amount, depth, row, col;

   complex<double> temp_cmpl_db;

   string temp_str_gtl_mat_dt, temp_str_gtl_int_dt; //temporary strings for retrieving matrices

   /*if(!argv)
   {
        cout << "error: no input file specified" << endl;

        return 1;
   }*/

   fstream input_file("example.txt");

   chmatrix input_matrices_transfer;

   if(input_file.is_open())
   {
        getline(input_file, temp_str_gtl_int_dt, ' ');
        matrix_amount = stoi(temp_str_gtl_int_dt, nullptr);
        getline(input_file, temp_str_gtl_int_dt, ' ');
        matrix_size = stoi(temp_str_gtl_int_dt, nullptr);

        chmatrix input_matrices(matrix_size, vector<vector<complex<double> > >(matrix_size, vector<complex<double> >(matrix_amount)));
        chmatrix input_matrices_temp(matrix_size, vector<vector<complex<double> > >(matrix_size, vector<complex<double> >(matrix_amount)));

        for(unsigned int matrix_index = 0; matrix_index < matrix_amount*matrix_size*matrix_size; matrix_index++)
        {
            getline(input_file, temp_str_gtl_mat_dt, ' ');
            cout << temp_str_gtl_mat_dt << endl;

            if(temp_str_gtl_mat_dt != "\n")
            {
                depth = matrix_index/(matrix_size*matrix_size);
                row = (matrix_index - (matrix_size*matrix_size*depth))%matrix_size;
                col = (matrix_index - (matrix_size*matrix_size*depth))/matrix_size;

                istringstream temp_conv_comp(temp_str_gtl_mat_dt);
                temp_conv_comp >> input_matrices[row][col][depth];
                cout << input_matrices[row][col][depth] << endl; //this works

            };

            cout << input_matrices[2][2][2] << endl; //this also works
            input_matrices_transfer = input_matrices;
        };


        input_file.close();

        cout << input_matrices_transfer[2][2][2] << endl; //this doesn't work
   }
   else
   {
    cout << "error: cannot find/open input file" << endl;
    return 1;
   };

   return 0;
};


Sorry I posted my entire contents, but I really don't know what part of it's causing the problem.
The matrix_size is 4 in this case, so the vector contains vectors containing complex numbers it's 4x4x4.
Last edited on
closed account (48T7M4Gy)
Who wrote the program?
I wrote it.
closed account (48T7M4Gy)
So, correct me if I'm wrong, and I'm sure you will, why is line 69 referring to a 3d array while your input appears to me to be 2d? I must be missing something.
input_matrices_transfer is chmatrix (custom hyper matrix if you were wondering)
input_matrices is also a chmatrix

input_matrices_temp is unused here, sorry.

line 62 and 63 was just me trying desperately to fix whatever is going on.
Everytime I try calling the arrary outside the for loop, I get a strange double value and I don't know where the problem is happening at.
closed account (48T7M4Gy)
You're making it sound too complicated, and maybe I'm missing something but input_matrices_transfer[2][2][2] is a 3d array of doubles. How that is different from what you say is beyond me.

However a very-near-zero double is 2.42092e-322. i.e. You have a rounding error and there's nothing you can do about it other than allow for it !

Last edited on
You're right, sorry, yes, input_matrices_tranfer is a 3d array of doubles. However, the output should be (2,2) in the second cout statement, but instead it's as you said a very near zero value (which i guess is essentially zero). The array was initiallized to a zeros (0,0) but I changed the values in the for loop. But when I try and access the element it's there but its nowhere near the right value because they are all (0,0) (or close to that) for some reason.
NEVERMIND I'M REALLY DUMB SORRY! I was trying to access an element that didn't exist, I forgot that arrays start at 0 instead of 1 so the element 2 doesn't exist. Sorry.
closed account (48T7M4Gy)
Please use line numbers. :)
closed account (48T7M4Gy)
I guess that solves it?
Topic archived. No new replies allowed.