Sunday, January 24, 2016

C++ arrays: matrix

In this  C++ exercise , your are about to write C++ program to display a matrix as shown below. The diagonal of the matrix fills with 0. Th... thumbnail 1 summary
In this C++ exercise, your are about to write C++ program to display a matrix as shown below. The diagonal of the matrix fills with 0. The lower side fills will -1s and the upper side fills with 1s.
01111 
-10111
-1-1011
-1-1-101
-1-1-1-10
 Solution:
#include<iostream>
#include<conio.h>
using namespace std;
   int main() 
    {
     int matrix[5][5]; 
     int i,j; 
     for(i=0;i<5;i++) //assign values to the matrix
        for(j=0;j<5;j++){
           if(i==j) matrix[i][j]=0;//if row=column=> fill the matrix with 0
           else if(i>j) matrix[i][j]=-1;//if row>columns=> fill matrix with -1
           else matrix[i][j]=1;//if row<columns=> fill matrix with 1
             }    
         for(i=0;i<5;i++){ //print the array
        for(j=0;j<5;j++)
            cout<<matrix[i][j]<<"\t";
        cout<<"\n";  
       }
     getch();
     return 0;
     } 

No comments

Post a Comment