Finding Positive and Negative Values in a Matrix in C++
using namespace std; #include< stdio.h> #include< stdlib.h> #include < iostream> void display(int **Mat, int N, int M){ for(int i = 0; i < N; i++) { for(int j = 0; j < M; j++) { Cost < < Mat[i][j]; Cost < < "\t"; } //display the following line Cost < < "\n"; } Cost < < "\n"; } int main(int argc, char *argv[]) { int n=3; int** M = new int* [ n ]; for (int i=0; i < n; i++) M[i] = new int[ n ]; srand(time(NULL)); for(int i=0; i< n; i++) for(int j=0; j< n; j++) //Generate random numbers between 0 and 200 after subtracting 100 e.g. if rand generates 74 then 74-100 = -36 M[i][j] =rand()%200-100; display(M,n,n); int negative = 0; int positive=0; //browse matrix for(int i=0; i< n; i++) for(int j=0; j< n; j++) { if(M[i][j]< 0) negative++; else positive++; } Cost < < "\n The number of negative values is "< < negative; Cost < < "\n The number of positive values is "< < positive< < "\n"; system("pause"); }
Advertisement
AdBlock Detected
Please disable your ad blocker and refresh the window to use this website.