Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Homework
Hi I'm in a need to write a code which print asterisks in such a way that 1 asterisk for first line, 2 for second line, 3 for third line etc.
Know I'm fully frustrated after trying a lot of times.
 
Please have an eye. . . . .
#include <iostream>

using namespace std;
 
int main()
{
    int n = 5;
    int row = 0;
    int colum = 0;
    while(row<n)
    {
        
        while(colum<row)
        {
            cout << "*" << '\n';
            colum++;
        }
        row++;
    }
    return 0;
}
Posted 1-Jan-13 10:09am
ctype.h1.7K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I don't normally do people's homework, but you've given it a shot, most people just ask us to do it. You've made two mistakes:
 
1 - you emit a newline after every *, you need to emit a newline after each group of *s
2 - you don't reset colum to 0, so your rows never get any bigger.
 
I recommend learning to use your debugger, that should have helped you find both of these issues. I don't know what compiler you're using, but they all suppor breakpoints AFAIK.
 
#include <iostream>

using namespace std;
 
int main()
{
    int n = 5;
    int row = 0;
    int colum = 0;
    while(row<n)>
    {
        
        while(colum<row)>
        {
            cout << "*";
            colum++;
        }
 
		cout << "\n";
		colum = 0;
        row++;
    }
    return 0;
}</iostream>
  Permalink  
Comments
Scenic Design at 1-Jan-13 15:18pm
   
Thank you Sir .. I'm using code blocks. Sir please recommend me some more learning stuff about that type of problems. . .
Christian Graus at 1-Jan-13 15:20pm
   
I have no idea what code blocks is. It's a compiler ? If you want to learn C++, I recommend buying 'The C++ programming language' by Stroustrup, and working through the exercises.
Scenic Design at 1-Jan-13 15:34pm
   
Yes sir its' an open source Compiler .. what about c++ for everyone by Horst Man?
Christian Graus at 1-Jan-13 15:43pm
   
I don't know it, but I'm sure it's fine, books are expensive, if you have one, work through that one. The one I recommended is from the guy who created the language, if you can get it, I would.
Scenic Design at 1-Jan-13 15:47pm
   
Gotcha ..
Thank u sir for your consideration ...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Inside the outer while loop you need to reset the value of column. Otherwise it only goes through the inner while the first time.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 245
1 Kamal Rocks 184
2 Sergey Alexandrovich Kryukov 175
3 BillWoodruff 173
4 PIEBALDconsult 160
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100