0

I have been trying for some time now, but i can't figure it out. I have to make a program where in a matrix, it finds the top, bottom, left and right numbers and prints them out. I made it where it prints the bottom, left and right numbers but can't figure out how to print the top one.

#include <iostream>

using namespace std;

int main()
{
    int a[10][10],i,j,m,n,zb,zb2,zb3,zb4;
    cin>>m>>n;
    
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            cin>>a[i][j];
        }    
    }
    
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(i+j<m-1){
                zb=a[i][j]; // first
            }
            if(i+j<m+1){
                zb2=a[i][j]; // second
            }
            if(i<j){
                zb3=a[i][j]; // third
            }        
            if(n+m<j+1){
                zb4=a[i][j]; // fourth
            }  
        }    
    }
   
    

    cout<<zb<<endl;
    cout<<zb2<<endl;
    cout<<zb3<<endl;
    cout<<zb4<<endl;

    return 0;
}

A Graph of how the program works Example of program

Thank you in advance!

Leaksy
  • 13
  • 2
  • Hint: Use `#define` or constants for your array capacities. You can then use them elsewhere. When changing array capacity, you'll only need to modify one location. Example: `const unsigned int MAX_ROWS = 10U;`. – Thomas Matthews Jun 10 '21 at 23:06
  • So you want to print the four **corners**, or the four **sides**? From the code it's really unclear what you wanted to do. – silverfox Jun 10 '21 at 23:14
  • 2
    A good [mre] would not rely on user input like this. Set `a` to values that demonstrate the error. Perhaps simplify to just the "top" and "bottom" calculations (one that works and one that does not). Tell us what the expected and actual outputs are. Bonus: tell us why you think your program should have worked. – JaMiT Jun 10 '21 at 23:14
  • @silverfox i want to print the **middle part** of each **side**, like shown in the example. The ones with red around them! – Leaksy Jun 10 '21 at 23:17
  • So print **all elements** in four sides, but **ignore** the corners? Or just the only **middle element** of each side? Then what happens if user input a matrix with even sides? – silverfox Jun 10 '21 at 23:17
  • 1
    What do you want printed for matrices that have even number of columns or even number of rows? – Thomas Matthews Jun 10 '21 at 23:20
  • If I have a 2d matrix, 5 x 5, do you want the middle (single) element of each side or more than one? – Thomas Matthews Jun 10 '21 at 23:21
  • Yep, you can run my program and see how it works. Just comment out the last zb4. Ex. 1 2 3 4 5 6 7 8 9 It will print out 4,8,6. But i can't figure out to print the 2! – Leaksy Jun 10 '21 at 23:22
  • `int a[10][10],i,j,m,n,zb,zb2,zb3,zb4;` Don't be THAT guy. Make descriptive identifiers. – user4581301 Jun 10 '21 at 23:23
  • But what happens when even-sided matrix are encountered? For example, what will you print with 4x4 matrix with numbers from 1->16? Please include more example than your 3x3 matrix, else there're not enough information for us to help. – silverfox Jun 10 '21 at 23:23
  • @user4581301 new to c++, so i just do what im told by teachers. – Leaksy Jun 10 '21 at 23:24
  • @silverfox, sorry for bad example.. Quite new to the whole c++ language and matrix. Here is an [link](http://i.prntscr.com/vlikitfBS_eib4SdS9BRfg.png) example image. I just need a simple program that selects all the numbers that are selected with the red paint and then print them out. It needs to ignore the middle ones and the corners. – Leaksy Jun 10 '21 at 23:31
  • If teachers ask for particular variable names, give them what they want, but if you use names with meaning you'll find that the code describes itself. This reduces the need for comments and often helps a lot in debugging. Plus people mix up `i` and `j` all the time. There are a couple "why doesn't my loop exit?" questions every day resulting from the code testing `i` and incrementing `j`. – user4581301 Jun 10 '21 at 23:45
  • *"like shown in the example"* -- examples make poor documentation of intent, as there are often multiple ways to interpret the results. You should write out (as text) what your program is supposed to accomplish. Try to be detailed, leaving no room for ambiguity, as if your audience cannot make intelligent guesses. – JaMiT Jun 11 '21 at 02:51
  • *"you can run my program and see how it works"* -- well, no, we cannot as the whole premise of this question is that your program does not work as intended. – JaMiT Jun 11 '21 at 02:52

2 Answers2

0

Hmmm, the midpoint is defined as:
length >> 1 or (length + 1) / 2

The index of the right most column is (column quantity) - 1.
The index of the bottom most row is (row quantity) - 1.

So, the locations are:

const unsigned int mid_column = MAXIMUM_COLUMNS / 2;
const unsigned int mid_row    = MAXIMUM_ROWS / 2;
std::cout << matrix[0][mid_column] << "\n"
          << matrix[mid_row][MAXIMUM_COLUMNS - 1] << "\n"
          << matrix[MAXIMUM_ROWS - 1][mid_column] << "\n"
          << matrix[mid_row][0] << "\n";
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

The for-loop is extremely unnecessary. If you already now the numbers of column and row, the midpoint can easily be calculated:

#include <iostream>

const unsigned int maxn = 10;
int a[maxn][maxn];

int main()
{
    int row,col;
    std::cin>>row>>col;

    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++) { std::cin>>a[i][j]; }
    }

    int midcol = col/2;
    int midrow = row/2;

    std::cout << "Top row : " << a[0][midcol];
    if (col%2==0) { std::cout << " " << a[0][midcol-1]; }
    std::cout << "\n";

    std::cout << "Bottom row : " << a[row-1][midcol];
    if (col%2==0) { std::cout << " " << a[row-1][midcol-1]; }
    std::cout << "\n";

    std::cout << "Left column: " << a[midrow][0];
    if (row%2==0) { std::cout << " " << a[midrow-1][0]; }
    std::cout << "\n";

    std::cout << "Right column: " << a[midrow][col-1];
    if (row%2==0) { std::cout << " " << a[midrow-1][col-1]; }
    std::cout << "\n";

    return 0;
}

Example :

3 3
1 2 3
4 5 6
7 8 9
Top row : 2
Bottom row : 8
Left column: 4
Right column: 6

Example (2):

4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Top row : 3 2
Bottom row : 15 14
Left column: 9 5
Right column: 12 8

Example (3):

3 4
1 2 3 4
5 6 7 8
9 10 11 12
Top row : 3 2
Bottom row : 11 10
Left column: 5
Right column: 8

It should also be noticed that:

  • As @ThomasMatthews mentioned, you can use #define or const variable to declare your array capacities. That way, when you change the size of the matrix, you'll only have to change 1 variable.

  • Don't declare a bunch of variables that is one-character or have no meaning like i,j,m,n,zb,zb2,zb3,zb4. A few is OK, but having more of those will cause a lot of debugging problems when your code gets longer. Also, while using i,j,k as iterator variables in loops are somewhat common within programmers (I do it frequently myself), as a beginner, they can cause a lot of confusion for newbies, for example :

for (int i = 0; i < n; i++)
{
    for (int j = 0; j < m; i++) //mistake here
    {

    }
}
silverfox
  • 1,568
  • 10
  • 27