HW loops i need help PLEASE
Mar 24, 2015 at 12:54am UTC
so ive been looking at this i really can't solve it i need explanation
i displayed it here the link. can people see this or this cant be displayed here
help
cpp.sh/4ir3 .
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
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int x, y, tempL, tempR, middle;
cout << "Please enter # of diamonds: " ;
cin >> y;
while (y){
cout << "Please enter the size: " ;
cin >> x;
if (x % 2 == 0) { middle = tempL=tempR = x / 2 ; }
else { middle=tempL= tempR = x / 2 + 1 ; }
for (int i=1; i<= x; i++){
for (int j=1; j<=x; j++){
if (j >= tempL && j<=tempR)
cout <<" *" ;
else
cout <<" " ;
}
cout << endl;
if (i<middle){ tempL--; tempR++; }
else {tempL++; tempR--; }
}
y--;
}
return 0;
}
//change this to display to
//cpp.sh/4ir3
Last edited on Mar 24, 2015 at 1:20am UTC
Mar 24, 2015 at 5:11am UTC
Someone posted something almost like what you wanted to do a couple days ago. I can't find the post, but the code was something like this:
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
#include <iostream>
using namespace std;
void printStars(int len)
{
if (len == 0)
return ;
for (int i = 0; i < len; ++i)
cout << '*' ;
cout << endl;
printStars(len - 1);
for (int i = 0; i < len; ++i)
cout << '*' ;
cout << endl;
return ;
}
int main()
{
int size;
cout << "Please enter the size: " ;
cin >> size;
printStars(size);
}
I'm sure you can convert it to using loops instead of a recursive function, and modify it to look exactly like what you want.
Last edited on Mar 24, 2015 at 5:12am UTC
Topic archived. No new replies allowed.