Friday, July 31, 2009

Need help with this C++ program.?

Fill in the blanks in the code below. The program is supposed to


print a triangle of the following shape:


*


**


***


****


***** %26lt;-- This example is for height=5





#include %26lt;iostream%26gt;


using namespace std;





void spaces(int x) {


for (________________________)


cout %26lt;%26lt; " ";


}





void stars(int x) {


for (_________________________)


cout %26lt;%26lt; "*";


}





int main() {


int rows;


cout %26lt;%26lt; "How many rows? ";


cin %26gt;%26gt; rows;





for (int star = 1; star %26lt;= rows; star ++) {


spaces (____________________);


stars (_____________________);


cout %26lt;%26lt; endl;


}





}

Need help with this C++ program.?
You should really do your own homework; otherwise, you won't learn anything. However, I'll help you as best I can.





What you seem to have is a procedural program with some functions. (If you don't understand that sentence, look up those terms. They are important. The same applies to any terms you don't understand in this answer)





The first block of code is a function called "spaces". It takes in a single variable: an integer called "x".


What is this block of code supposed to do? It features a for loop that prints out (cout) a single space (" ") each iteration. Clearly you want to control how many spaces are printed out. It seems that "x" is probably important for this.





You've probably been taught in class the way a for loop is supposed to be created, but here's a refresher.


The syntax is:


for( [an initial statement]; [some condition]; [a statement to execute each iteration])


Your initial statement will usually initialize a variable such as "i" which will be used in the loop. Examples of this could be int i=0; int i=x*2; char* i="foo"; bool i = true; or anything else.


Your condition is the terms that must be met for the loop to continue. Usually, this is also in terms of i. For example: i == true; i %26gt;= x; i != 7; etc.


Your final statement is a single line of code that will be executed at the end of the for loop. This is usually something simple like i++, but it could be anything else such as i = i + x or even void * j = %26amp;spaces(i)





Figure out a combination of three statements in terms of x that will make spaces print out the correct number of spaces.





Next is another function called stars. stars looks almost exactly like spaces except that it prints stars ("*") instead of spaces. This part should be easy.





Next comes your main code block. It first asks the user to input a number of rows. Then it goes to another for loop.


This last for loop goes through as many iterations as the user entered (in the example you gave, 5).


Each iteration calls spaces and then stars and then outputs a new line (cout %26lt;%26lt; endl;)


spaces and stars both take in a single argument, right? That single argument is an integer? You need to specify an integer (in terms of the variables "star" and "rows" since those are the only ones you have to work with) that prints out the correct number of stars and spaces.





Good luck!
Reply:if you do a search on answers you'll find this exact program fully written. There are 62 results for "print a triangle". Several of the first are recursive, but many of the rest feature fully written forms of this program.
Reply:First comes your main code block. It first asks the user to input a number of rows. Then it goes to another for loop.


This last for loop goes through as many iterations as the user entered (in the example you gave, 5).


Each iteration calls spaces and then stars and then outputs a new line (cout %26lt;%26lt; endl;)


spaces and stars both take in a single argument, right? That single argument is an integer? You need to specify an integer (in terms of the variables "star" and "rows" since those are the only ones you have to work with) that prints out the correct number of stars and spaces.








give the 10 points to that guy steve up above me!


No comments:

Post a Comment