Help please?!

closed account (2UX23TCk)
-
Last edited on
@codecodecode

When you're couting the contents of the array, check what it is before you print the number. Use if/else

ie. If (array[x][y] > 0)
cout << array[x][y]<< " ";//Put a space between the numbers
else
cout << " "; // Output 2 spaces

At the end of y loop, output a newline if you're wanting a 4x4 output.
If not, forget the newline
Last edited on
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
#include <iostream>
#include <iomanip>

int main()
{
    const int n = 4 ;
    int array[4][4] {};
    const char space = ' ' ;

    // set some values to integers, others to spaces
    for( int row = 0 ; row < n ; ++row )
    {
        for( int col = 0 ; col < n ; ++col )
        {
            if( row != col ) array[row][col] = row*n + col ; // integer value
            else array[row][col] = space ; // space (implicit conversion from char to int)
        }
    }
    
    // range based for loop http://www.stroustrup.com/C++11FAQ.html#for
    for( const auto& row : array )
    {
        for( int v : row )
        {
            std::cout << std::setw(3) ;
            if( v == space ) std::cout << space ; // if it is a space print a space
            else std::cout << v ; // otherwise, print the integer value
        }
        std::cout << '\n' ;
    }
}
Topic archived. No new replies allowed.