Program error

Hello. So this program should read N and then N numbers greater than 9 from a file, then if the first two digits of a number are not a perfect square it should delete that number. It kinda does that but it has an error. For example if in the "elempp.in" file I have these numbers:(n = 5) 253 16 225 100 3678, then in the "elempp.out" file, there should be these numbers: 253 16 3678, but in my case, there is an extra 100, so the numbers are: 253 16 100 3678.


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
34
35
36
 #include <fstream>
#include <cmath>
using namespace std;
int v[21], i, n, j, roz;
bool pp(int x)
{

   if(sqrt(x) == int(sqrt(x))) return true;
   return false;
}
int main()
{
  ifstream f("elempp.in");
    ofstream g("elempp.out");
    f >> n;
    for(i = 1; i <= n; i++)
        f >> v[i];
    for(i = 1; i <= n; i++)
    {
        roz = v[i];
        if(roz >= 100)
            {
                while(roz >= 100)
                roz /= 10;
            }
                if(!pp(roz))
                {
                    for(j = i; j < n; j++)
                        v[j] = v[j + 1];
                        n--;
                }
    }
    for(i = 1; i <= n; i++)
        g << v[i] << " ";

}

I would be very glad if someone would help me.
Last edited on
I checked pp(10) and that worked out.

I don't see the issue print all the values that are sent into PP to see if that is an issue.
I cannot reproduce the error with:
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 <sstream>
#include <iostream>
#include <cmath>

bool pp(int x)
{
  return sqrt(x) == static_cast<int>( sqrt(x) );
}

int main()
{
    // test
    for ( int i = 1; i < 100; ++i )
    {
        if ( pp(i) ) std::cout << i << " ";
    }
    std::cout << '\n';

    std::string data {"5 253 16 225 100 3678"};
    std::istringstream f {data};
    int n {0};
    f >> n;
    for ( int i = 0; i < n; ++i )
    {
        int v {0};
        if ( f >> v )
        {
            while ( 100 <= v ) v /= 10;
            if ( pp(v) ) std::cout << v << " ";
        }
    }
    return 0;
}

Perhaps floating point math on your platform is more delicate (or I did too many changes)?
@FraNNkie,
You delete the value in the ith position (lines 28-30). What was in the (i+1)th position gets moved to the ith position ... and hence is never checked because i is then incremented.

It would be safer either to write a sentinel into v[i] (e.g. -1) or have a separate array for your legitimate outputs.


You had better hope that N never exceeds 20.


@Keskiverto's code avoids both problems.
Last edited on
Thanks to both of you I got it to work (@lastchance and @keskiverto). The error in my code was caused by me forgetting to add an instruction after line 30 (i--;)
Code:
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
34
35
36
37
#include <fstream>
#include <cmath>
using namespace std;
int v[21], i, n, j, roz;
bool pp(int x)
{

   if(sqrt(x) == int(sqrt(x))) return true;
   return false;
}
int main()
{
  ifstream f("elempp.in");
    ofstream g("elempp.out");
    f >> n;
    for(i = 1; i <= n; i++)
        f >> v[i];
    for(i = 1; i <= n; i++)
    {
        roz = v[i];
        if(roz >= 100)
            {
                while(roz >= 100)
                roz /= 10;
            }
                if(!pp(roz))
                {
                    for(j = i; j < n; j++)
                        v[j] = v[j + 1];
                        n--;
                        i--;
                }
    }
    for(i = 1; i <= n; i++)
        g << v[i] << " ";

}
Topic archived. No new replies allowed.