Two dimensional array, output different with my prof's

Hello guys, I'd like to ask for your assistance from you guys on my C++ lab. The code main objective is to interpolate between each pair of numbers(both horizontally and vertically) such that for matrix 4 by 4, the result is the 7 by 7 matrix. So, below is my code so far, but the output given is different than my prof. The 2 examples my prof gave are:

Example 1:

Enter the number of rows and columns in the square data matrix.
The dimensions of your matrix must be greater than 2 by 2.
3 3
Enter the data of row 1.
1 2 3
Enter the data of row 2.
4 5 6
Enter the data of row 3.
4 3 2
This program will interpolate the 3 by 3 matrix you entered and
create a 5 by 5 matrix.
1.0 2.0 2.0 3.0 3.0
2.7 3.0 3.5 4.0 4.3
4.0 4.0 5.0 4.8 6.0
4.0 4.0 4.0 4.0 4.0
4.0 3.7 3.0 3.0 2.0

Example 2:

Enter the number of rows and columns in the square data matrix.
The dimensions of your matrix must be greater than 2 by 2.
4 4
Enter the data of row 1.
78 56 98 12
Enter the data of row 2.
73 59 99 83
Enter the data of row 3.
17 14 63 56
Enter the data of row 4.
77 44 11 24
This program will interpolate the 4 by 4 matrix you entered and
create a 7 by 7 matrix.
78.0 66.8 56.0 77.3 98.0 61.0 12.0
72.5 66.5 64.9 78.0 87.0 73.0 56.0
73.0 59.8 59.0 73.7 99.0 82.6 83.0
43.6 40.8 43.1 58.8 74.0 75.2 71.4
17.0 27.4 14.0 42.2 63.0 58.2 56.0
44.0 38.0 32.2 33.0 36.4 38.5 39.5
77.0 53.0 44.0 29.3 11.0 24.5 24.0

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
101
102
103
104
105
 #include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int i, j, row_counter(1);
    int num_rows, num_cols;    // dimensions of original matrix
    int rows, cols;            // dimensions of interpolated matrix
    
    cout << "Enter the number of rows and columns in the square data matrix. " << endl;
    cout << "The dimensions of your matrix must be greater than 2 by 2." << endl;
    
    cin >> num_rows >> num_cols;
    
    // calculate dimensions of interpolated matrix
    rows = 2*num_rows - 1;
    cols = 2*num_cols - 1;
        
    double grid [rows][cols];   // poor style but we'll use it here

    
    // read original matrix values into appropriate positions in interpolated matrix
    for (i=0; i<rows; i+=2)
    {
        cout << "Enter the data of row " << row_counter << "." << endl;
        for (j=0; j<cols; j+=2)  // increment by 2 to make room for interpolated values
            cin >> grid[i][j];
        row_counter++;
    }
    
    cout << "This program will interpolate the " << num_rows << " by " << num_cols
         << " matrix you entered and create a " << rows << " by " << cols << " matrix." << endl;
    
    // interpolate the points in the centre of a square (step 1)
    for (i=1; i<rows; i+=2)
    {
        for(j=1; j<cols; j+=2)
        {
            grid [i][j] = (grid[i-1][j-1] + grid[i-1][j+1] + grid[i+1][j-1] + grid[i+1][j+1])/4;
        }
    }
    
    // interpolate the points in the centre of a cross  (step 2)
    for(i=1; i<rows; i+=1)
    {
        for (j=2; j<cols - 1; j+=2)
        {
            grid[i][j] = (grid[i][j-1] + grid[i][j+1] + grid[i-1][j] + grid[i+1][j])/4;
        }
        i++;
        
        if(i>=rows-1)
        { 
            break;
        }
        for(j=1;j<cols -1; j+=2)
        {
            grid[i][j] = (grid[i][j-1] + grid[i][j+1] + grid[i-1][j] + grid[i+1][j])/4;
        }
    }
    
    // interpolate the top row (step 3)
    
    for(j=1; j<cols; j+=2)
    {
        grid[0][j] = (grid[0][j-1] + grid[1][j] + grid [0][j+1])/3;
    }
    
    // interpolate the most lefthand column  (step 3)
    for(i=1; i<rows; i+=2)
    {
  
        grid [i][0] = (grid[i-1][0] + grid[i+1][0] + grid [i][j])/3;
    }
    
    // interpolate the most righthand column  (step 3)
    
    for(i=1; i<rows; i+=2)
    {
         j = cols-1;
        grid [i][j] = (grid[i][j-1] + grid[i-1][j] + grid [i+1][j])/3;
    }
    
    // interpolate the bottom row  (step 3)
    
    for(j=1; j<rows; j+=2)
    {
        i = rows-1;
        grid[i][j] = (grid[i-1][j] + grid[i][j-1] + grid[i][j+1])/3;
    }
    
    // Display the interpolated matrix
    cout << fixed << setprecision(1);
    
    for (i=0; i<rows; i++)
    {
        for(j=0; j<cols; j++)
            cout  << grid[i][j]<< "\t";
        
        cout << endl;
    }
    
    return 0;
}

Last edited on
This is the test that I made using the exact input my prof gave.
When I entered input like in Example 1, this is what is displayed:

Enter the number of rows and columns in the square data matrix.
The dimensions of your matrix must be greater than 2 by 2.
3 3
Enter the data of row 1.
1 2 3
Enter the data of row 2.
4 5 6
Enter the data of row 3.
4 3 2
This program will interpolate the 3 by 3 matrix you entered and create a 5 by 5 matrix.
1.0 2.0 2.0 3.0 3.0
3.0 3.0 3.5 4.0 4.3
4.0 4.0 5.0 4.8 6.0
4.0 4.0 4.0 4.0 4.0
4.0 3.7 3.0 3.0 2.0

For example 2,

Enter the number of rows and columns in the square data matrix.
The dimensions of your matrix must be greater than 2 by 2.
4 4
Enter the data of row 1.
78 56 98 12
Enter the data of row 2.
73 59 99 83
Enter the data of row 3.
17 14 63 56
Enter the data of row 4.
77 44 11 24
This program will interpolate the 4 by 4 matrix you entered and create a 7 by 7 matrix.
78.0 66.8 56.0 77.3 98.0 61.0 12.0
74.7 66.5 64.9 78.0 87.0 73.0 56.0
73.0 59.8 59.0 73.7 99.0 82.6 83.0
35.7 40.8 43.1 58.8 74.0 75.2 71.4
17.0 27.4 14.0 42.2 63.0 58.2 56.0
57.0 38.0 32.2 33.0 36.4 38.5 39.5
77.0 53.0 44.0 29.3 11.0 24.5 24.0

I've figured out that the problem is in the first column. The output in the first column somehow different from what my prof gave. Please assist me and point out where did I went wrong. I'd really appreciate any help.
Topic archived. No new replies allowed.