Click here to Skip to main content
15,888,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is the code
this is giving some error, plz help

tried to find max non perfect square in an arary

What I have tried:

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

int main()
{
    int mp=-1;
    int n;
    int A[n];
    std::cin>>n;
    int i;
    for(i=0;i<n;i++)
    {
        std::cin>>A[i];
    }
    for(i=0;i<n;i++)
    {
        int d=sqrt(A[i]);
        if(d*d!=A[i])
        {
            if(A[i]>mp)
            {
                mp=A[i];
            }
        }
    }
    std::cout<<mp;
    return 0;
}
Posted
Updated 23-Jan-18 12:16pm
v3
Comments
ZurdoDev 23-Jan-18 15:00pm    
And what is the error?

You don't even need an array for that:
C++
#include <iostream>
#include <cmath>

int main()
{
  int mp=-1;

  int N;

  std::cin >> N;

  for(int n =0; n < N; n++)
  {
    int x;
    std::cin >> x;
    if ( x > mp)
    {
      int d = static_cast<int>(sqrt(x));
      if ( d*d != x )
        mp = x;
    }
  }
  std::cout << "the maximum non-perfect-square number is " << mp << std::endl;
}
 
Share this answer
 
This will not compile as it is. You need to obtain n first and then size the array according to it. Here is one way to allocate an integer array :
C++
...
   int n;
   std::cin >> n;
   int * intArray = new int[n];

   // rest of code goes here

   delete [] intArray;  // memory must be freed after use
}
 
Share this answer
 
Quote:
Why this error is coming terminated by signal 11

When you don't understand why you code fail, the answer is debugger.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900