Help with Matrix Readout?

I am trying to create a code that takes matrices from a couple of files and performs mathematical operations with them to get a third matrix. The whole code is shown at the bottom of the page, but directly below this question is the part of code I am struggling with.

1
2
3
4
5
6
7
8
for (i = 0; i <= 9; i++)
		{
			for (j = 0; j <= 9; j++)
			{
				z[i][j] = x[i][j] + y[i][j];
				cout << setw(9) << z[i][j];
			}
		}


I want this to print the third matrix to the console in a 10x10 form, yet when the program runs right now, it prints the numbers one following another without the 10x10 organization. How can I fix this?

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
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<iostream>
#include<iomanip>
#include<fstream>
#include<math.h>
#include<string>
using namespace std;

int main()
{
	ifstream infile;
	int i, j;
	int x[10][10], y[10][10];
	int z[10][10];

	infile.open("C:\\C++\\Matrix1.txt");
	if (!infile.is_open())
		cout << "file could not be opened." << endl;

	while (infile.good())
	{
		for (i = 0; i <= 9; i++)
		{
			for (j = 0; j <= 9; j++)
				infile >> x[i][j];
		}
	}
	infile.close();

	infile.open("C:\\C++\\Matrix2.txt");
	if (!infile.is_open())
		cout << "file could not be opened." << endl;

	while (infile.good())
	{
		for (i = 0; i <= 9; i++)
		{
			for (j = 0; j <= 9; j++)
			{
				infile >> y[i][j];
			}
		}
	}
	infile.close();

	int c;
	cout << "What operation would you like to perform on the two matrices?" << endl;
	cout << "1 = addition" << endl;
	cout << "2 = subtraction" << endl;
	cout << "3 = multiplication" << endl;
	cout << "Your selection (1-3): ";
	cin >> c;

	if (c == 1)
	{
		for (i = 0; i <= 9; i++)
		{
			for (j = 0; j <= 9; j++)
			{
				z[i][j] = x[i][j] + y[i][j];
				cout << setw(9) << z[i][j];
			}
		}
	}
	else if (c == 2)
	{
		for (i = 0; i <= 9; i++)
		{
			for (j = 0; j <= 9; j++)
			{
				z[i][j] = x[i][j] - y[i][j];
				cout << setw(9) << z[i][j];
			}
		}
	}
	else if (c == 3)
	{
		for (i = 0; i <= 9; i++)
		{
			for (j = 0; j <= 9; j++)
			{
				z[i][j] = x[i][j] * y[i][j];
				cout << setw(9) << z[i][j];
			}
		}
	}
	else
	{
		cout << "You did not select an available option." << endl;
	}
	return 0;
}
Add an endl to your cout:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

for( i = 0; i <= 9; i++ )
    {
    for( j = 0; j <= 9; j++ )
        {
        z[ i ] [ j ] = x[ i ][ j ] + y[ i ] [ j ];

        cout << setw( 9 ) << z[ i ][ j ];

        }    /* for( j <= 9 )    */

    cout << endl;

    }    /* for( i <= 9 )    */

cout << setw(9) << z[i][j];
This worked, thank you! What if I want to allow the user to choose how big the square matrix is, up to 10? Would I replace all the 9's in my code with an "n" for example and have the user enter the value for n at the beginning of the program? Or is it more complicated than that? I assume that would mess with the variable input from the file, and if so, how would one go about this?
Sorry for the late answer: I simply forgot to check back.

First of all, I'm glad that helped!

What you want to do with the user specifying the size of the matrix is doable, but you have use new or malloc to create your arrays on the fly.

The better way is to use a Standard Template Library (STL) container such as an array or vector: Those containers handle all of the memory allocation for you.
Topic archived. No new replies allowed.