![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0
![]() |
I'm on a role with these questions here...
Make this, only using while statements:
user inputs a number: (for example, 5) * * * * * * * * * * * * * * * * and makes a square of asterisks, with 5 asterisks on each side. Here's my code so far, and i'm stuck... // Exercise 4.25 Asterisk square problem.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; int main() { int number; cout << "Enter a number to make a square with that many number of asterisks on each side: "; cin >> number; int count = 0, column = 0, wideness = 0, rows = 0; cout << endl << endl << endl; while (count < number) { cout << "* "; count++; } cout << endl; while (rows < number - 2) { cout << "*"; while (column < number-2) { cout << " "; column++; } cout << " "; cout << "*"; cout << endl; rows++; } while (wideness < number) { cout << endl <<"* "; wideness++; } return 0; }
__________________
Support Our Troops |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0
![]() |
and it's not making me the box I want....
o o o o o o o o o o o o o o o o
__________________
Support Our Troops |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0
![]() |
ok, the box has the number of asterisks on each side, boardering the square, that the user inputs... it won't let me do it, it keeps pushing the outside border of asterisks on the left side of the square, even in my program...
__________________
Support Our Troops |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Edit your posts, and put both the squares and the C++ code in [code] tags.
|
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Here's how I did it in Python:
def square (size):
print "*" * size
print ("*" + " " * (size - 2) + "*\n") * (size - 2),
print "*" * size |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
i didn't see anything in the project specs about the square being empty. was that left out, or did you just assume that? obviously, it would be much easier if the square were filled like:
***** ***** ***** ***** *****
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0
![]() |
supposed to be empty
__________________
Support Our Troops |
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
ahhh. i'm always looking for the easy way out.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0
![]() |
me too, too bad teachers don't.
__________________
Support Our Troops |
|
|
|
|
|
#10 | |
|
Newbie
Join Date: Apr 2005
Posts: 9
Rep Power: 0
![]() |
you need to re-initialise your variables before you use them again... particullarly in the nested loop, heres some working code:
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number to make a square with that many number of asterisks on each side: ";
cin >> number;
int count = 0, column = 0, wideness = 0, rows = 0;
cout << endl << endl << endl;
while (count < number){
cout << "* ";
count++;
}
cout << endl;
while (rows < number - 2){
cout << "* ";
column=0;
while (column < number - 2)
{
cout << " ";
column++;
}
cout << "* ";
cout << endl;
rows++;
}
count=0;
while (count < number){
cout << "* ";
count++;
}
return 0;
}not sure what was going on with this: Quote:
__________________
www.ice52.co.uk Last edited by ice52; Apr 11th, 2005 at 8:38 PM. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|