how to add space to the following star pattern?
up vote
-2
down vote
favorite
How is it possible to print the following pattern?
*
**
***
****
*****
****
***
**
*
How can we add the spaces during the first half of the pattern?
I only managed to get the second half of the pattern right:
#include <iostream>
using namespace std;
void printpattern(int n)
{
for (int r = 0; r <= n; r++)
{
for (int z = 0; z <= r; z++) {
cout << "*";
}
cout << endl;
}
}
int main()
{
int n = 5;
printpattern(n);
}
c++ loops for-loop
|
show 4 more comments
up vote
-2
down vote
favorite
How is it possible to print the following pattern?
*
**
***
****
*****
****
***
**
*
How can we add the spaces during the first half of the pattern?
I only managed to get the second half of the pattern right:
#include <iostream>
using namespace std;
void printpattern(int n)
{
for (int r = 0; r <= n; r++)
{
for (int z = 0; z <= r; z++) {
cout << "*";
}
cout << endl;
}
}
int main()
{
int n = 5;
printpattern(n);
}
c++ loops for-loop
Ad a new loop before the nested loop to print the spaces?
– Some programmer dude
Nov 21 at 13:47
what 'boutcout << " ";
– Sigismondo
Nov 21 at 13:47
4
A picture of a screen that is rendering plain text? Really?
– Quentin
Nov 21 at 13:50
Homework? Show more work please. What have you tried? What are the results?
– Tim
Nov 21 at 13:51
1
@Quentin At least it's not as bad as Web 0.1 (sorry for repost, accidentally remove first comment).
– Some programmer dude
Nov 21 at 14:02
|
show 4 more comments
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
How is it possible to print the following pattern?
*
**
***
****
*****
****
***
**
*
How can we add the spaces during the first half of the pattern?
I only managed to get the second half of the pattern right:
#include <iostream>
using namespace std;
void printpattern(int n)
{
for (int r = 0; r <= n; r++)
{
for (int z = 0; z <= r; z++) {
cout << "*";
}
cout << endl;
}
}
int main()
{
int n = 5;
printpattern(n);
}
c++ loops for-loop
How is it possible to print the following pattern?
*
**
***
****
*****
****
***
**
*
How can we add the spaces during the first half of the pattern?
I only managed to get the second half of the pattern right:
#include <iostream>
using namespace std;
void printpattern(int n)
{
for (int r = 0; r <= n; r++)
{
for (int z = 0; z <= r; z++) {
cout << "*";
}
cout << endl;
}
}
int main()
{
int n = 5;
printpattern(n);
}
c++ loops for-loop
c++ loops for-loop
edited Nov 21 at 14:08
Swordfish
8,49611234
8,49611234
asked Nov 21 at 13:45
Sarah_Xx
616
616
Ad a new loop before the nested loop to print the spaces?
– Some programmer dude
Nov 21 at 13:47
what 'boutcout << " ";
– Sigismondo
Nov 21 at 13:47
4
A picture of a screen that is rendering plain text? Really?
– Quentin
Nov 21 at 13:50
Homework? Show more work please. What have you tried? What are the results?
– Tim
Nov 21 at 13:51
1
@Quentin At least it's not as bad as Web 0.1 (sorry for repost, accidentally remove first comment).
– Some programmer dude
Nov 21 at 14:02
|
show 4 more comments
Ad a new loop before the nested loop to print the spaces?
– Some programmer dude
Nov 21 at 13:47
what 'boutcout << " ";
– Sigismondo
Nov 21 at 13:47
4
A picture of a screen that is rendering plain text? Really?
– Quentin
Nov 21 at 13:50
Homework? Show more work please. What have you tried? What are the results?
– Tim
Nov 21 at 13:51
1
@Quentin At least it's not as bad as Web 0.1 (sorry for repost, accidentally remove first comment).
– Some programmer dude
Nov 21 at 14:02
Ad a new loop before the nested loop to print the spaces?
– Some programmer dude
Nov 21 at 13:47
Ad a new loop before the nested loop to print the spaces?
– Some programmer dude
Nov 21 at 13:47
what 'bout
cout << " ";
– Sigismondo
Nov 21 at 13:47
what 'bout
cout << " ";
– Sigismondo
Nov 21 at 13:47
4
4
A picture of a screen that is rendering plain text? Really?
– Quentin
Nov 21 at 13:50
A picture of a screen that is rendering plain text? Really?
– Quentin
Nov 21 at 13:50
Homework? Show more work please. What have you tried? What are the results?
– Tim
Nov 21 at 13:51
Homework? Show more work please. What have you tried? What are the results?
– Tim
Nov 21 at 13:51
1
1
@Quentin At least it's not as bad as Web 0.1 (sorry for repost, accidentally remove first comment).
– Some programmer dude
Nov 21 at 14:02
@Quentin At least it's not as bad as Web 0.1 (sorry for repost, accidentally remove first comment).
– Some programmer dude
Nov 21 at 14:02
|
show 4 more comments
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
Although comments suggest using std::string
, I believe this was intended to be written using only loops. This works:
void printpattern(int n)
{
// print first half.
for (int i = 0; i < n; ++i) {
// print spaces
for (int r = n - i; r > 0; --r)
std::cout << ' ';
// print stars.
for (int j = i; j > 0; --j)
std::cout << '*';
std::cout << 'n';
}
/// print second half. No need to print spaces here.
for (int i = 1; i <= n; ++i) {
for (int r = n - i; r >= 0; --r)
std::cout << '*';
std::cout << 'n';
}
}
wow that is fascinating how did you analyze it? i really tried but couldn't is it because i am still a beginner or what
– Sarah_Xx
Nov 22 at 20:17
add a comment |
up vote
2
down vote
If one is afraid of using loops such thingies can of course always be solved with recursion and a little math:
#include <iostream>
void pattern(int n, int p = 0)
{
if (!n) return;
if (!p) { pattern(2 * n * n - n, n); return; }
int k = --n / p, o = n % p + 1, t = o - (p - k);
std::cout.put(" *"[k >= p && t < p || k < p && t >= 0]);
--o || std::cout.put('n');
pattern(n, p);
}
1
I want to be able to upvote this more than once. It's beautiful!
– Retired Ninja
Nov 21 at 15:59
add a comment |
up vote
1
down vote
It's easier to do this by composing a longer string and using a sliding view.
That's pretty straightforward in C++:
#include <iostream>
#include <string>
#include <string_view>
void printpattern(std::size_t n)
{
const auto s = std::string(n, ' ') + std::string(n, '*') + std::string(n, ' ');
for (std::size_t i = 1; i < n*2; ++i)
std::cout << std::string_view(s.data()+i, n) << 'n';
}
int main()
{
printpattern(5);
}
You could of course make the space padding be of length n-1
on both sides, and use a more conventional loop starting i at zero:
const auto s = std::string(n - 1, ' ') + std::string(n, '*')
+ std::string(n-1, ' ');
for (std::size_t i = 0; i < n * 2 - 1; ++i)
It's up to you whether saving two characters of temporary string is worthwhile.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Although comments suggest using std::string
, I believe this was intended to be written using only loops. This works:
void printpattern(int n)
{
// print first half.
for (int i = 0; i < n; ++i) {
// print spaces
for (int r = n - i; r > 0; --r)
std::cout << ' ';
// print stars.
for (int j = i; j > 0; --j)
std::cout << '*';
std::cout << 'n';
}
/// print second half. No need to print spaces here.
for (int i = 1; i <= n; ++i) {
for (int r = n - i; r >= 0; --r)
std::cout << '*';
std::cout << 'n';
}
}
wow that is fascinating how did you analyze it? i really tried but couldn't is it because i am still a beginner or what
– Sarah_Xx
Nov 22 at 20:17
add a comment |
up vote
1
down vote
accepted
Although comments suggest using std::string
, I believe this was intended to be written using only loops. This works:
void printpattern(int n)
{
// print first half.
for (int i = 0; i < n; ++i) {
// print spaces
for (int r = n - i; r > 0; --r)
std::cout << ' ';
// print stars.
for (int j = i; j > 0; --j)
std::cout << '*';
std::cout << 'n';
}
/// print second half. No need to print spaces here.
for (int i = 1; i <= n; ++i) {
for (int r = n - i; r >= 0; --r)
std::cout << '*';
std::cout << 'n';
}
}
wow that is fascinating how did you analyze it? i really tried but couldn't is it because i am still a beginner or what
– Sarah_Xx
Nov 22 at 20:17
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Although comments suggest using std::string
, I believe this was intended to be written using only loops. This works:
void printpattern(int n)
{
// print first half.
for (int i = 0; i < n; ++i) {
// print spaces
for (int r = n - i; r > 0; --r)
std::cout << ' ';
// print stars.
for (int j = i; j > 0; --j)
std::cout << '*';
std::cout << 'n';
}
/// print second half. No need to print spaces here.
for (int i = 1; i <= n; ++i) {
for (int r = n - i; r >= 0; --r)
std::cout << '*';
std::cout << 'n';
}
}
Although comments suggest using std::string
, I believe this was intended to be written using only loops. This works:
void printpattern(int n)
{
// print first half.
for (int i = 0; i < n; ++i) {
// print spaces
for (int r = n - i; r > 0; --r)
std::cout << ' ';
// print stars.
for (int j = i; j > 0; --j)
std::cout << '*';
std::cout << 'n';
}
/// print second half. No need to print spaces here.
for (int i = 1; i <= n; ++i) {
for (int r = n - i; r >= 0; --r)
std::cout << '*';
std::cout << 'n';
}
}
edited Nov 21 at 14:31
answered Nov 21 at 14:20
sstefan
312211
312211
wow that is fascinating how did you analyze it? i really tried but couldn't is it because i am still a beginner or what
– Sarah_Xx
Nov 22 at 20:17
add a comment |
wow that is fascinating how did you analyze it? i really tried but couldn't is it because i am still a beginner or what
– Sarah_Xx
Nov 22 at 20:17
wow that is fascinating how did you analyze it? i really tried but couldn't is it because i am still a beginner or what
– Sarah_Xx
Nov 22 at 20:17
wow that is fascinating how did you analyze it? i really tried but couldn't is it because i am still a beginner or what
– Sarah_Xx
Nov 22 at 20:17
add a comment |
up vote
2
down vote
If one is afraid of using loops such thingies can of course always be solved with recursion and a little math:
#include <iostream>
void pattern(int n, int p = 0)
{
if (!n) return;
if (!p) { pattern(2 * n * n - n, n); return; }
int k = --n / p, o = n % p + 1, t = o - (p - k);
std::cout.put(" *"[k >= p && t < p || k < p && t >= 0]);
--o || std::cout.put('n');
pattern(n, p);
}
1
I want to be able to upvote this more than once. It's beautiful!
– Retired Ninja
Nov 21 at 15:59
add a comment |
up vote
2
down vote
If one is afraid of using loops such thingies can of course always be solved with recursion and a little math:
#include <iostream>
void pattern(int n, int p = 0)
{
if (!n) return;
if (!p) { pattern(2 * n * n - n, n); return; }
int k = --n / p, o = n % p + 1, t = o - (p - k);
std::cout.put(" *"[k >= p && t < p || k < p && t >= 0]);
--o || std::cout.put('n');
pattern(n, p);
}
1
I want to be able to upvote this more than once. It's beautiful!
– Retired Ninja
Nov 21 at 15:59
add a comment |
up vote
2
down vote
up vote
2
down vote
If one is afraid of using loops such thingies can of course always be solved with recursion and a little math:
#include <iostream>
void pattern(int n, int p = 0)
{
if (!n) return;
if (!p) { pattern(2 * n * n - n, n); return; }
int k = --n / p, o = n % p + 1, t = o - (p - k);
std::cout.put(" *"[k >= p && t < p || k < p && t >= 0]);
--o || std::cout.put('n');
pattern(n, p);
}
If one is afraid of using loops such thingies can of course always be solved with recursion and a little math:
#include <iostream>
void pattern(int n, int p = 0)
{
if (!n) return;
if (!p) { pattern(2 * n * n - n, n); return; }
int k = --n / p, o = n % p + 1, t = o - (p - k);
std::cout.put(" *"[k >= p && t < p || k < p && t >= 0]);
--o || std::cout.put('n');
pattern(n, p);
}
edited Nov 21 at 16:25
answered Nov 21 at 15:41
Swordfish
8,49611234
8,49611234
1
I want to be able to upvote this more than once. It's beautiful!
– Retired Ninja
Nov 21 at 15:59
add a comment |
1
I want to be able to upvote this more than once. It's beautiful!
– Retired Ninja
Nov 21 at 15:59
1
1
I want to be able to upvote this more than once. It's beautiful!
– Retired Ninja
Nov 21 at 15:59
I want to be able to upvote this more than once. It's beautiful!
– Retired Ninja
Nov 21 at 15:59
add a comment |
up vote
1
down vote
It's easier to do this by composing a longer string and using a sliding view.
That's pretty straightforward in C++:
#include <iostream>
#include <string>
#include <string_view>
void printpattern(std::size_t n)
{
const auto s = std::string(n, ' ') + std::string(n, '*') + std::string(n, ' ');
for (std::size_t i = 1; i < n*2; ++i)
std::cout << std::string_view(s.data()+i, n) << 'n';
}
int main()
{
printpattern(5);
}
You could of course make the space padding be of length n-1
on both sides, and use a more conventional loop starting i at zero:
const auto s = std::string(n - 1, ' ') + std::string(n, '*')
+ std::string(n-1, ' ');
for (std::size_t i = 0; i < n * 2 - 1; ++i)
It's up to you whether saving two characters of temporary string is worthwhile.
add a comment |
up vote
1
down vote
It's easier to do this by composing a longer string and using a sliding view.
That's pretty straightforward in C++:
#include <iostream>
#include <string>
#include <string_view>
void printpattern(std::size_t n)
{
const auto s = std::string(n, ' ') + std::string(n, '*') + std::string(n, ' ');
for (std::size_t i = 1; i < n*2; ++i)
std::cout << std::string_view(s.data()+i, n) << 'n';
}
int main()
{
printpattern(5);
}
You could of course make the space padding be of length n-1
on both sides, and use a more conventional loop starting i at zero:
const auto s = std::string(n - 1, ' ') + std::string(n, '*')
+ std::string(n-1, ' ');
for (std::size_t i = 0; i < n * 2 - 1; ++i)
It's up to you whether saving two characters of temporary string is worthwhile.
add a comment |
up vote
1
down vote
up vote
1
down vote
It's easier to do this by composing a longer string and using a sliding view.
That's pretty straightforward in C++:
#include <iostream>
#include <string>
#include <string_view>
void printpattern(std::size_t n)
{
const auto s = std::string(n, ' ') + std::string(n, '*') + std::string(n, ' ');
for (std::size_t i = 1; i < n*2; ++i)
std::cout << std::string_view(s.data()+i, n) << 'n';
}
int main()
{
printpattern(5);
}
You could of course make the space padding be of length n-1
on both sides, and use a more conventional loop starting i at zero:
const auto s = std::string(n - 1, ' ') + std::string(n, '*')
+ std::string(n-1, ' ');
for (std::size_t i = 0; i < n * 2 - 1; ++i)
It's up to you whether saving two characters of temporary string is worthwhile.
It's easier to do this by composing a longer string and using a sliding view.
That's pretty straightforward in C++:
#include <iostream>
#include <string>
#include <string_view>
void printpattern(std::size_t n)
{
const auto s = std::string(n, ' ') + std::string(n, '*') + std::string(n, ' ');
for (std::size_t i = 1; i < n*2; ++i)
std::cout << std::string_view(s.data()+i, n) << 'n';
}
int main()
{
printpattern(5);
}
You could of course make the space padding be of length n-1
on both sides, and use a more conventional loop starting i at zero:
const auto s = std::string(n - 1, ' ') + std::string(n, '*')
+ std::string(n-1, ' ');
for (std::size_t i = 0; i < n * 2 - 1; ++i)
It's up to you whether saving two characters of temporary string is worthwhile.
edited Nov 21 at 16:36
answered Nov 21 at 15:59
Toby Speight
16k133965
16k133965
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413464%2fhow-to-add-space-to-the-following-star-pattern%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Ad a new loop before the nested loop to print the spaces?
– Some programmer dude
Nov 21 at 13:47
what 'bout
cout << " ";
– Sigismondo
Nov 21 at 13:47
4
A picture of a screen that is rendering plain text? Really?
– Quentin
Nov 21 at 13:50
Homework? Show more work please. What have you tried? What are the results?
– Tim
Nov 21 at 13:51
1
@Quentin At least it's not as bad as Web 0.1 (sorry for repost, accidentally remove first comment).
– Some programmer dude
Nov 21 at 14:02