简体   繁体   中英

How to extract zeros from one array and save non-zero part to another array?

I am working on small program about prime numbers. For example I have an arrays of prime numbers:

array[15]={0,0,0,13,0,17,0,31,7,3,0,0,0,37,0};

I need to extract zeros the from array then I need to save remaining numbers to another array(which is tmp for this case).

I generated following function for this operation.

For following code piece:

counter is number of primes in array(which is 6 for this case).

dB is size of array(which is 15 for this case).

void function2(int array[],int counter){

     int tmp[counter];

     //takes arrayB with zeros in it, extract zeros prints new array.
        for(int a=0;a<counter;a++){

            for(int i=0;i<dB;i++){

                if(array[i]!=0 ){

                    tmp[a]=array[i];
                    break;
                }   
            }
        }
    cout<<"Prime numbers array extracted from above:\n";
    for(int b=0;b<counter;b++){
        cout<<tmp[b]<<" ";
    }   
}

When I execute this code it just prints first prime number of array(13) for counter times(6).

13,13,13,13,13,13

However, I need following output.

13,17,31,7,3,27

I think my algorithm is wrong how can I fix it any thoughts?

Thank You!

When I change if statement like this:

if(array[i]!=0 && array[a-1]!=array[i])

I am getting following output

13,17,13,17,13,17

Do I need to do change for all of 6 elements?

Approach is simple , just iterate the original array which contains 0 and prime numbers and once you get a non-zero number add it to the tmp array.

Following is your code refactored to perform the above approach :

void function2(int array[],int counter){    
     int tmp[counter],a=0;
     for(int i=0;i<dB;i++){
                if(array[i]!=0 ){
                    tmp[a++]=array[i];
                }
            }
    cout<<"Prime numbers array extracted from above:\n";
    for(int b=0;b<counter;b++){
        cout<<tmp[b]<<" ";
    }   
}

Here is a solution: It loops through the array, if the element is not 0 it is inserted into a vector .

#include <iostream>
#include <vector>
using namespace std;

int foo[15]={0,0,0,13,0,17,0,31,7,3,0,0,0,37,0};

void GetPrimesFromArray(int inputArray[], int array_size) {

    vector<int> tmp;

    for(int i = 0; i < array_size; ++i)
        if(inputArray[i]!=0)
            tmp.push_back(inputArray[i]);

    for(int i = 0; i < tmp.size(); ++i)
        cout << tmp[i] << endl;

}

int main() {
    GetPrimesFromArray(foo, 15);
}

Outputs:

13
17
31
7
3
37

Firstly, make only one loop and iterate over all your array 'array'. When a value is non-zero, add it to your result and increment a counter. When your counter is equal to dB, use the break instruction to get out of your loop.

Secondly, everytime you try to add a new prime number, you starts from the beginning of your array whitout erasing the previous prime number you've found so you will continuously add the first prime number available in your array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM