Multiplication of row element 2 D array

For some reason I keep getting zero as my result. I am supposed to multiply row elements of the array A[4][6]. Can someone tell me whats wrong with my code?
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
#include<iostream>

using namespace std;

int main()
{
    const int m = 4; // Number of rows of the array
    
    const int n = 6; // Number of columns
    
    int A[m][n] =    // elements of the array
    {
       {1,2,3,4,5},
       {4,3,5,2,7},
       {1,2,4,5,6},
       {3,4,5,7,8}
    };
    
    int mult = 1; 
    
    for(int r = 0; r < m; r++) // loop through rows
    {
        for(int c = 0; c < n; c++) // loop through columns
        {
            
            mult *= A[r][c]; // multiply row
        }
    }
    cout <<"The result is: "<< mult << endl; // Display the result
    
    cin.ignore();
    cin.get();
    
    return 0;
}
they array has 5 collumns, not 6
Ok I corrected it but now I am getting a negative number as a result something must be wrong... here is my updated code.

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
#include<iostream>

using namespace std;

int main()
{
    const int m = 4;
    
    const int n = 6;
    
    int A[m][n] =
    {
       {1,2,3,4,5,8},
       {4,3,5,2,7,6},
       {1,2,4,5,6,4},
       {3,4,5,7,8,5}
    };
    
    int mult = 1;
    
    for(int r = 0; r < m; r++)
    {
        for(int c = 0; c < n; c++)
        {
            mult *= A[r][c];
        }
    }
    cout <<"The result is: "<< mult;
    
    cin.ignore();
    cin.get();
    
    return 0;
}


Here is the output
The result is: -1545601024




Last edited on
mult is multiplied with each element of the array and then assigned to itself.
The final value of mult is 78033715200000, which is too large for a 4byte integer, so the result wraps iirc.
Topic archived. No new replies allowed.