Hello. They asked me to write a program, .. there is a condition, but for me it is somehow poorly understood. Here it is:

Let some device as a state contain a number from 0 to 126. At each subsequent time, the number changes according to the formula x (n + 1) = a * x (n) + b (mod127), where a = 10, b = 11. Create a class that displays this device.

The class must have member functions:

  1. the constructor with the argument is the initial state,
  2. function that takes the device to the next state,
  3. a function that displays the status of the device.

Are x (n + 1) and x (n) the next and previous values? What is it all about? Can you hint an algorithm? There is no way for a lecturer to ask a simple task to verify the mastering of information ...

    2 answers 2

    who prevents to create a class in which there will be one integer type field, where the current state will be stored, a constructor with one parameter that will set this state and one method, such as

    class Generator { private: int curr; // поле текущего значения public: int getNext() { int x = (10*curr + 11) % 127; // тут я не ручаюсь, что точно расшифровал формулу // но по идее, именно так должно быть curr = x; return x; } int getCurrent() { return curr; } Generator(int start) { curr = start % 127; // поделим сразу, что бы точно было в заданном диапазоне. } } 

    I think you can do it yourself :)

      Apparently, yes, it is assumed that x(n) is the current state, and x(n + 1) is the following.

      Store state in the object; for the method “function that translates the device into the next state”, do this.state = (this.a * this.state + this.b) % 127; (if I understood the formula correctly).

      (To keep a and b in the classroom or to declare them constants is your business.)

      • I'm afraid you didn’t fully understand me ... At every next time point, the number changes according to the formula x (n + 1) = a * x (n) + b (mod127) -This line is mostly not clear (Here is a number, it is change according to this formula, then put the resulting number in the formula and get another? And how much to do so? - Alerr
      • one
        To do so every time you call a certain class function, which is described in the task as “transferring the device to the next state” - gecube