How do I get the last number to create a new line?

I am working on a lab for a C++ course. My current code gives me most but not all points available for this lab. I can’t quite seem to get the code to print correctly. I’ve tried many possible ways of printing the code. On my closest attempts, the printed output seems to be missing a new line at the end of the last row. I would be most appreciative for any suggestions.

The assignment stated the following:

Hailstone sequence
Given a positive integer n, the following rules will always create a sequence that ends with 1, called the hailstone sequence:

If n is even, divide it by 2 If n is odd, multiply it by 3 and add 1 (i.e. 3n +1) Continue until n is 1 Write a program that reads an integer as input and prints the hailstone sequence starting with the integer entered. Format the output so that ten integers, each separated by a tab character (\t), are printed per line.

The output format can be achieved as follows: print(n, end=’\t’)

Ex: If the input is:
25

the output is:

25   76   38   19   58   29   88   44   22   11 
34   17   52   26   13   40   20   10   5    16 
8    4    2    1

My program below works but the auto-grader just wants me to have the exact match to what they have which has having a new line at the last number.

#include <iostream>
using namespace std;

int main() {
    int n; 
    int counter = 1;
    cin >> n;
    cout << n << "\t";

    while ( n >1) {
        if ( n%2 == 0) {
            n = n /2;
            cout << n << "\t";
        }
        else {
            n = 3 * n + 1;
            cout << n << "\t";
        }
        counter++;
        if ( counter % 10 ==0) {
            cout << endl;
        }
    }
    return 0;
}

Here is the auto grader telling what I need to fix:Zybooks auto-grader

I really don’t understand, how it could be done?

Well I tried adding the "\t" new tab but it give me an error of not being a new line at every 10 number, so I changed the program back to endl:
cout << n << "\t";

so I really don’t understand why the auto grader is so finicky.

  • Why is your code such sparse with empty lines, not invented consistently and reducing readability?

    – 




  • try to replace if ( counter % 10 ==0) { with if ( counter % 10 ==0 || n == 1) {

    – 

  • 1

    The auto-grader is garbage. You put in all of this work to actually solve the problem given, but some auto-grader is getting upset over a new-line at the end. What a waste of a student’s time. If anything, whoever maintains the auto-grader needs to fix the issue.

    – 




  • “I am working on a lab for a c++ course.” — As a lead-in to a question, this tells me that you have not done enough to isolate the question. This might be useful as background information (at the end of your question), but the beginning of your question should give the precise issue, which I think is basically “when printing data formatted at 10 per line, how do I ensure there is a newline character after the last datum?” Note that you can drop the arithmetic for this question; just print the numbers 1 through 24 as example data. After you get an answer, apply it to your real assignment.

    – 

  • Instead of one person (a post grad presumably) fixing the issue with the autograder, every single student has to fix the non-existant issues with their code. Really makes you feel valued doesn’t it?

    – 

Note that all the numbers are separated by whitespace – the difference is just which whitespace is separating them.

You’re printing “number and tab” as one unit, then occasionally adding a linebreak.
This make every line end with “number, tab, linebreak”, and the last one with “number, tab”.

Instead print number, separator, number, separator, number, …
Finish the whole thing off with a linebreak after the loop.

Something like this:

int main() {
    int counter = 1;
    int n; 
    cin >> n;
    // Print the first number
    cout << n;
    while (n > 1) {        
        // Select a separator.
        if (counter % 10 == 0) {
            cout << endl;
        }
        else {
            cout << '\t';
        }
        // Determine the next number.
        if (n % 2 == 0) {
            n = n / 2;
        }
        else {
            n = 3 * n + 1;
        }
        // Print the number.
        cout << n;
        counter++;
    }
    cout << '\n';
}

You can also shorten the loop a bit:

while (n > 1) {   
    n = n % 2 == 0 ? n / 2 : 3 * n + 1;
    char separator = counter % 10 == 0 ? '\n' : '\t';
    cout << separator << n;
    counter++;
}

After your while loop, check that counter is not divisible by 10 (and thus the loop hasn’t already printed a newline), and if so, print a newline.

if (count % 10 != 0) {
    cout << endl;
}

You could add another cout << endl; just before your return statement. This is outside the while (n > 1) loop, so will add a final newline before exiting. However I suspect the auto grader will still complain about your tab character, since it will be printed in your cout << n << "\t"; line from the n = 2 to n = 1 step.

If that doesn’t work with the autograder you could try refactoring your code slightly, and separate the arithmetic and logging (while keeping both in the loop). For instance since the sequence always ends with a 1 you can replace all of your logging with:

    if (n == 1) {
        cout << n << "\n";
    } else {
        cout << n << "\t";
    }

Rather than logging the value after the arithmetic, which is a bit cleaner and will end the sequence on a newline.

Leave a Comment