If you can help me with an array program ?!

Ok i have an array with p<50. Program should delete every array with "0" in it. For example 10 20 30 40 and put the next one to it, for example it should look like 39 41 42 etc.
It`s not so hard but i cant do it ...
Last edited on
It is difficult to understand what you want. Can you give examples of what your array should look like both before and after?
before ... 7 8 9 10 11 12 ... 19 20 21 22 ...
after ... 7 8 9 11 12 ... 19 21 22 ...
Last edited on
Ah.

Remember your basic math. 32 / 10 = 3 remainder 2. The remainder operator in C++ is %.

So:

1
2
3
4
5
6
7
for (n = 0; n < N; n++)
  {
  if (a[n] % 10) is zero:
    {
    delete element n
    }
  }

Deleting an element from an array is fairly simple, but not the easiest way to do it.

The simplest way to do this would be to have a temporary array. Copy the elements that don't have zeros to the temporary, then copy the temporary back to the first array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Copy the elements that are not zero to the temp array
m = 0;
for (n = 0; n < N; n++)
  {
  if (a[n] % 10) is not zero:
    {
    temp[m] = a[n];
    m++;
    }
  }

// Copy the temp array back to the first array
N = m;
for (n = 0; n < N; n++)
  {
  a[n] = temp[m];
  }

Hope this helps.
i have to cout << temp[n] ?! Can you make me a full code ? Thanks !

#include <iostream>
using namespace std;
 
#include <iomanip>
using std::setw;
 
int main ()
{
   int n[50];
m = 0;
for (n = 0; n < N; n++)
  {
  if (a[n] % 10) is not zero:
    {
    temp[m] = a[n];
    m++;
    }
  }
N = m;
for (n = 0; n < N; n++)
  {
  a[n] = temp[m];
  cout << setw( 7 )<< n << setw( 15 ) << temp[ m ] << endl;
  }
 
   return 0;
}
Last edited on
Topic archived. No new replies allowed.