2-D arrays

sAksham-Ar published on
1 min, 131 words

Categories: C++

Double dimension arrays store variable in a rows*columns size matrix. We input them using nested for loops which basically means one for loop inside another. Here i make use of logical AND operator(&&) which means that if and only if both the statements are true , the code inside the if is executed. The first square bracket specifies the rows, the second specifies the columns.

#include<iostream>
using namespace std; 
int main()
{
    int i, j; 
    char A[5][9]; 
    for(i=0; i<5; i++)
    {
        for(j=0; j<9; j++)
        {
            if((j>=i)&&(j<9-i))
            {
                A[i][j]='*';      
            }   
            else
            {
                A[i][j]=' ';
            }
        }
    }
    for(i=0; i<5; i++)
    {
        for(j=0;j<9;j++)
        {
            cout<<A[i][j];  
        }
        cout<<"\n";
    }
    return 0; 
}

OUTPUT:

output