C++/triangle

Advertisement


Question
I'm trying to make a triangle that is facing the left.
a
aa
aaa
aa
a

The code i wrote is displaying it facing the right side, like above. Can u give me any hints on what i should do? The code I wrote is below:

#include <iostream>
using namespace std;

int main()

{
int row, column;
char sym;
int size;
cout << "Enter Desired Symbol:";

cin >> sym;

cout << "enter number of rows:";
cin >> size;

do
{
for (row = 1; row <= size; row++)
{
for(column = 1 ; column <= row ; column++)
{
cout << sym;
}

cout << "\n";

}

for (row = 1; row <= size; row++)
{
for(column = 1 ; column <= size - row ; column++)
{
cout << sym;
}

cout << "\n";

}

}while(row <= size);

return 0;

}


Answer
You just need to put enough space before the actual symbol:

#include <iostream>
using namespace std;

int main(){
int row, column;
char sym;
int size;

cout << "Enter Desired Symbol:";
cin >> sym;

cout << "enter number of rows:";
cin >> size;

for (row = 1; row <= size; row++){
for(column = 1 ; column <= size-row ; column++){
cout << ' ';
}
for(column = 1 ; column <= row ; column++){
cout << sym;
}
cout << "\n";
}

for (row = 1; row <= size; row++){
for(column = 1 ; column <= row ; column++){
cout << ' ';
}
for(column = 1 ; column <= size - row ; column++){
cout << sym;
}
cout << "\n";
}

return 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Sam

Expertise

You may ask me about simple to complicated C/C++ programming methods/style and most like questions about network programming in C++.

Experience

I'm a professional programer in C++ and network/internet programming.

©2016 About.com. All rights reserved.