-1
int n;
cin >> n; cin.ignore();

for(int i = 1; i < 11; i++){
    cout << i * n << " ";// stop printing space at the end number
}

Desired output-> "1 2 3 4 5 6 7 8 9 10"

  • `if(i != 1) cout << ' ';` before `cout << i * n;` – πάντα ῥεῖ Jul 06 '22 at 11:30
  • Well, don't print it. Either use a conditional or print the first element (without a trailing space) and then the other elements, *preceded* by a space. You may want to end the line with a `'\n'`, though. – Bob__ Jul 06 '22 at 11:32
  • Don't print the extra space, see [idiom-for-iterating-between-each-consecutive-pair-of-elements](https://stackoverflow.com/questions/35372784/idiom-for-iterating-between-each-consecutive-pair-of-elements) – Jarod42 Jul 06 '22 at 12:27
  • 2
    Here's how to figure this out, this always works! Take out a blank sheet of paper. Write down a step-by-step process of doing this, as short, brief sentences in plain English. [Have your rubber duck review your plan](https://en.wikipedia.org/wiki/Rubber_duck_debugging). We always refer such questions to their rubber duck. After your rubber duck approves your proposed plan of action, simply take what you've written down and translate it directly into C++, and you're done! Have you discussed this with your rubber duck, yet? – Sam Varshavchik Jul 06 '22 at 12:28
  • @SamVarshavchik onwards, I will use the rubber duck method, thanks. – Hiranmay Mandal Jul 07 '22 at 09:30

3 Answers3

2

The standard approach is to use a boolean variable, which is one time either true or false, then print a space or not, depending on the boolean, and set the boolen to a new value.

Often the output of the space in the loop is done first and then then value.

Example:

bool printSpace = false;
for(int i = 1; i < 11; ++i) {
    if (printSpace) std::cout << ' ';
    printSpace = true;
    std::cout << i * n;
}

You could also use std::exchange for flipping the bool. Please read here.

Then you could do something like the below:

#include <iostream>
#include <vector>
#include <utility>

int main() {
    
    std::vector data{1,2,3,4};
    
    bool showComma{};
    for (const int i: data) 
        std::cout << (std::exchange(showComma, true) ? "," : "") << i;
}

And the super high sophisticated stuff will be done with the std::ostream_joiner.

Reade here for an example. But, unfortunately this is not available for many installations.

A M
  • 14,694
  • 5
  • 19
  • 44
  • 1
    *"The standard approach"* Why more standard than alternatives? – Jarod42 Jul 06 '22 at 12:29
  • I used extra variable, but not a boolean (and no branching :-) ) with [my answer for idiom-for-iterating-between-each-consecutive-pair-of-elements](https://stackoverflow.com/a/35373017/2684539) (And I won't qualify my way of standard neither). Sorry to nit-picking for wording. (*"simple"* seems more appropriate than *"standard"* IMO). – Jarod42 Jul 06 '22 at 13:46
1

A simple approach is the following

for( int i = 0; i < 10; i++){
    if ( i ) cout << ' ';
    cout << ( i + 1 ) * n;
}

or

for( int i = 1; i < 11; i++){
    if ( i != 1 ) cout << ' ';
    cout << i * n;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I don't think you should've rewritten the bounds for this. When _not_ accessing arrays iterating from `1` is entirely acceptable and in this case makes perfect sense. – Mike Vine Jul 06 '22 at 11:56
0

One-liner:

cout << (i > 1 ? " " : "") << i * n;

Note: I prefer the "prefix" form because the starting value is easier to know than the ending one (>1 vs. >9).