Given a matix NxM. Your task is to form one dimention array "B" that wil hold only positive elements from the given matrix sorte | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Given a matix NxM. Your task is to form one dimention array "B" that wil hold only positive elements from the given matrix sorte

Input First line N and M (1<=N,M<=100). Then NxM table is given(all number are integers) Output First line have to contain total number of positive elements in the matrix. The next line have to contain all positive elements from the matrix. Sample input: 3 3 1 0 1 3 -1 0 0 -1 -1 Sample output: 3 1 1 3 What is wrong? #include <iostream> using namespace std; int main (){ int n ,m ; int count; cin >> n >> m ; int aza[100][100]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin >> aza[i][j]; } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(aza[i][j]>0){ count++; //use the counter to find how many numbers greater than "0" } } } cout << count << endl ; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(aza[i][j]>0){ cout << aza[i][j] << " "; } } } return 0; }

2nd Oct 2020, 3:53 PM
Azat Malgazhdar
Azat Malgazhdar - avatar
1 Answer
+ 6
Azat Malgazhdar Tried your code. It works fine for me. What problems you're facing? The only warning it raised was count variable was not initialise. You can initialise like this int count=0;
2nd Oct 2020, 4:03 PM
Minho
Minho - avatar