How Can I Convert a Nested For Loop to a Do-While Loop in C++?

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    Loops Product
AI Thread Summary
The discussion focuses on converting a nested for loop into a do-while loop in C++. The original code generates a diagonal pattern of "#" characters, while the modified version using a do-while loop fails to replicate the intended diagonal alignment. Users suggest incorporating conditional statements within the loop to control when the "#" is printed, but this leads to unintended outputs, such as multiple diagonal lines. There is a request for the original and modified code outputs to clarify the differences. The conversation highlights the challenges of maintaining the desired output format when changing loop structures.
magnifik
Messages
350
Reaction score
0
I am trying to create a code that does the exact same thing as this does
Code:
#include <iostream>
	using namespace std;

	int main()
	{
	    int len;

	    cout << "Enter a number: ";
	    cin >> len;

	    for (int i = 0; i < len; i++)
	    {
		for (int j = i+1; j < len; j++)
		{
		    cout << ' ';
		}
		cout << "#\n";
	    }
	}

but using a do while loop instead. what i have so far is this

Code:
#include <iostream>
	using namespace std;

	int main()
	{
	    int len;

	    cout << "Enter a number: ";
	    cin >> len;
			
	    for (int i = 0; i < len; i++)
	    {
			int j = i+1;
			do
			{
				cout << ' ';
				j++;
			}
		while (j < len);
		cout << "#\n";
		}
	}

the only problem is on the last line the "#" signs line up instead of being diagonal. any help on how to fix this??
 
Physics news on Phys.org
try put a if condition insdie the do while loop instead just cout outside while loop. could use conditions like if j = len -1, then cout the #
 
kentigens said:
try put a if condition insdie the do while loop instead just cout outside while loop. could use conditions like if j = len -1, then cout the #

hmm, not exactly sure what you mean. the closest i got was adding

Code:
 if (j == len -1) {
           cout << "#";
           }
into the do part of the do while loop, but this just created two side by side diagonal lines :T to top line had len - 1, but the bottom line had the correct shape and number. any idea on how to get rid of the first one?
 
i mean to take out the line where u cout the # from ur original code. and btw, don't have vs here, so can't really run ur code. can u please post up the outputs? both original code and the one u have atm?
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Back
Top