How to remove zeros from an array

I am new and I would be much obliged if someone gives a hand.

I have an array looks like this :

{1,0,0,2,0,3,0,0,4};

I want to take all the zeros out so that the array will look like this:

{1,2,3,4};

Thanks a lot
Make it a vector if resizing is required.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    // vector: https://cal-linux.com/tutorials/vectors.html
    std::vector<int> array {1,0,0,2,0,3,0,0,4};

    // print it out
    // range base loop: http://www.stroustrup.com/C++11FAQ.html#for
    for( int v : array ) std::cout << v << ' ' ;
    std::cout << '\n' ;

    // remove all zeroes from the vector
    // iterators and algorithms: https://cal-linux.com/tutorials/STL.html
    // erase=remove idiom: https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom
    array.erase( std::remove( array.begin(), array.end(), 0 ), array.end() ) ;

    // print out the modified vector
    for( int v : array ) std::cout << v << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/79f1409c3c0c0ddb

//I just found one:

include<iostream>
using namespace std;
int main()
{
int list[9] = {1,0,0,2,0,3,0,0,4};
int counter2nd = 0;
for( int I = 0; I < 9; I++)
if(list[I] == 0)
{
}
for(int l = 0; l < 9; l++)
{
if(list[l] != 0)

cout << list[l] << " ";
}
cout << endl;
return 0;
}
This is how your code looks in "code tags" (and fixed up a little).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
    int list[9] = {1,0,0,2,0,3,0,0,4};

    for (int i = 0; i < 9; i++)
    { 
        if (list[i] != 0)
            cout << list[i] << ' ';
    } 
    cout << endl;

    return 0;
}


To put your code in code tags, simply paste it between [code] and [/code], like this:

[code]
paste your code here
[/code]

Your program does not actually "remove the zeros" from the array.
It just prints the array without the zeros.
That's a different thing, and as you've discovered, is much easier to do.
Last edited on
One can use array, but then one has to keep track of how many elements of that array are actually "in use":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <algorithm>

int main()
{
    constexpr int Capacity {9} ;
    int array[Capacity] {1,0,0,2,0,3,0,0,4} ;

    // print it out
    for( int v : array ) std::cout << v << ' ' ;
    std::cout << '\n' ;

    // copy non-zero elements to front of array
    auto end = std::remove( array, array + Capacity, 0 ) ;
    int size = end - array; // count of remaining elements

    // print out the first size elements
    for( int i=0; i < size; ++i ) std::cout << array[i] << ' ' ;
    std::cout << '\n' ;
    
    // print out the whole array
    for( int v : array ) std::cout << v << ' ' ;
    std::cout << '\n' ;
}

The std::remove in above does effectively do
1
2
3
4
5
array[0] = array[0];
array[1] = array[3];
array[2] = array[5];
array[3] = array[8];
return array + 4;


A real question is, are you happy and able to use std::remove, or do you have to understand how std::remove does its magic?
Thanks "dutch"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

//I just found one:

 include<iostream>
 using namespace std;
 int main()
 {
 int list[9] = {1,0,0,2,0,3,0,0,4};
 int counter2nd = 0; 
 for( int I = 0; I < 9; I++)
 if(list[I] == 0)
 {
 } 
 for(int l = 0; l < 9; l++)
 { 
 if(list[l] != 0)

 cout << list[l] << " "; 
 } 
 cout << endl;
 return 0;
 }

 

Last edited on
Keep in mind allenmaki that the code you just presented here does not remove the unwanted 0's from the array. You are just skipping over them with that cout bit. To really remove the 0's you will need to go with one of the other suggestions here.
Hi keskiverto,
thanks for trying to help. Below are error messages when I tried to compile


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1
1>Compiling...
1>TEST 002.cpp
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(6) : error C2601: 'Capacity' : local function definitions are illegal
1>        c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(5): this line contains a '{' which has not yet been matched
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(6) : error C2143: syntax error : missing ';' before '}'
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(7) : error C2065: 'Capacity' : undeclared identifier
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(7) : error C2470: 'array' : looks like a function definition, but there is no parameter list; skipping apparent body
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(10) : error C2143: syntax error : missing ',' before ':'
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(14) : error C2390: 'end' : incorrect storage class 'auto'
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(14) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(14) : error C2065: 'array' : undeclared identifier
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(14) : error C2065: 'array' : undeclared identifier
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(14) : error C2065: 'Capacity' : undeclared identifier
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(15) : error C2065: 'array' : undeclared identifier
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(18) : error C2143: syntax error : missing ')' before ';'
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(18) : error C2143: syntax error : missing ';' before ')'
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(18) : error C2143: syntax error : missing ';' before ')'
1>c:\users\allen\documents\visual studio 2008\projects\test 002\test 002\test 002.cpp(18) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://c:\Users\Allen\Documents\Visual Studio 2008\Projects\TEST 002\TEST 002\Debug\BuildLog.htm"
1>TEST 002 - 15 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Visual Studio 2008 won't support C++11 syntax.
Microsoft has free versions of Visual Studio ("Visual Studio Community" I believe it's called), so I would suggest downloading that. But if you are truly stuck with 2008:

Change constexpr int Capacity {9} ; to const int Capacity = 9;

Change for( int v : array ) std::cout << v << ' ' ;
to
1
2
3
4
for (int i = 0; i < 9; i++)
{
    std::cout << array[i] << ' ';
}
Last edited on
//I tried this code, but it does not work. I do not know why? I would appreciate it, if someone tells me.

The old area is 1,2,0,0,0,3,4,5,0
The new area should be 1,2,3,4,5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

 #include<iostream>

 using namespace std;

 int main()
 {
 int list[9] = {1, 2, 0, 0, 0, 3, 4, 5, 0};

 int rmv0sArr[] = {0,0,0,0,0,0,0,0,0}; 

 int counter2nd = 0; 

 for( int I = 0; I < 9; I++)
 {
 if(list[I] != 0)
 { 
 rmv0sArr[I] = list[I] ; 

 counter2nd++; 
 } 
 } 

 cout << "The length is " << counter2nd << endl;

 for (int Q = 0; Q < counter2nd; Q++)

 {
 cout << rmv0sArr[Q] << " ";
 } 

 return 0;
 }
Last edited on
Your current output:
The length is 5
1 2 0 0 0  

Notice that it is calculating the new length correctly: 5. But the first 5 elements (1 2 0 0 0) are still the first 5 elements of your list array.

The issue is line 18: You're just assigning list[i] to removedArr[i], so there's still going to be 0s in the places where you don't assign anything.

I would instead use your counter2nd variable as the index: assign to rmv0sArr[counter2nd], not rmv0sArr[i].
Last edited on
To Ganado, It works fine. Thanks a lot. I, promise, will find an alternative variable name for " rmv0sArr". Maybe "noZerosArray"
Topic archived. No new replies allowed.