-2

So I wrote a program that is supposed select the perfect squares from an array and put it into another array. Example: (2,4,13,5,25,66) and the second array(the result) should look like this (4,25) My result looks like this (0,4,0,0,25,0) ...so its half good ...how to make it show only 4,25 ?

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

int main()
{
    int A[100],i,n,p,j;

    cout << "Number of array elements=";
    cin >> n;

    for(i=1;i<=n;i++)
    {
        cout<<"A["<<i<<"]=";
        cin>>A[i];
    }

    for(i=1;i<=n;i++)
    {
        p=sqrt(A[i]) ;   
        if(p*p==A[i])
            A[j]=A[i];
        else
            A[i]=0;

        cout << A[i] << " ";
    }

    return 0;
}

USING ONLY c++ basic commands...as i did

Alex
  • 9,891
  • 11
  • 53
  • 87
cUser26
  • 77
  • 1
  • 5
  • 2
    What do you think `j` will do? And is there some reason "another array" is the same array? – Beta Dec 09 '12 at 12:29

5 Answers5

3

You need to keep a separate count of how many perfect squares you've found and use that to place your answers into an array of perfect squares:

int squares[???];
// ...
if(p*p==A[i]) {
  squares[squaresFound]=A[i];
  squaresFound++;
}

The problem now will be to decide how long the squares array should be. You don't know ahead of time how many squares you're going to get. Are you going to have it the same size as A and fill the rest with 0s? Or do you want the array of squares to be exactly the right size?

If you want it to be the right size, you're much better off using a std::vector:

std::vector<int> squares;
// ...
if(p*p==A[i]) {
  squares.push_back(A[i]);
}

But I think your silly "only basic C++ commands" restriction will not allow you to do this.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
2

You talk about a second array (the result), yet your code declares only one array! Additionally, you reference A[j], but your j has not be initialized.

You should declare another array B[100], initialize j to zero, and then use this code when you find a square:

int j = 0;
for (int i=0 ; i != n ; i++) {
    int p = sqrt(A[i]);
    if(p*p==A[i]) {
        B[j++] = A[i];
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    It is technically possible to use `A` as the result array as long as `j` is actually used. – Emil Vikström Dec 09 '12 at 12:32
  • 1
    @EmilVikström Absolutely! However, this is not the way the question is phrased. I strongly suspect that this is either a school assignment or a self-study where the second array is required. – Sergey Kalinichenko Dec 09 '12 at 12:35
0

Make another array, remove all occurrences of 0 from the resultArray and add non-0 to newArray.

OR

int j=0
if(A[i]==p*p) 
  squares[j++]=A[i];
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0
#include <cmath>
#include <iostream>
using namespace std;


int main()
{
    int A[100];
    int n;

    cout << "Number of array elements = " << endl;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        cout << "A[" << i << "] = ";
        cin >> A[i];
    }

    int B[100];
    int cnt_sqr = 0;
    for(int i = 0; i < n; i++)
    {
        int p = sqrt(A[i]);
        if (p * p == A[i])
        {
            B[cnt_sqr++] = A[i];
        }
    }

    for (int i = 0; i < cnt_sqr; i++)
    {
        cout << B[i] << ' ';
    }
    return 0;
}

Full code of that about what you were told above

Dima00782
  • 171
  • 1
  • 3
  • 13
0

If you do not want to modify your code you can write the following:

for(i=1;i<=n;i++)
{
    p=sqrt(A[i]) ;   
    if(p*p==A[i])
    {
        cout << A[i] << " ";
    }
}

It will print you only perfect squares.

If you want to copy elements to another array:

int squares[100] = {0}; // Assuming that all values can be perfect squares
int square_count = 0;

for(i=1;i<=n;i++)
{
    p=sqrt(A[i]) ;   
    if(p*p==A[i])
    {
        squares[square_count++] = A[i];
    }
}
Alex
  • 9,891
  • 11
  • 53
  • 87