reading matrix rows, cols, values from file

The first and second lines of the file will contain the
number of rows and the number of columns respectively. The remaining lines in the file will each
contain three integers representing the row, column, and data value for an element in the matrix.
If a particular matrix element is not listed the data value should be assumed to be zero.

This is the file:
3
5
0 4 22
0 3 3
1 0 -3
2 2 -10
2 1 7
2 0 18
1 1 25
0 2 -15

I dont have a problem deleting the ptr or opening the file, just reading the rows, cols and values into the matrix

This is what I have for that part:


while(inputFile >> rows >> cols)
{
if (rows < 1 || cols < 1)
{
cerr << "Error: Bad Value";
return -1;
}

int ** twoD = new int *[rows];

for (int j=0; j < rows; j++)
{
twoD[j] = new int[cols];
}

inputFile >> i >> k >> value;

for (int i = 0; i < rows; i++)
{
for (int k = 0; k < cols; k++)
{
//twoD[i][k] = 0;
//twoD[i][k] = value;
inputFile >> twoD[i][k];
cout << twoD[i][k] << " ";
}
cout << endl;
}

And the output matrix is:

0 3 3 1 0
-3 2 2 -10 2
1 7 2 0 18
-15
closed account (48T7M4Gy)
input file >> i >> k >> value

what is this line?
the line reads in the numbers from the file from line 3 onwards (i is 0, k is 4, value is 22) and so on..

I tried using the while loop for that line but I still didnt get the right output
closed account (48T7M4Gy)
What is 'value' used for?
its used to store the third value from line 3 onwards...its the value in the matrix
For example, the 3rd line says row o, column 4 is the value 22
closed account (48T7M4Gy)
If I understand your problem correctly your program misses out on three elements of the matrix. This line appears to me to be the problem. Give it a go and try your program without this line. :)
I tried that...and I got this output:

0 4 22 0 3
3 1 0 -3 2
2 -10 2 1 7
Error: Bad Value

its reading all the numbers into the matrix, instead of just the 3rd column of values:(

closed account (48T7M4Gy)
Ok, I get it now.

You will need to initialize the new matrix with zero values for each element.

Then,

1
2
inputFile >> i >> k >> value;
twoD[i][k] = value;


Then,
keep the two for loops just for printing out the matrix. So this means they should be outside the while loop. Note also this loop can be used for the initialize step. Instaed of cout etc, just write twoD[i][k] = 0;
Last edited on
So like this?
It doesnt work:(
It just prints a bunch of addresses i think

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
while(inputFile >> rows >> cols)
    {
        if (rows < 1 || cols < 1)
        {
            cerr << "Error: Bad Value";
            return -1;
        }

        int ** twoD = new int *[rows];

        for (int j=0; j < rows; j++)
        {
            twoD[j] = new int[cols];
        }

        for (int i = 0; i < rows; i++)
        {
            for (int k = 0; k < cols; k++)
            {
                twoD[i][k] = 0;
            }
		cout << endl;
        }

        inputFile >> i >> k >> value;
        twoD[i][k] = value;

        for (int i = 0; i < rows; i++)
        {
            for (int k = 0; k < cols; k++)
            {
                cout << twoD[i][k] << " ";
            }
		cout << endl;


        for (int i = 0; i < rows; i++)
        {
            delete[] twoD[i];
        }

	delete[] twoD;
        }
    }
closed account (48T7M4Gy)
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
while(inputFile >> rows >> cols)
    {
        if (rows < 1 || cols < 1)
        {
            cerr << "Error: Bad Value";
            return -1;
        }

        int ** twoD = new int *[rows];

        for (int j=0; j < rows; j++)
        {
            twoD[j] = new int[cols];
        }

        for (int i = 0; i < rows; i++)
        {
            for (int k = 0; k < cols; k++)
            {
                twoD[i][k] = 0;
            }
		//cout << endl;
        }

        inputFile >> i >> k >> value;
        twoD[i][k] = value;
    }// end while loop here

        for (int i = 0; i < rows; i++)
        {
            for (int k = 0; k < cols; k++)
            {
                cout << twoD[i][k] << " ";
            }
		cout << endl;
         }// printing complete 


       // Final clean up

        for (int i = 0; i < rows; i++)
        {
            delete[] twoD[i];
        }

	delete[] twoD;
       // revised because while ended earlier }
return 0; // ??
    }
Last edited on
Im sorry it still wont work..this is what i have:

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
int main()
{
    int rows, cols, i, k, value;

    ifstream inputFile;
    inputFile.open("matrix.dat");

    if (inputFile.fail())
    {
       cout << "ERROR: File Not Open." << endl;
       return -1;
    }

    int ** twoD;

   while(inputFile >> rows >> cols)
    {
        //cout << rows << " " << cols;
       if (rows < 1 || cols < 1)
        {
            cerr << "Error: Bad Value";
            return -1;
        }

        twoD = new int *[rows];

        for (int j=0; j < rows; j++)
        {
            twoD[j] = new int[cols];
        }

        for (int i = 0; i < rows; i++)
        {
            for (int k = 0; k < cols; k++)
            {
                twoD[i][k] = 0;
            }
        }

        inputFile >> i >> k >> value;
        twoD[i][k] = value;

    }// end while loop here

        for (int i = 0; i < rows; i++)
        {
            for (int k = 0; k < cols; k++)
            {
                cout << twoD[i][k] << " ";
            }
		cout << endl;
         }// printing complete


       // Final clean up

        for (int i = 0; i < rows; i++)
        {
            delete[] twoD[i];
        }

	delete[] twoD;
       // revised because while ended earlier }
return 0; // ??
    }


when i try to cout the rows and columns it gives : 3 50 0
closed account (48T7M4Gy)
I'll try it on my machine. So I'll get back shortly.
closed account (48T7M4Gy)
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
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    int rows, cols, i, k, value;
    
    ifstream inputFile;
    inputFile.open("matrix.dat");
    
    if (!inputFile.is_open())
    {
        cout << "ERROR: File Not Open." << endl;
        return -1;
    }
    
    int ** twoD;
    
    inputFile >> rows >> cols;
    
    cout << rows << " " << cols << endl;
    if (rows < 1 || cols < 1)
    {
        cerr << "Error: Bad Value";
        return -1;
    }
    
    twoD = new int *[rows];
    
    for (int j=0; j < rows; j++)
    {
        twoD[j] = new int[cols];
    }
    
    for (int i = 0; i < rows; i++)
    {
        for (int k = 0; k < cols; k++)
        {
            twoD[i][k] = 0;
        }
    }
    
    while(inputFile >> i >> k >> value)
    {
        twoD[i][k] = value;
    }
    
    
    for (int i = 0; i < rows; i++)
    {
        for (int k = 0; k < cols; k++)
        {
            cout << twoD[i][k] << "**";
        }
        cout << endl;
    }// printing complete
    
    
    
    return 0; // ??
}
Thank you so much!!!

I guess i complicated it too much!

Thanks again:):)
closed account (48T7M4Gy)
Cheers :)
Topic archived. No new replies allowed.