GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Aug-2008, 10:58
lief480's Avatar
lief480 lief480 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
lief480 is on a distinguished road

Need help on a program for school


Hello thank you in advance to anyone who can help me!

Here is the assignment:

"Write a program that uses for statements to print the following patterns seperately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form cout << '*';
(this causes the asterisks to print side by side.) [Hint: The last two patterns require that each line begin with an appropriate number of blanks.]
CPP / C++ / C Code:
Patterns:
(a)
*                      
**                    
***                 
****                
*****             
******          
*******       
********     
*********    
**********
(b)
**********
*********
********
*******
******
*****
****
***
**
*
(c)
**********
 *********   
  ********
   *******
    ******       
     *****
      ****
       ***
        **
         *
(d)
         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********
As i said all help is appreciated!
Edit:Ohh by the way im using Microsoft Visual C++ 6.0.
  #2  
Old 23-Aug-2008, 11:08
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Need help on a program for school


Quote:
Originally Posted by lief480
Hello thank you in advance to anyone who can help me!
The value of sites like this is to help you help yourself. If we simply provide the final solution, you will have learned little to nothing.

So here is a hint. Given that the patterns provided show various ascending & descending patterns, you may want to experiment with various ascending & descending for-loops.

If you have a specific question about syntax or are unclear about particular concepts, post what code you have completed to establish a basis for further discussion.
  #3  
Old 23-Aug-2008, 11:49
lief480's Avatar
lief480 lief480 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
lief480 is on a distinguished road

Re: Need help on a program for school


Hi,
The thing is i have no idea how to even start writing this program. I know this is a forum to help me help myself but i have no idea where to start. I just started programming in c++ this summer and have only written a couple of simple programs. So if you can help me it would be very useful for me. Thank you!!
  #4  
Old 23-Aug-2008, 12:11
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Need help on a program for school


Quote:
Originally Posted by lief480
So if you can help me it would be very useful for me.
Various comments:
  • The first thing you need to do is figure out the patterns found in the output. Looking at pattern a, what is the relationship between each line & number of asterisks printed? Hint: assign a number to each line. Given this information, how many asterisks should be printed on the fourth line?
  • Use one for-loop to count through the lines seen in output.
  • Recognize that the output displayed for each line is variable, however, this variable output has a relation to the line number.
  • How can you output a varying length of asterisks which appears to be based on the line number?
The next thing you should do is simply output the correct number of lines irregardless of the varying number of asterisks. In particular, post the code needed to output the constant string "abc" on 10 separate lines. Be sure to place [cpp] before your code, & [/cpp] afterwards to format the code for easier reading.

We will use that as the basis for further discussion.
  #5  
Old 23-Aug-2008, 16:04
lief480's Avatar
lief480 lief480 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
lief480 is on a distinguished road

Re: Need help on a program for school


Here is the program printing "abc" as you asked for:
CPP / C++ / C Code:
// Print "abc down the line 10 times

#include<iostream>

using namespace std;

int main()
{
	for ( int counter = 1; counter <=10; counter++ ) {
		cout << "abc" << endl;
	}

	return 0;
}

Also here is somthing I was experimenting with. The only problem it isnt printing the astrisks only blank spaces.
CPP / C++ / C Code:
// Test
//Jon H.
 
#include<iostream>

using namespace std;

int main()
{
	for( int counter=0;counter <= 10;counter++ ){
		for( int linenum= 0;linenum!=0;linenum++ )
			cout<<"*";
		cout<<endl;

	}
	return 0;
}
  #6  
Old 23-Aug-2008, 16:20
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Need help on a program for school


Quote:
Originally Posted by lief480
Here is the program printing "abc" as you asked for:
Okay, this is a start. You are displaying strings of constant length on each line.
  • Now, change your code such that it outputs only a single asterisk per line.
  • What is the relationship between the line number & the number of asterisks displayed? How might you write code to implement this relationship?
  #7  
Old 23-Aug-2008, 16:29
lief480's Avatar
lief480 lief480 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
lief480 is on a distinguished road

Re: Need help on a program for school


Alright
Quote:
  • Now, change your code such that it outputs only a single asterisk per line.
All you would have to do for this part would be to change "abc" to '*'

Quote:
  • What is the relationship between the line number & the number of asterisks displayed? How might you write code to implement this relationship?
For example with pattern a, for every line one more "*" is being added
  #8  
Old 23-Aug-2008, 16:36
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Need help on a program for school


Quote:
Originally Posted by lief480
All you would have to do for this part would be to change "abc" to '*'
Correct! Now change your code accordingly.
Quote:
For example with pattern a, for every line one more "*" is being added
So write a for-loop which will output three asterisks per line & place this for-loop inside the one already present in your code. I leave it to you to figure out where to output the newline. This is not the final solution for pattern a, but you are building the solution incrementally.
  #9  
Old 23-Aug-2008, 17:08
lief480's Avatar
lief480 lief480 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
lief480 is on a distinguished road

Re: Need help on a program for school


i kinda dont understand what to do. But this is what i tried to do from what you said and i don't think its what you want me to do.
CPP / C++ / C Code:
#include<iostream>

using namespace std;

int main()
{
	for( int counter=0;counter <= 10;counter++ ){
		cout << '*' << endl;
	        for( int counter=0;counter <= 10;counter++ )
                     cout << "***" << endl;
        }
	return 0;
}
  #10  
Old 23-Aug-2008, 17:29
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Need help on a program for school


Quote:
Originally Posted by lief480
Also here is somthing I was experimenting with. The only problem it isnt printing the astrisks only blank spaces.
CPP / C++ / C Code:
// Test
//Jon H.
 
#include<iostream>

using namespace std;

int main()
{
	for( int counter=0;counter <= 10;counter++ ){
		for( int linenum= 0;linenum!=0;linenum++ )
			cout<<"*";
		cout<<endl;

	}
	return 0;
}
I didn't see this earlier. This is very close to the answer for pattern a.

The inner for-loop is doing just as you prescribed. Note that the test condition linenum!=0 is checked after incrementing is done. Therefore linenum is always not equal to zero. The body of the inner for-loop is never executed.

Note also that your outer for-loop begins at 0 & continues until counter <= 10. In other words, you will be displaying 11 lines. C++ (as is C...) are predicated on 0-based counting. Get used to the following as counting up to ten:
CPP / C++ / C Code:
for (int i = 0; i < 10; i++) {
    // whatever...
}
FWIW.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation solver RazoR C Programming Language 3 18-May-2008 10:24
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 04:48
Help with a complex program lordfuoco C++ Forum 5 24-Jun-2006 07:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:42.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.