-1
#include <iostream>

const int turns = 2022;

int processing(int array[], int number, int index);

int main()
{
    int numLine[turns]; //actual number line
    //int counter = 0; //turns since last refference??
    int previousNumber;

    for (int i = 0; i < 7; i++) //for seven turns
    {
        std::cout << "Seed Number " << i+1 << ": "; //Seed Numer 1,2,3... : 
        std::cin >> numLine[i]; //input number
        std::cout << std::endl; // endline
    }

    for (int i = 7; i < turns; i++) //for the entire length of the turns after 7
    {
        previousNumber = numLine[i - 1];
        numLine[i] = processing(numLine, previousNumber, i-2); //set the current element of numLine to the processed number

        std::cout << numLine[i] << " ";
    }
}

int processing(int array[], int number, int index) //processing seeded with int number
{
    int counter = -1; // number of turns since last referrence

    for (int i = index; i >= 0; i--) //searches down from index to 0
    {
        if (array[i] == number) {
            counter = i; //times since its been said
        } 
    }

    if (counter == -1)
    {
        return 0;
    }
    else
        return counter;
        //has been seen before so return the count
}

// 13,16,10,12,1,5,8 (seed numbers)

This is my code, this is the prompt that I was given to design my program: http://cis.scc.losrios.edu/~TownJ/dinosaur/memory.html

When inputting the seed numbers required for the prompt, the output I receive is 9 (for the 2022nd element of the array, this code outputs all the elements) but when putting that into the website, it is incorrect, as is the number before it. I can't see what I am doing wrong, could someone with more experience tell me what I messed up?

  • I have successfully compiled the program in visual studio, when ran, it outputs a bunch of numbers (which is what is intended) –  Mar 08 '22 at 01:38
  • There are several examples given on the linked page along with the correct answer for each. Does your code provide the correct result for those? – Retired Ninja Mar 08 '22 at 01:39
  • @Retired Ninja it does not... –  Mar 08 '22 at 01:41
  • OK. hacked out example and eliminated my doubts. – user4581301 Mar 08 '22 at 01:43
  • If I understand what you are trying to do here, you want to return how many steps you need to go back to find a duplicate, but what you return is the index of the duplicate. `counter` literally needs to count. – user4581301 Mar 08 '22 at 02:00
  • @user4581301 okay so it's something i'm doing wrong in my processing function? I'm mistaking the index number for the value held in it somewhere within the search? –  Mar 08 '22 at 02:03
  • The index number is the location in the array. You could compute the difference between `index` and the starting location, or you could count with `counter`. Your call, but there's a simple elegance in `if (array[i] == number) { return index - i + 1; }` – user4581301 Mar 08 '22 at 02:15
  • 1
    Rather than start trying to find the unknown number you should consider solving one where you're given the answer. Once your code calculates a known value correctly move on the the unknown. The step by step example given is a good way to see if you're on the right track. You can step through your code in the debugger and compare the result for each step. – Retired Ninja Mar 08 '22 at 02:20
  • You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Mar 08 '22 at 02:38

1 Answers1

1

Thank you to everyone for your help, the following is my code and commented within is an explanation of such, the list of changes I made was big.

#include <iostream>

const int turns = 2022;

int processing(int array[], int number, int index); 

int main()
{
    int numLine[turns]; //actual number line
    int previousNumber;

    for (int i = 0; i < 7; i++) //for seven turns
    {
        std::cout << "Seed Number " << i+1 << ": "; //Seed Numer 1,2,3... : 
        std::cin >> numLine[i]; //input number
        std::cout << std::endl; // endline
    }

    for (int i = 7; i < turns; i++) //for the entire length of the turns after 7
    {
        previousNumber = numLine[i - 1];
        numLine[i] = processing(numLine, previousNumber, i); //set the current element of numLine to the processed number

        std::cout << numLine[i] << " "; //displays current value with space
    }
}

int processing(int array[], int number, int index) //processing seeded with array, previous value 
                                                   //on numLine and index position of the for loop (which mirrors the array element index)
{
    int counter = 0; // 

    for (int i = 0; i < index-1; i++) //loop starting at 0 and moving up to the position we are at in the array
    {
        if (array[i] == number) { //if the array element value at loop iteration i is the same as the previous number, then:
            counter = index - i - 1; //set counter to the index position passed, minus the search loop iteration, minus 1
        } 

    }
    return counter; //return the current value of counter
}

// 13,16,10,12,1,5,8 (seed numbers)
  • If you're interested, here's an dynamic programming approach that executes in linear time (no searching): https://godbolt.org/z/j8WazzcT5 -- it _does_ assume that the possible seeds must lie in the range [0, turns-1]. It's unclear whether that's guaranteed -- if not then a `unordered_map` can be used instead of `vector`. Such a change may also be appropriate if using a huge value for the number of turns (e.g. millions). – paddy Mar 08 '22 at 03:11
  • Asker had the right idea iterating the list backwards. If you go backwards you can stop as soon as you find the first match. This way you have to keep scanning in case there are more after the first match. [Basically Schlemiel the Painter's Algorithm](https://en.wikichip.org/wiki/schlemiel_the_painter%27s_algorithm). – user4581301 Mar 08 '22 at 03:28
  • @paddy That's pretty sweet! I went for brute force, as usual. :) https://godbolt.org/z/e8P8T6dPP – Retired Ninja Mar 08 '22 at 03:32
  • 1
    Yeah works well enough, until you set `turns` to 2022000 ;) – paddy Mar 08 '22 at 03:37