Can anyone help me??

Write a C++ program that fills the right to left diagonal of a square matrix with zeros, the lower right triangle with -1s and the upper left triangle with +1s. Let the user enter the size of the matrix up to size 21. Use a constant and error check. Output the formatted square matrix with appropriate values.
So the output is something like this?

1
2
3
4
5
6
7
8
9
10
+1 +1 +1 +1 +1 +1 +1 +1 +1  0 
+1 +1 +1 +1 +1 +1 +1 +1  0 -1 
+1 +1 +1 +1 +1 +1 +1  0 -1 -1 
+1 +1 +1 +1 +1 +1  0 -1 -1 -1 
+1 +1 +1 +1 +1  0 -1 -1 -1 -1 
+1 +1 +1 +1  0 -1 -1 -1 -1 -1 
+1 +1 +1  0 -1 -1 -1 -1 -1 -1 
+1 +1  0 -1 -1 -1 -1 -1 -1 -1 
+1  0 -1 -1 -1 -1 -1 -1 -1 -1 
 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 


You can find a pattern that will allow you to decide what value to output using nested for loops, where the outer loop controls the row, and the inner loop controls the column.

In the case above, the matrix has 10 rows and 10 columns. In row 1, it's all +1 except column 10, which has a zero for the diagonal. In row 2, it's +1 up to column 9 which gets a 0 for the diagonal. In row 3, it's +1 up to column 8 which gets a 0 for the diagonal.

There's a pattern here that can be used to output the 0 for the diagonal.
Row + Col on diagonal 1 + 10
Row + Col on diagonal 2 + 9
Row + Col on diagonal 3 + 8
and so on....

Does that make any sense? The +1 and -1 follows on from this.
I recommend using some tutorials on the web and learn some c++ programming for better coding. Really important to learn a lot as c++ can get very complicated which can lead to errors. Those errors can be avoided. If you find it troubling you can also try using an app to help you, such as checkmarx and others.
Good luck with that!
Ben.
Thanks wildblue
Topic archived. No new replies allowed.