Note that there are some explanatory texts on larger screens.

plurals
  1. POUnknown Error in Number Prediction Algorithm
    primarykey
    data
    text
    <p>So I'm working on this simple number prediction algorithm as an entry into algorithms and pattern recognition. It takes a linear string of numbers (14 in this case) starting at 2 and going up by 2 until 28.</p> <p>The program works out the difference between each number by subtracting the number before from it. It then checks all the differences are the same, then adds the difference to the last number and prints it to the screen.</p> <p>It works fine except that it thinks the difference is 0 every time and so prints the last number, 28, and the next number. There seem to be other questions like this, except they are asking how to do it with non-linear sequence and no one has the problem I do.</p> <p>I've tried everything I can think of, but it still can't determine the difference. It's probably something really obvious that I'm missing.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace number_predition_with_constant { class Program { static void Main(string[] args) { int[] sequence = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28 }; //all differ by 2. Diff = 2. Console.WriteLine(sequence); Console.WriteLine(""); int[] differences = {}; int legnth = sequence.Length; int diff = 0; //when not given value, some other instances not recognised int j = 0; //find difference between each number. for (int i = 0; i == legnth-1; i++) { j = i + 1; diff = sequence[j] - sequence[i]; differences[i] = diff; } //Print the difference between each number. Console.Write("Difference: "); Console.WriteLine(diff); //Check all diffs are same. If not the same, print "Error" for (int i = 0; i == legnth-1; i++) { if (differences[i] != differences[i+1]) { Console.WriteLine("Error"); } } //calculate next number and print. Console.Write("There are: "); Console.Write(legnth); Console.WriteLine(" Numbers in the sequence"); legnth = legnth - 1; int next = sequence[legnth] + diff; Console.Write("The next Number in the sequence is: "); Console.WriteLine(next); Console.ReadKey(); //Stop Console from closing till key pressed } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload