October 6th, 2005, 05:42 AM
C++ loop question
I just started learning C++ through a class in college and we are on loops right now. We started with this simple program.
It simply produces the shape:
using namespace std;
int row, column;
int maxRow = 5;
int maxCol = 4;
for(row = 0;row < maxRow; row++)
for(column = 0; column<maxCol; column++)
cout << "*";
cout << endl;
My question is how can I manipulate this program to produce the following shape:
Any suggestions would be MUCH appreciated, thanks again.
October 6th, 2005, 07:30 AM
1. You could either split your loop into 3 parts:
"top", a loop to create your "** **" structure, "bottom"
2. A slower method would be to replace
with a few if-statements, like
I am sure you could have worked this out by your own ?
if ( (row%(maxRow-1)!=0) &&
( column > 1) && (column<maxCol-2) ) cout << " ";
else cout << "*";
If the only tool you have is a hammer, you tend to see every problem as a nail.
(Abraham Maslow, Psychologist, 1908-70)
October 13th, 2005, 12:51 PM
Both the methods are good but the second one is judged on the basis of the output required. Though at the begineer level you need to focus on the output only but still if you divide the problem into parts then it would be easy to modify the same if more no. of star space rows are needed. if the no. of rows required changes all u have to do is to change digits in for loop.
So try to make generic programs to solve problems of a particular category and dont go for the solution to a particular problem. This will help you in future when you create programs for generating complex patterns. If for each such pattern you try to find a mathematical expressioned if startements than it will be creating problems for urself. So try to break a problem into parts and reach the conclusion.
\"The Smilie Wars\" ... just arrived after the great crusades
.... computers come to the rescue .... ah technology at last has some use.