S
S
samsungovetch2018-07-11 15:35:17
C++ / C#
samsungovetch, 2018-07-11 15:35:17

C — How to find the largest subsequence of perfect squares in an array?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void ent(int *arrayName, int arraySize)
{
    int i;


  for (int i = 0; i < arraySize; i++)
  {
    printf("a[%i] = ", i);
    scanf("%i", &arrayName[i]);
  }
}

void proc(int *arrayName, int arraySize) {
    int i,k = 0;
    int flag = 0;
    for (i = 0; i < arraySize; ++i)
    {
        if ((sqrt(arrayName[i])) == (round((sqrt(arrayName[i])))))
            {
                printf("%d - full square \n", arrayName[i]);
                k++;
            }
        else
            {
                if (k!=0)
                    k = 1;
            }
    }
    printf("Max otrezok = %d \n", k);
}

void printArray(int *arrayName, int arraySize) {
    int i;
    for (i = 0; i < arraySize; ++i) {
        printf("%d ", arrayName[i]);
    }
}

void main()
{   int i;
  int n;//
  int squar = 0;

  printf("Size Massive: ");
  scanf("%d", &n);

    int Arr[n];
  ent(Arr, n);

  printf("Array: ");
    printArray(Arr, n);
    
    printf("\n");

    proc(Arr,n);
    


    system("pause");
}

I have this code. In this procedure
void proc(int *arrayName, int arraySize) {
    int i,k = 0;
    int flag = 0;
    for (i = 0; i < arraySize; ++i)
    {
        if ((sqrt(arrayName[i])) == (round((sqrt(arrayName[i])))))
            {
                printf("%d - full square \n", arrayName[i]);
                k++;
            }
        else
            {
                if (k!=0)
                    k = 1;
            }
    }
    printf("Max otrezok = %d \n", k);
}

when you enter, for example, 1 2 1 2 1, the answer is that the max. sublast = 2.
Help fix the conditions.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
doublench21, 2018-07-11
@doublench21

Do you even understand what a maximum subsequence is? Based on your algorithm, no.
And what are you writing the code for? Under beer? Everything dances like I don't know how.

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h>

void ent(int * arrayName, int arraySize) {
     // int i; Так давно уже писать не нужно

        for (int i = 0; i < arraySize; i++) {
            printf("a[%i] = ", i);
            scanf("%i", & arrayName[i]);
        }
}

void proc(int * arrayName, int arraySize) {
    int repeat = 0, maxRepeat = 0;

    for (int i = 0; i < arraySize; ++i) {
        if (checkSquareNumber(arrayName[i])) {
            printf("%d - full square \n", arrayName[i]);
            repeat++;
        } else {
            repeat = 0;
        }
        maxRepeat = repeat > maxRepeat ? repeat : maxRepeat;
    }

    printf("Max otrezok = %d \n", repeat); // Забыли как будет слово отрезок на английском? 
}

int checkSquareNumber(int number) {
    if (number == 0 || number == 1)
        return 1;

    if (number % 4 == 0 || number % 9 == 0)
        return 1;

    if (number % 8 == 1 || number % 3 == 1)
        return 1;

    return 0;
}

void printArray(int * arrayName, int arraySize) {
    // int i; Так давно уже писать не нужно
    for (int i = 0; i < arraySize; ++i) {
        printf("%d ", arrayName[i]);
    }
}

void main() {
    int i;
    int n; //
    int squar = 0;

    printf("Size Massive: "); // Забыли как будет слово массив на английском? 
    scanf("%d", & n);

    int Arr[n];
    ent(Arr, n);

    printf("Array: ");
    printArray(Arr, n);

    printf("\n");

    proc(Arr, n);

    system("pause");
}

R
res2001, 2018-07-11
@res2001

1. 2 - because you make k = 1 when the sequence ends. Make k = 0 - it will be 1.
And remove the condition - it is completely unnecessary.

else
            {
                    k = 0;
            }

2. In my opinion, it is impossible to calculate whether a number is a perfect square in this way - at least, because comparing real numbers directly is a bad idea. Read for seed, for example here .
I would do something like this:
double v = sqrt(arrayName[i]);
double v1 = floor(v);
if((v - v1) < 0.000001)
{
// число- полный квадрат
}

The constant 0.000001 - I chose at random, as small enough for your case.
In general, the question of comparing real numbers is a separate topic, the approach presented above is just one of the options, not the best, but it will work in your case.

S
samsungovetch, 2018-07-11
@samsungovetch

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void ent(int *arrayName, int arraySize)
{
    int i;


  for (int i = 0; i < arraySize; i++)
  {
    printf("a[%i] = ", i);
    scanf("%i", &arrayName[i]);
  }
}

void proc(int *arrayName, int arraySize) {
    int i,k = 0, max = 0;
    int flag = 0;
    for (i = 0; i < arraySize; ++i)
    {
        if ((sqrt(arrayName[i])) == (round((sqrt(arrayName[i])))))
            {
                printf("%d - full square \n", arrayName[i]);
                k++;
            }
        else k = 0;
        if (k>max)
            max = k;
    }
    printf("Max otrezok = %d \n", max);
}

void printArray(int *arrayName, int arraySize) {
    int i;
    for (i = 0; i < arraySize; ++i) {
        printf("%d ", arrayName[i]);
    }
}

void main()
{   int i;
  int n;//
  int squar = 0;

  printf("Size Massive: ");
  scanf("%d", &n);

    int Arr[n];
  ent(Arr, n);

  printf("Array: ");
    printArray(Arr, n);

    printf("\n");

    proc(Arr,n);



    system("pause");
}

Correct code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question