0

I input p and n (int type) numbers from my keyboard, I want to generate the first p*n square numbers into the array pp[99]. Here's my code:

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
int i, j, n, p, pp[19];

cout<<"n="; cin>>n;
cout<<"p="; cin>>p;

i=n*p;
j=-1;
while(i!=0)
{
    if(sqrt(i)==(float)sqrt(i))
    {
        j++;
        pp[j]=i;
    }
    i--;
}

for(i=0; i<n*p; i++)
    cout<<pp[i]<<" ";

return 0;
}

But I am encountering the following problem: If I for example I enter p=3 and n=3, it will only show me the first 3 square numbers instead of 9, the rest 6 being zeros. Now I know why this happens, just not sure how to fix it (it's checking the first n * p natural numbers and seeing which are squares, not the first n*p squares).

If I take the i-- and add it in the if{ } statement then the algorithm will never end, once it reaches a non-square number (which will be instant unless the first one it checks is a perfect square) the algorithm will stop succeeding in iteration and will be blocked checking the same number an infinite amount of times.

Any way to fix this?

Ahtisham
  • 9,170
  • 4
  • 43
  • 57
Lastrevio
  • 41
  • 1
  • 7
  • 3
    why not just set `pp[i] = i*i`? That'd give you all squares of integers from `0` to `n*p` (which are the first `n*p` squares). – Zinki Jan 29 '19 at 15:09
  • 2
    What is your expectation for `if(sqrt(i)==(float)sqrt(i))`? – François Andrieux Jan 29 '19 at 15:10
  • 1
    Last loop: you got `j` square numbers, not `n*p` – Damien Jan 29 '19 at 15:13
  • @Zinki Yes, that's what I wanted, thanks!! Should've posted that as an actual answer so I could approve it. – Lastrevio Jan 29 '19 at 15:26
  • @FrançoisAndrieux if that condition is true then that number is a perfect square. For example in the case of 8, sqrt(8) is 2 while (float)sqrt(8) is 2.82... so that number is not a square. In the case of 9 for example, both are 3, so it's a square. – Lastrevio Jan 29 '19 at 15:26
  • @Damien not possible unfortunately :/ as i need to get the first n*p numbers, even if that gets me rid of the zeros it still will output me less numbers than I told the computer I wanted to. – Lastrevio Jan 29 '19 at 15:26
  • @Lastrevio `sqrt(i)` doesn't care what you cast the result to. `sqrt(8)` and `(float)sqrt(8)` both produce `2.82`. The difference is with `(float)` you drop a few decimals of precision. In c++, what you do with the result does not change how that result is calculated and return type doesn't participate in overload resolution. See [`std::sqrt`](https://en.cppreference.com/w/cpp/numeric/math/sqrt) how `sqrt` returns `double` for all `IntergralType` arguments. Maybe you meant `sqrt(i) == (int)sqrt(i)`? Even then, you should use a proper rounding function. Floating point precision is imperfect. – François Andrieux Jan 29 '19 at 15:28

1 Answers1

0

Instead of searching for them, generate them.

int square(int x)
{
    return x * x;
}

int main()
{
    int n = 0;
    int p = 0;
    std::cin >> n >> p;
    int limit = n * p;
    int squares[99] = {};
    for (int i = 0; i < limit; i++)
    {
        squares[i] = square(i+1);
    }
    for (int i = 0; i < limit; i++)
    {
        std::cout << squares[i] << ' ';
    }
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82