Finding the sum of diagonals in a 4x4 matrix

The goal of the program is to randomly generate a 4x4 matrix with both 1's and 0's. Then print out whether any diagonals/columns/rows have all 1's or all 0's. The columns and rows are easy, but I am stuck on the diagonals.

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
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <string>
using namespace std;

int main()
{
    srand(time(0));
    int sum;
    int q = 0, w=0;
    int count1 = 0, count2 = 0, count3 = 0, count4 =0;
int array[4][4];
for(int x = 0; x < 4; x++){
 for(int y = 0; y < 4; y++){
     array[x][y] = rand()%2;
         }
    }
    for(int x = 0; x <4; x++){
        for(int y = 0; y < 4; y++){
         cout << array[x][y];   
        }
        cout << endl;
    }
    for(int x = 0; x <4; x++){
        sum = 0;
        for(int y = 0; y < 4; y++){
            sum+=array[x][y];
        }
        if(sum == 4){
         cout << "Row " << x << " has 4" << " 1's" << endl; 
         count1++;
        } else if(sum == 0){
         cout << "Row " << x << " has 4" << " 0's" << endl;   
         count1++;
        }
    }
    for(int x = 0; x <4; x++){
        sum = 0;
        for(int y = 0; y <4; y++){
            sum+=array[y][x];
            
        }
        if(sum == 4){
          cout << "Column " << x << " has 4" << " 1's" << endl;
          count2++;
        } else if(sum ==0){
            cout << "Column " << x << " has 4" << " 0's" << endl;
            count2++;
        }
    }
    while(q < 1){
    for(int x = 0; x < 4; x++){
        for(int y = 0; y <4; y++){
         if(x==y){
          sum+=array[x][y];  
         }
        }
        
    }
    if(sum == 4){
         cout << "Main diagonal has 4" << " 1's" << endl; 
         count3++;
        } else if(sum == 0){
            cout << "Main diagonal has 4" << " 0's" << endl;   
            count3++;
        }
    q++;
    }
    while(w < 1){
    for(int x = 0; x <4; x++){
        for(int y = 0; y<4; y++){
            if(x+y==3){
             sum+=array[x][y];   
            }
        }
    }
    if(sum == 4){
         cout << "Secondary diagonal has 4" << " 1's" << endl; 
         count4++;
        } else if(sum == 0){
            cout << "Secondary diagonal has 4" << " 0's" << endl;   
            count4++;
        }
    w++;
    }
    
    if(count1==0){
     cout << "No same numbers on a row" << endl;
    }
    if(count2==0){
     cout << "No same numbers on a column" << endl;   
    }
    if(count3==0){
        cout << "No same numbers on main diagonal" << endl;   
    }
    if(count4==0){
        cout << "No same numbers on secondary diagonal" << endl;   
    }
  return 0;  
}


My above code will work with columns and rows, but the diagonals will not work. The reason I put it in a while loop was because I wanted to check sum after it had gone through the entire array. If I checked it in the for loop, it would print the result multiple times which is wrong. How can I fix both of the for loops relating to the primary/secondary diagonals? Thanks

Edit: Also i'm almost certain there is a much more efficient way of doing this, but for now I don't mind doing it this way. Also, I know calling all local variables the same thing can get problematic, but since it is a small program I didn't see any reason not change them.

Edit2: I just tried to debug it by printing out the sum, and it seems that it is adding on the row/column sum for some reason, or the sum is some how getting to 5+.

Edit3: I fixed the main diagonal. The secondary diagonal still doesn't work, but I think that is due to my if statement to check if it is one of those numbers is wrong.

Edit4: Fixed it
Last edited on
Topic archived. No new replies allowed.