-1

i have this CS question that says:

We will define a series two three to be a series whose first term is some natural number. If the value of the member number n in the series is x, then the value of the (n +1)th member in the series is: (x % 2 ==0) ? x/2 : x*3 +1.

You must write a program that prints two or three series starting with the numbers 1 to twenty-five (not inclusive), but the creation of each series will stop when a value greater than a thousand or a value that has already appeared in a previous series is produced (and therefore the sub-series that was produced from this array onwards has already been produced). The value that is produced must be displayed again, thus stopping the production of the series.

now the code i have written outputs a similar result to the solution output but it needs some changes in order to get the same exact result which i couldn't figure out, this is my code.

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
int array[25];

    for (int i = 1; i < 25; i++)
    {
        int currentNum = i;
        int theNumAfter;
        bool occured = false;
    
        while (occured == false)
        {
            for (int i = 0; i <= 25; i++)
            {
                if (array[i] == currentNum)
                {
                    occured = true;
                    cout << endl;
                }
            }
    
            array[currentNum] = currentNum;
    
            cout << currentNum << " ";
    
            if (currentNum % 2 == 0)
            {
                theNumAfter = currentNum / 2;
            }
            else
            {
                theNumAfter = (3 * currentNum) + 1;
            }
    
            array[theNumAfter] = theNumAfter;
    
            cout << theNumAfter << " ";
            currentNum = theNumAfter;
        }
    }

}

the code doesn't take any input and there is only one right output which should be this:

1 4 2 1
2
3 10 5 16 8 4
4
5
6 3
7 22 11 34 17 52 26 13 40 20 10
8
9 28 14 7
10
11
12 6
13
14
15 46 23 70 35 106 53 160 80 40
16
17
18 9
19 58 29 88 44 22
20
21 64 32 16
22
23
24 12

the result of my code:

1 4
4 2
2 1 3 10
10 5
4 2
5 16 6 3
3 10 7 22
22 11 8 4
4 2 9 28 28 14
14 7
10 5
11 34 12 6
6 3 13 40 40 20
20 10
14 7 15 46 46 23
23 70
16 8 17 52 52 26 26 13
13 40 18 9
9 28 19 58 58 29 29 88 88 44 44 22
22 11

what should i change in the code, so we have matching outputs. thanks in advance

  • `int array[25];` is uninitialized. It could contain any values. You might try `int array[25] = {0};` instead. – Retired Ninja Nov 24 '22 at 20:47
  • @RetiredNinja thanks, but that doesn't solve the issue – Milad Bannourah Nov 24 '22 at 21:21
  • 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 Nov 24 '22 at 21:30
  • @SamVarshavchik i still no thing about debugging yet, it's been only 1 month since we started the semester, they didn't get into how to use the debugger – Milad Bannourah Nov 24 '22 at 22:20
  • Sounds like an excellent opportunity to spend a day or two quickly learning the basics, then amaze all other classmates with what looks like dark sorcery: watching your program run, one line at a time, monitor and see all variables, as they change during execution, and finding and fixing all the bugs all by yourself while everyone else can only post questions on Stackoverflow, then wait for days on a small chance that someone will answer. – Sam Varshavchik Nov 24 '22 at 22:34

1 Answers1

1

the creation of each series will stop when a value greater than a thousand or a value that has already appeared in a previous series is produced.

Up to 24, none of the produced values is greater than a thousand, but the posted code still has an access out of bounds bug:

int main()
{
    int array[25];
    //        ^^
    for (int i = 1; i < 25; i++)
    {
        int currentNum = i;
        int theNumAfter;
       
        // ...
            array[currentNum] = currentNum;
        // ...
            array[theNumAfter] = theNumAfter;
        // ...
   }
   // ...
}

Note the many of numbers in the expected output are greater than 25.

I'm not sure what this part was supposed to achive:

for (int i = 0; i <= 25; i++)
{ //            ^^^^^^^   it "checks" only the first 25 values that may occur
     if (array[i] == currentNum)
     {
          occured = true;
          cout << endl;      // <-- The duplicate should be printed before the newline.
                             // Here it should break out of the loop.
     }
}
array[currentNum] = currentNum;
cout << currentNum << " ";

But it fails to produce the expected output.

I'd use a simple array of 1000 bools to memorize the already occurred numbers.

#include <iostream>

int main()
{
    constexpr int limit{ 1'000 };
    bool already_seen[limit + 1]{};

    for (int i = 1; i < 25; i++)
    {
        int current{ i };
        while ( current <= limit  and  not already_seen[current] )
        {
            std::cout << current << ' ';
            already_seen[current] = true;

            if ( current % 2 == 0)
            {
                current /= 2;
            }
            else
            {
                current = (3 * current) + 1;
            }
        }
        std::cout << current << '\n';
    }
}

Testable here.

Bob__
  • 12,361
  • 3
  • 28
  • 42