Answer the question
In order to leave comments, you need to log in
How to remove negative numbers from an array?
Write a C++ program to create a dynamic array A[N], fill the array using a random number generator (get a random number in the interval [a,b] as a function (a=-30,b=30)). Sort the resulting array in decreasing order and write only positive numbers to the new array B[M], and remove the old array A from memory.
Arrange sorting of an array as a function.
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, i, j;
cin » n;
float a [n];
for (i = 1; i <= n; i ++) {
a[i]=-30+rand()%60;
cout«a[i]«"\t";
}
for (i = 1; i < n; i ++) {
for (j = i + 1; j <= n; j++) {
if (a[i]>0 && a [i] < a [j] ) { a [0] = a [i]; a [i] = a [j]; a [j] = a [0];}
}
}
cout«endl;
for (i = 1; i <= n; i++)
cout « a [i] « " ";
return 0;
}
Answer the question
In order to leave comments, you need to log in
How to remove negative numbers from an array?there are two options, you can create a separate array, which will add numbers greater than -1 and then sort it, or remove negative numbers from the first array and reduce its length by the number of removed elements and then sort it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question