-
December 11th, 2017, 12:08 PM
#1
Beginner stuff: Pyramid with while loop
Hello. So I'm having trouble with c) & d) and I need some help.
I think I should add cout << " "; somewhere, but I don't know exactly where. Here is my code so far for a) and b). We haven't studied for yet, by the way. We have to use while.
Code:
#include <iostream>
using namespace std;
int main()
// 1
{
cout << "Pyramid 1" << endl;
int line1 = 1;
do
{
int stars1 = 1;
do
{
cout << "*";
stars1++;
} while (stars1 <= line1);
cout << endl;
line++;
} while (line <= 10);
// 2
cout << "Pyramid 2" << endl;
int stars2 = 10;
int line2 = 1;
while (stars2 != 0)
{
while (stars2 >= red2)
{
cout << "*";
line2++;
}
stars2--;
line2 = 1;
cout << endl;
}
// 3
cout << "Pyramid 3" << endl;
return 0;
}
Last edited by 2kaud; December 11th, 2017 at 12:44 PM.
Reason: Changed tages to code
-
December 11th, 2017, 12:53 PM
#2
Re: Beginner stuff: Pyramid with while loop
When posting code, it is better that you use code tags rather than html tags so that the code is formatted better. Go Advanced, select the code and click '#'.
For pyramids c) and d), you need to output spaces before the stars to make the stars align on the right. The total of the number of spaces output and number of stars output needs to be always 10. For c) you start with 0 spaces and 10 stars then for each line you increment the number of spaces and decrement the number of stars. For d) you start with 9 spaces and 1 star then for each line you decrement the number of spaces and increment the number of stars. So that in both cases the total of both equal 10.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 11th, 2017, 12:59 PM
#3
Re: Beginner stuff: Pyramid with while loop
So I should include another while for spaces?
-
December 11th, 2017, 01:05 PM
#4
Re: Beginner stuff: Pyramid with while loop
Sorry for double posting. If it's not too much trouble, could you give me an example code of how it should look? I tried adding space as int space = 0; and then adding this to the code for c):
Code:
while (stars2 >= line2)
{
cout << "*";
line2++;
cout << " ";
space++;
}
-
December 11th, 2017, 01:23 PM
#5
Re: Beginner stuff: Pyramid with while loop
For c), using a do loop rather than a for loop, consider
Code:
#include <iostream>
#include <iomanip>
using namespace std;
const int swidth = 10;
int main()
{
int line = swidth;
do {
cout << setw(swidth - line) << setfill(' ') << "" << setw(line) << setfill('*') << "*" << endl;
} while (--line);
}
The for loop would be
Code:
#include <iostream>
#include <iomanip>
using namespace std;
const int swidth = 10;
int main()
{
for (int line = swidth; line; --line)
cout << setw(swidth - line) << setfill(' ') << "" << setw(line) << setfill('*') << "*" << endl;
}
Last edited by 2kaud; December 11th, 2017 at 01:26 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 11th, 2017, 01:25 PM
#6
Re: Beginner stuff: Pyramid with while loop
We haven't studied for yet, we have to use do while and while only.
-
December 11th, 2017, 01:30 PM
#7
Re: Beginner stuff: Pyramid with while loop
Originally Posted by thestickupman
We haven't studied for yet, we have to use do while and while only.
Yes - see the first example code in post #5 which uses do..while. I only included the second example code for your information.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 11th, 2017, 01:32 PM
#8
Re: Beginner stuff: Pyramid with while loop
Can you help me work around this? Cause a lot of things from your first example are not familiar. I set space3 = 0 and I should increase it, right? So, where exactly am I supposed to include it?
Code:
int stars3 = 10;
int line3 = 1;
int space3 = 0;
while (stars3 != 0)
{
while (stars3 >= line)
{
cout << "*";
line3++;
}
stars3--;
line3 = 1;
cout << endl;
}
-
December 11th, 2017, 01:43 PM
#9
Re: Beginner stuff: Pyramid with while loop
Consider
Code:
#include <iostream>
using namespace std;
const int swidth = 10;
int main()
{
int line3 = swidth;
do {
int stars3 = line3;
int space3 = swidth - stars3;
while (space3--)
cout << " ";
while (stars3--)
cout << "*";
cout << endl;
} while (--line3);
}
You should be able to do d) now OK.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 11th, 2017, 01:49 PM
#10
Re: Beginner stuff: Pyramid with while loop
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 11th, 2017, 01:53 PM
#11
Re: Beginner stuff: Pyramid with while loop
Thanks, man. What are you using "swidth" for?
-
December 11th, 2017, 02:44 PM
#12
Re: Beginner stuff: Pyramid with while loop
So that it's easy to change the number of stars on the first line. Try changing it to say 20. It's better to use a const variable to store something that is constant in the program but could change. IF you had 10 used in various places in the code, then to change from 10 to 20 would require looking through the code and making several changes. This way you just change the value once.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|