C++ exercises and solutions: matrix


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. The lower side fills will -1s and the upper side fills with 1s.


01 11 1 
-10 11 1
-1-1 01 1
-1-1 -10 1
-1-1 -1-1 0
 

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;

 


    }


Comments

lor comment

 lor

#include <iostream>
#include <string>

using namespace std;

void mat(int arg[][], int a){
for(int i=0;i<a;i++){
for(int j=0;j<a;j++){
if (i==j) arg[i][j]=0;
else if (i<j) arg[i][j]=1;
else arg[i][j]=-1;
}
cout <<arg[i][j];
}
}
int main(){
int gjatesia;
cout <<"jepni permasate matrices katrore";
cin >>j;
int vek[gjatesia];
mat(vek, gjatesia);
return 0;
}


2017-10-07
lor comment

 lor

#include <iostream>
#include <string>

using namespace std;

void mat(int arg[][], int a){
for(int i=0;i<a;i++){
for(int j=0;j<a;j++){
if (i==j) arg[i][j]=0;
else if (i<j) arg[i][j]=1;
else arg[i][j]=-1;
}
cout <<arg[i][j];
}
}
int main(){
int gjatesia;
cout <<"jepni permasate matrices katrore";
cin >>j;
int vek[gjatesia];
mat(vek, gjatesia);
return 0;
}


2017-10-07
vipin bahar comment

 vipin bahar

#include <iostream>
using namespace std;
int main()
{

int m,i,j,k;
int c=1;
cout<<"enter the value of m"<<endl;
cin>>m;
for(i=0;i<m;i++)
{

for(j=0;j<m;j++)
{

if(i==j)
cout<<"0"<<"\t";
else if (i-j>0)
cout<<"-1"<<"\t";
else
cout<<"1"<<"\t";




if(c%m==0)
cout<<"\n" ;
c++;
}
}

}


2017-07-19
13ramrat@gmail.com comment

 13ramrat@gmail.com

//initial array
int matrix_array[5][5];

//row
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 5; y++)
{
//add 0 to same value [0][0],[1][1] etc
matrix_array[x][x] = 0;

if (matrix_array[x][x] == 0)
{
//add 1 after same value above [0][1],[1][2]
for (int z = x + 1; z < 5; z++)
{
matrix_array[x][z] = 1;
}

//add -1 before same value above
for (int a = x - 1; a >= 0; a--)
{
matrix_array[x][a] = -1;
}
}
cout << matrix_array[x][y] << "\t";
}
//make space
cout << "\n";
}


2017-06-12



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.