half dia pattern c++ reverse
Nov 29, 2020 at 4:40am UTC
How can I like reverse this pattern? to look like this?
https://imgur.com/a/TGlvIvK
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
using namespace std;
int main()
{
int trev, j, inpuT, column;
cout<<"Enter number of columns:" ;
cin>>inpuT;
columns=1;
for (trev=1;trev<inpuT*2;trev++){
for (j=1; j<=column; j++){
cout<<"* " ;
}
if (trev < inpuT){
column++;
}
else {
column--;
}
cout<<"\n" ;
}
return 0;
}
Nov 29, 2020 at 6:05am UTC
> In function 'int main()':
> 8:5: error: 'columns' was not declared in this scope
If your code doesn't even compile as posted, who knows what you're seeing.
Study what I did here with the "+ "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
using namespace std;
int main()
{
int trev, j, inpuT, column;
cout << "Enter number of columns:" ;
cin >> inpuT;
column = 1;
for (trev = 1; trev < inpuT * 2; trev++) {
for (j = 1; j <= column; j++) {
cout << "* " ;
}
for (j = column + 1; j < inpuT; j++)
cout << "+ " ;
if (trev < inpuT) {
column++;
} else {
column--;
}
cout << "\n" ;
}
return 0;
}
Learn how to indent code.
Nov 29, 2020 at 11:32am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
#include <iomanip>
int main()
{
size_t col {};
const auto disrow {[](auto no) {
for (size_t j = 1; j <= no; ++j)
std::cout << "* " ;
std::cout << '\n' ;
}};
const auto just {[&](auto no) {
std::cout << std::setw((col * 2 - no) * 2) << std::setfill(' ' ) << "" ;
disrow(no);
}};
std::cout << "How many columns: " ;
std::cin >> col;
for (size_t i = 1; i <= col; ++i)
disrow(i);
for (size_t i = col - 1; i > 0; --i)
disrow(i);
for (size_t i = 1; i <= col; ++i)
just(i);
for (size_t i = col - 1; i > 0; --i)
just(i);
}
How many columns: 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Last edited on Nov 29, 2020 at 11:36am UTC
Topic archived. No new replies allowed.