My code only works for specific test cases, and I'm not sure why.

I'm doing an extra credit assignment and my teacher said that my code doesn't work for all test cases, only specific ones. I'm not sure why. She also said that my first test case doesn't work, even though it does? I have to find the minimum sum of values in a square matrix.

EDIT:

The code doesn't work when the last row has the minimum value, in sum cases.

EDIT:

I figured it out, I am dumb lol


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
#include <iostream>
using namespace std;

const int SIZE = 4;
//---------------------------------------------------------------
int findMin (int myArray[ ])
{
    int minimum = myArray[0];
    
    for(int index = 0; index < SIZE - 1; index++)
    {
        if(myArray[index] < minimum)
        {
            minimum = myArray[index];
        }
    }
    
    return minimum;
}
//---------------------------------------------------------------
void displayArray (int myArray[ ])
{
    for(int index = 0; index < SIZE; index++)
    {
        cout<< "Row "<< index<< " Sum: "<< myArray[index]<< endl;
    }
}
//---------------------------------------------------------------
void getRowSums (int matrix[][SIZE], int myArray[ ])
{
    int result;
    
    cout<< endl;
    
    for (int rows = 0; rows < SIZE; rows++)
    {
        for (int cols = 0; cols < SIZE; cols++)
        {
            result = result + matrix[rows][cols];
            
            myArray[rows] = result;
        }
        
        result = 0;
    }
}
//---------------------------------------------------------------
void displayMatrix (const int matrix [][SIZE])
{
    for (int rows = 0; rows < SIZE; rows++)
    {
        cout<< endl;
        
        for (int cols = 0; cols < SIZE; cols++)
        {
            cout<< matrix[rows][cols] << "\t"<< "\t";
        }
    }
    cout<< endl;
}
//---------------------------------------------------------------
int main ()
{
    int minimum;
    
    int matrix[SIZE][SIZE] = {   {1,2,3,4},
                                 {5,0,2,2},
                                 {8,1,10,3},
                                 {1,4,6,8}    };
                                 
    displayMatrix(matrix);                             
    
    int myArray[SIZE];
    
    getRowSums(matrix, myArray);
    
    displayArray(myArray);
    
    minimum = findMin(myArray);
    
    cout<< endl<< "The minimum row sum is: "<< minimum;
   
   return (0);
   
}



TEST CASES:

1		2		3		4		
5		0		2		2		
8		1		10		3		
1		4		6		8		

Row 0 Sum: 10
Row 1 Sum: 9
Row 2 Sum: 22
Row 3 Sum: 19

The minimum row sum is: 9

===============================================

2		3		4		5		
8		8		9		5		
9		10		10		15		
5		12		6		8		

Row 0 Sum: 14
Row 1 Sum: 30
Row 2 Sum: 44
Row 3 Sum: 31

The minimum row sum is: 14

===============================================

33		4		19		20		
29		7		13		2		
15		5		7		3		
20		4		16		5		

Row 0 Sum: 76
Row 1 Sum: 51
Row 2 Sum: 30
Row 3 Sum: 45

The minimum row sum is: 30

===============================================

3		3		3		3		
2		2		2		2		
1		1		1		1		
0		0		0		0		

Row 0 Sum: 12
Row 1 Sum: 8
Row 2 Sum: 4
Row 3 Sum: 0

The minimum row sum is: 0

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void getRowSums (int matrix[][SIZE], int myArray[ ])
{
    int result;
    // what is the value of 'result' right now?
    cout<< endl;
    for (int rows = 0; rows < SIZE; rows++)
    {
        for (int cols = 0; cols < SIZE; cols++)
        {
            result = result + matrix[rows][cols];
            myArray[rows] = result;
        }
        result = 0;
    }
}

A good compiler / compiler with good verbosity options should warn about use of uninitialized variable.
result is meant to change, it is the sum of the values in the row of the matrix
result is meant to change, it is the sum of the values in the row of the matrix

Adding to an uninitialized variable will result in uninitialized results. An uninitialized variable will contain garbage, you have no control over what it contains.

Line 31, set result to zero so when you sum the values you are not starting with a bogus value.
Topic archived. No new replies allowed.