﻿ C++ exercises and solutions: matrix

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.

 0 1 1 1 1 -1 0 1 1 1 -1 -1 0 1 1 -1 -1 -1 0 1 -1 -1 -1 -1 0

Solution:

#include<iostream>

#include<conio.h>

using namespace std;

int main()
{

int matrix;
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;

} lor#include #include using namespace std; void mat(int arg[][], int a){ for(int i=0;i>j; int vek[gjatesia]; mat(vek, gjatesia); return 0; }2017-10-07 lor#include #include using namespace std; void mat(int arg[][], int a){ for(int i=0;i>j; int vek[gjatesia]; mat(vek, gjatesia); return 0; }2017-10-07 vipin bahar#include using namespace std; int main() { int m,i,j,k; int c=1; cout<<"enter the value of m"<>m; for(i=0;i0) cout<<"-1"<<"\t"; else cout<<"1"<<"\t"; if(c%m==0) cout<<"\n" ; c++; } } }2017-07-19 13ramrat@gmail.com //initial array int matrix_array; //row for (int x = 0; x < 5; x++) { for (int y = 0; y < 5; y++) { //add 0 to same value , etc matrix_array[x][x] = 0; if (matrix_array[x][x] == 0) { //add 1 after same value above , 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