Trying to do pattern...
Apr 21, 2011 at 8:59pm UTC
Hi,
Im a beginner to C++. I know some, but Im trying to get the code posted below
to output the number of diamonds entered, BUT I want them to go side by side instead of below one another..Any help?
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// Declare variables.
int width = 13; // the width the user wants the diamond
int half_width; //the width used to do the triangles for the diamond
int inner; // inner loop to display the stars and spaces
int outer; // outer loop
int number; // number of diamonds printed
int count = 1;
half_width = (width / 2) + 1; // used to determine the width of the diamond
cout << "How many diamonds do you want to create? " ;
cin >> number;
while (count <= number)
{
cout << "\nXXXXXXXXXXXXXXX" ;
// Top half of the diamond.
for (outer = 1; outer <= half_width ; outer++)
{
cout << "\n" ;
for (inner = half_width; inner >= outer; inner--)
cout << "X" ;
for (inner = 2; inner <= outer; inner++)
cout << " " ;
for (inner = 1; inner <= outer; inner++)
cout << " " ;
for (inner = half_width; inner >= outer; inner--)
cout << "X" ;
}
// Bottom half of the diamond
for (outer = 1; outer < half_width; outer++)
{
cout << "\n" ;
for (inner = 0; inner <= outer; inner++)
cout << "X" ;
for (inner = half_width - 1; inner > outer; inner--)
cout << " " ;
for (inner = half_width; inner > outer; inner--)
cout << " " ;
for (inner = 0; inner <= outer; inner++)
cout << "X" ;
}
cout << "\nXXXXXXXXXXXXXXX" ;
cout << "\n\n" ;
count++;
}
return 0;
}
Last edited on Apr 21, 2011 at 9:01pm UTC
Apr 21, 2011 at 9:47pm UTC
I've marked the modified lines.
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// Declare variables.
int width = 13; // the width the user wants the diamond
int half_width; //the width used to do the triangles for the diamond
int inner; // inner loop to display the stars and spaces
int outer; // outer loop
int number; // number of diamonds printed
int count = 1;
half_width = (width / 2) + 1; // used to determine the width of the diamond
cout << "How many diamonds do you want to create? " ;
cin >> number;
//while (count <= number)
//{
cout << "\n" ;
for (count = 0; count < number; count++)
{
cout << "XXXXXXXXXXXXXXX" ;
cout << " " ; //the space between diamonds
}
// Top half of the diamond.
for (outer = 1; outer <= half_width ; outer++)
{
cout << "\n" ;
for (count = 0; count < number; count++)
{
for (inner = half_width; inner >= outer; inner--)
cout << "X" ;
for (inner = 2; inner <= outer; inner++)
cout << " " ;
for (inner = 1; inner <= outer; inner++)
cout << " " ;
for (inner = half_width; inner >= outer; inner--)
cout << "X" ;
cout << " " ; //the space between diamonds
}
}
// Bottom half of the diamond
for (outer = 1; outer < half_width; outer++)
{
cout << "\n" ;
for (count = 0; count < number; count++)
{
for (inner = 0; inner <= outer; inner++)
cout << "X" ;
for (inner = half_width - 1; inner > outer; inner--)
cout << " " ;
for (inner = half_width; inner > outer; inner--)
cout << " " ;
for (inner = 0; inner <= outer; inner++)
cout << "X" ;
cout << " " ; //the space between diamonds
}
}
cout << "\n" ;
for (count = 0; count < number; count++)
{
cout << "XXXXXXXXXXXXXXX" ;
cout << " " ; //the space between diamonds
}
cout << "\n\n" ;
//count++;
//}
return 0;
}
Apr 21, 2011 at 11:42pm UTC
thanks a lot...I was trying to tab right after, but I should have used another loop as you showed me.
Last edited on Apr 21, 2011 at 11:43pm UTC
Topic archived. No new replies allowed.