-3

I'm trying to make a c++ program that finds the maximum non perfect square in an array and print it, perfect square i.e. x = y^2 => 4 = 2^2.

Here is what I've tried and doesn't work for me, don't know why:

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

int main()
{
    int n;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++)
    {

        cin >> arr[i];
        sqrt(arr[i]);

        if ((arr[i] * 10) % 10 == 0)
            arr[i] = arr[1];
        else
            arr[i] = arr[0];
    }
    for (int i = 0; i < n; i++)
    {
        if (arr[0] < arr[i])
            arr[0] = arr[i];
    }
    cout << arr[0] << endl;
    return 0;
}

My logic is to take the square root of each array element and check if it's non-perfect or perfect. If we multiply the element by 10, then take modulus of 10, then we know whether it is an integer or decimal. For example: 2*10 = 20, 20%10 = 0 (perfect square), otherwise it is not perfect. Then, I stored each non-perfect square in arr[0], in the next loop I'm supposed to find the largest non perfect square and print it. What am I doing wrong?

PS:
Consider arr[variable] is valid, because it works in CodeBlocks. Thank you!

Azeem
  • 11,148
  • 4
  • 27
  • 40
Adam Hussein
  • 75
  • 5
  • 12

3 Answers3

1

You lost the result of sqrt. sqrt(arr[i]) does not change arr[i]).

You improperly check if a square root is an integral. You should cast a result of sqrt to int, multiply it by itself and compare with arr[i].

I left you free to update your code properly yourself.

273K
  • 29,503
  • 10
  • 41
  • 64
1
#include <iostream>
#include <cmath>
using namespace std;
int main () {
  int n;
  cin>>n;
  int k[n];
  double arr[n];
  for (int i = 0 ; i < n ; i++){
    cin>>k[i];
    arr[i]=sqrt(k[i]);
    int j = arr[i];
    if (arr[i]==j){
      arr[i]=0;
      }
    }
  double m=0;
  int index = 0;
  for (int i = 0; i < n; i++){
    if (arr[i]>m){
      m=arr[i];
      index = i;
    }
  }
  cout << k[index];
}

Here is a code. We introduce a double, such that it can store the decimals. Then we introduce an integer. If the square root of the number is a decimal, it is not a perfect square. However, when I introduce this integer j, it will convert arr[i] to an integer. If the number is a perfect square, then arr[i] is an integer, and j==arr[i]. We do not want that, so we put that equal 0. We find the largest array, and mark the index. Then we print out the original number in the original array with that index. i have added this as float does not store every single decimal point.

To clarify: lets say arr[i]=4.55556. Then j=4. arr[i]!=j. If arr[i]=5, j=5, arr[i]=j, and then arr[i] is set to 0.

QuIcKmAtHs
  • 297
  • 4
  • 18
1

You can use this logic to find if a number is perfect-square or not, this is one way to find largest non perfect square of an array of positive numbers, initialize answer=-1 before you enter the loop, n is the size of the array

double answer = -1,temp;
for(int i=0;i<n;i++){                                       
    if((temp = array[i]) != (sqrt(array[i])*sqrt(array[i]))){
        if(temp > answer){
            answer = temp;
        }
    }
}
  • 1
    Probably going to [need some epsilon](https://en.wikipedia.org/wiki/Machine_epsilon) in here `(temp = array[i]) != (sqrt(array[i])*sqrt(array[i]))` because equals with doubles is iffy due to imprecision. Side note: 2 `sqrt`s is pretty expensive. Might be worth `sqrt`ing once and caching the result. – user4581301 Jan 21 '18 at 04:45
  • Improper solution. – 273K Jan 21 '18 at 04:52