Welcome to our community

Be a part of something great, join today!

nested loops c++

ineedhelpnow

Well-known member
Jul 3, 2014
805
Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints:

0
1​
2​
3​

Sample program:

Code:
#include <iostream>
using namespace std;

int main() {
   int userNum  = 0;
   int i = 0;
   int j = 0;

   <STUDENT CODE>

   return 0;
}
someone help me! i dont know how to do this!
the indents at the top arent supposed to be that big. just one extra space for each but i had to use the indent thing so it made a HUGE space.
 

ZaidAlyafey

Well-known member
MHB Math Helper
Jan 17, 2013
1,667
Use a nested loop. The idea is that for each number $j$ the inner loop will make the spaces because it will run for $j$ number of times.
 
Last edited:

ineedhelpnow

Well-known member
Jul 3, 2014
805
(Giggle) yes the instructions say to use a nested loop but i dont know how. i actually did this problem a while ago but i cant remember how i did it.
 

Bacterius

Well-known member
MHB Math Helper
Jan 26, 2012
644
Hint:

Code:
0
    1
        2
            3
If you rewrite it in words, it looks like this:

Code:
0
SPACE 1
SPACE SPACE 2
SPACE SPACE SPACE 3
Notice a pattern? Looks like for all numbers N from 0 to 3, it prints out N spaces, and then that number. So you can use two loops here: the outer loop runs N from 0 to the user input, and the inner one takes care of printing as many spaces as required each time (equivalently, you can write a "print_n_spaces" function to help you, probably using a loop).

Remember, programming is predominantly about problem solving. It's okay to ask for help if you're really stuck, but you really, really, really should dig in before giving up and saying you can't do it. For instance, a problem solving trick could be to try and replicate the output for, say, N = 3. Then see how you can modify it to handle N = 4, and then try to generalize. Another technique is to work through what's going on on paper for a little while, and then extract the basic concept and code it up. Yet another technique (probably the most useful one) is to break up a problem into its constituent parts until they are easy to understand separately, code them up, and then put them back together into a solution to your problem - the print_n_spaces function I mentioned is an example of this.
 

jgaudiojr

New member
Sep 28, 2015
1
Couldn't somebody answer the question... I don't need hints.
 

MarkFL

Administrator
Staff member
Feb 24, 2012
13,775
Couldn't somebody answer the question... I don't need hints.
I cleaned up your post to make it more civil. Please don't post using censored words.

Here at MHB, we don't just do people's work for them...we try to actually help. What have you tried so far in this assignment?
 

LearnCode

New member
Oct 16, 2015
4
Did you ever figure this one out? I am struggling on this one too, I am able to make it work except it throws in extra spaces at the end.
 

MarkFL

Administrator
Staff member
Feb 24, 2012
13,775
Did you ever figure this one out? I am struggling on this one too, I am able to make it work except it throws in extra spaces at the end.
If you post your code, perhaps we can figure out why you are getting extra spaces at the end. :)
 

LearnCode

New member
Oct 16, 2015
4
it is giving me 4 spaces on a line after the last digit, I need the last digit to be the last line.
#include <iostream>
using namespace std;

int main() {
int userNum = 0;
int i = 0;
int j = 0;

/* Your solution goes here */
for(i=0; i<=userNum; i++) {
cout << i << endl;
for(j=0; j<=i; j++) {
cout << " ";
}
}
return 0;
}
 

johng

Well-known member
MHB Math Helper
Jan 25, 2013
236
Your code prints the integer followed by output of spaces; the problem says "print" spaces and then the integer. So move "cout<<i<<endl;" after the inner "for j loop". Also your for j loop "prints" i+1 spaces; the problem says print i spaces. So you need to modify the for j loop slightly.
In future, wrap code tags around your code so that it's more readable.
 

LearnCode

New member
Oct 16, 2015
4
I am still not fully understanding what to do, this stuff is due tomorrow and I have been strugging throug it for 3 days. If someone could help me see exactly what I am doing wrong I would appreciate it. I tried another variation based on what I am being told but still not getting it.

Code:
int main() {
    
    int userNum = 3;
    int i = 0;
    int j = 0;
    
	while (i<=0) {
	    while(j<=userNum) {
	        cout << j << endl;
	        cout << " ";
	        j++;
	    }
	 
	    i++;
	}
	return 0;
}
 

MarkFL

Administrator
Staff member
Feb 24, 2012
13,775
First, it looks to me like you want the outer loop to run from 0 to [m]userNum[/m], and then have the inner loop run from 0 to 1 less than the index of the outer loop. And only have the spaces printed by the inner loop, as follows:

Code:
int main() {
    
	int userNum = 3;
	int i = 0;
	int j = 0;
    
	while (i <= userNum) {
		while(j < i) {
			cout << " ";
			j++;
		}
	        cout << i << endl;	 
		i++;
		j = 0;
	}
	return 0;
}
 

LearnCode

New member
Oct 16, 2015
4
Thanks that was it, although I would like to understand how you came to it. I was about to start trying some crazy if/else statements
 

MarkFL

Administrator
Staff member
Feb 24, 2012
13,775
The best I can tell you about how I came up with it is to mentally step through the code line by line, iterating the loops, to see what's happening and how it works. :)
 

holmesr2007

New member
Oct 18, 2019
1
Another method is:

for (i = 0; i <= userNum; ++i){

for (j = 0; j < i; ++j){
cout << " ";}

cout << i << endl;
}
 

SpatiallyChallenged

New member
Oct 14, 2020
1
Another method is:

for (i = 0; i <= userNum; ++i){

for (j = 0; j < i; ++j){
cout << " ";}

cout << i << endl;
}
I love that this just saved me in October 2020. I can't thank you enough for this!! I was *so close* ... but close only counts in horseshoes and hand grenades, so I was still stuck. I feel like I could *almost* grasp the shape of the thing, but nooooot quite. Seeing this (combined with the thread advice in general) made a H U G E difference. TY again!!