-1

For a sequence of numbers a1, a2,...,an, we say that there is a period if 1≤p<n and if it holds that it is ai=ai+p for all values for which this equality makes sense.

For example, the sequence of numbers 1, 3, 1, 4, 2, 1, 3, 1, 4, 2, 1, 3 has period 5, because ai=ai+5 for all values such that both indices i and i+5 are within the allowable range (i.e. for 1 to 7 inclusive). The same sequence also has a period of 10. Next, we say that the sequence of numbers is periodic if it exists at least one number that is the period of that sequence, with the smallest such number being called the base sequence period. If such a number does not exist, the sequence is not periodic. For example, the above the sequence of numbers is periodic with the base period 5, while the sequence of numbers 4, 5, 1, 7, 1, 5 is not periodic.

#include <iostream>
#include <vector>

int period(std::vector<double> vektor) {
  int p;
  for (int i : vektor) {
    for (int j : vektor) {
      if (vektor[i] == vektor[j])
        p = j;
    }
  }
  return p;
}

int main() {
  std::vector<double> vektor{1, 3, 1, 4, 2, 1, 3, 1, 4, 2, 1, 3};
  std::cout << period(vektor);
  return 0;
}
  • This should be solved using vector.

Could you help me fix this code? This returns 3 as base period of sequence.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    Your function returns the last value in your vector, cast to `int`, after verifying that it is equal to itself. (Effectively returning the last value that is not `NaN`.) "Fixing" this code may come very close to rewriting it entirely. – Drew Dormann Mar 11 '22 at 21:05
  • 1
    A [range-for](https://en.cppreference.com/w/cpp/language/range-for) loop enumerates *values*, not *indexes*, so your use of `i` and `j` in `if (vektor[i] == vektor[j])` is wrong. In fact, the logic of the entire `period()` function is just plain wrong, it needs to be re-written from scratch to use a different approach. – Remy Lebeau Mar 11 '22 at 21:13
  • @devec Why are you using std::vector instead of std::vector or std::vector? – Vlad from Moscow Mar 11 '22 at 21:46
  • @VladfromMoscow because of task setting –  Mar 11 '22 at 22:21
  • @devec What task setting do you mean when all provided examples by your deal with integers? – Vlad from Moscow Mar 11 '22 at 22:23
  • I didn't put the whole task setting, all examples are integer values, but this must work for double also, because this code will be put to autotests on our IDE –  Mar 11 '22 at 22:26

1 Answers1

0

For starters it is unclear why you are using a vector with the value type double instead of the type int when all initializers have the type int.

The function period should accept a vector by constant reference.

The variable p is not initialized. As a result the function can return an indeterminate value.

The range based for loop does not return indices in a container as you think

for (int i : vektor) {

It returns stored in the vector objects of the type double.

So the condition in the if statement

if (vektor[i] == vektor[j])

makes no sense.

The function can look the following way as it is shown in the demonstration program below.

#include <iostream>
#include <vector>

size_t period( const std::vector<double> &v )
{
    size_t p = 0;

    for (size_t i = 1; !p && i < v.size(); i++)
    {
        size_t j = 0;

        while (j < v.size() - i && v[j] == v[j + i]) ++j;

        if ( j + i == v.size() ) p = i;
    }

    return p;
}

int main()
{
    std::vector<double> v = { 1, 3, 1, 4, 2, 1, 3, 1, 4, 2, 1, 3 };
    std::cout << period( v ) << '\n';
}

The program output is

5
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I need just one thing which I have not mentioned, could you please help me to check if number x is period of sequence? https://onlinegdb.com/mja0JRTBlZ for example in { 1, 3, 1, 4, 2, 1, 3, 1, 4, 2, 1, 3 } base period is 5, but how to prove that 10 is also period even though it is not the base period? –  Mar 11 '22 at 23:29
  • @devec You can ask one more question at SO. – Vlad from Moscow Mar 11 '22 at 23:30
  • here it is: https://stackoverflow.com/questions/71445747/ –  Mar 11 '22 at 23:41