reading txt with2D array then pass it to function

Apr 20, 2018 at 11:35pm
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
ifstream data;
string filename;
double array[6][3];
cout<<"Enter file name: ";
cin>>filename;
data.open(filename.c_str());
for( int row=0; row<6; row++)
{
for(int col=0; col<3; col++)
{
data>>array[row][col];
cout<<array[row][col]<<" ";
}cout<<endl;
}


return 0;
}
/*
Enter file name: data.txt
1 2 3
4 5 6
7 8 9
11 12 13
14 15 17
18 2.34269e-317 2.35933e-317

--------------------------------
Process exited after 4.012 seconds with return value 0
Press any key to continue . . .
*/
*-------------------------------------------------
the data.txt contains
1 2 3
4 5 6
7 8 9
11 12 13
14 15 17
18


all i want to do is the 2nd column.
pls. help
Last edited on Apr 22, 2018 at 4:33am
Apr 21, 2018 at 12:57am
all i want to do is the 2nd column.

What do you mean?
Apr 21, 2018 at 1:06am
Please put your code within the code tags when putting your code here so it'll be easier to read.

Before writing data from the file to your array I'd put an if statement to check end of file. Simple as
1
2
if (!data.eof())
     input and output code here


What I don't understand is do you only want to print the 2nd column? If so then it's just a for loop as such:
1
2
for (int row = 0; row < 6; ++row]
    cout << array[row][1]

That'll set the loop to only print the 2nd column of each row via [i][1].

Also a tip is you can (and should) set a const, or constexpr (c++11 or newer) type to rows and columns and reuse that expression for your array sizes and loop iterations.
Apr 22, 2018 at 4:24am
thanks.
yes, that's what i want and then get the odd terms NOT ODD NUMBER but odd terms only in the second column ( e.x 2, 8, 15). how would i do that?
Last edited on Apr 22, 2018 at 4:36am
Apr 22, 2018 at 4:44am
instead of ++row do row += 2
Apr 22, 2018 at 5:25am
it didn't work. it just printing column 2.
i started doing this:
cout<< array[0][1]<<endl;//print the column
cout<< array[0][7]<<endl;//print the column
cout<< array[0][13]<<endl;//print the column
cout<< array[0][19]<<endl;//print the column
cout<< array[0][25]<<endl;//print the column
cout<< array[0][31]<<endl;//print the column
cout<< array[0][36]<<endl;//print the column
it printed out but how can i sum it all up?
Apr 22, 2018 at 12:02pm
You’d better take advantage of the C++11 features.
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
// Data.txt:
// 1 2 3
// 4 5 6
// 7 8 9
// 11 12 13
// 14 15 17
// 18
// 
// What's above means:
//              columns
//              0  1  2
// row 0  -->   1  2  3
// 
//              columns
//              0  1  2
// row 1  -->   4  5  6
// 
//              columns
//              0  1  2
// row 2  -->   7  8  9
// 
//              columns
//              0  1  2
// row 3  -->  11 12 13
// 
//              columns
//              0  1  2
// row 4  -->  14 15 17
// 
//              columns
//              0  1  2
// row 5  -->  18

#include <fstream>
#include <iostream>
#include <string>

// using namespace std;     <-- this introduces 'array' in the global namespace
                            //  http://en.cppreference.com/w/cpp/container/array
                            // See above: you call a C-Style array 'array'

int main()
{
    std::cout << "Enter file name: ";
    std::string filename;
    std::cin >> filename;
    std::ifstream data(filename);

    // 'array': poor name!
    double array[6][3] {};  // initialize all the array to 0

    for( int row = 0; row < 6; row++)
    {
        for(int col = 0; col < 3; col++)
        {
            // Beware! This statement will attempt to read after the file end:
            data >> array[row][col];
            std::cout << array[row][col] << ' ';
        }
        std::cout << '\n';
    }

    // Sum up 'array' second column:
    int sum {};
    for(int row {}; row < 6; ++row) {
        for(int col {}; col < 3; ++col) {
            if(col ... ) {  // ???
                // make the sum
            }
        }
    }
    // output the sum
    return 0;
}

Apr 22, 2018 at 7:41pm
the data file contains:
Data.txt:
1 2 3
4 5 6
7 8 9
11 12 13
14 15 17
18
Input : data file. txt array[6][3] = {0}

Output :
odd = 2 + 8 + 15 + 0 = 25
even = 5 + 12 + 0 = 17

this is what is have so far:

for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
file >> array[i][j];
if (i % 2 == 0)
even += array[i][1];
else
odd += array[i][1];
}
} cout << " Even index positions sum " << even<<endl;
cout << " n Odd index positions sum " << odd;

pls. help. thanks
Topic archived. No new replies allowed.