.knm

scotty (15)
.mn
Last edited on
Chervil (1206)
Why not post the questions in the forum.

Actually, I don't think you "need the c++ code for them". What you need is to know how to write the C++ code for them.
Last edited on
JLBorges (1756)
Let's start with this:

1) Write a program that asks a user to input a positive integer the produces the following output from the input. For example, if the user enters 5 the following triangle should be drawn.

*
**
***
****
*****


Step 1. Write a for loop, which prints all the numbers from 1 to n
Post the code for that, and we can then move on to Setep 2.
scotty (15)
.
Last edited on
JLBorges (1756)
1
2
3
// void main() // *** no
int main() // main() returns an int
{


The loop should print all the numbers from 1 to n and nothing else.
So, if n is 6, your loop should print 1 2 3 4 5 6 - nothing more, nothing less.

Give it another try.
scotty (15)
.
Last edited on
JLBorges (1756)
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
    int n = 6 ;

    // print numbers 1 to n
    for( int i = 1 ; i <= n ; ++i ) std::cout << i ;

    std::cout << '\n' ; // and print a new line at the end
}


Now modify this program so that it prints n asterisks.

If n is 8, the program should print ********

Write it, compile it, and run it. And after that, post the code.
Last edited on
Fourc00h (7)
God damn I just had a shot at this, took me a while, I had the new line inside the nested for loop so it was just showing

*
*
*
*
*

Finally got it!
scotty (15)
.
Last edited on
JLBorges (1756)
> would you change the 1 after cout<< to an *

It is not 1, it is i.
And yes, that i would be changed to a '*' (within quotes).


> I don't have the software until tomorrow

Ok, it would be best to wait till you have a C++ compiler, and then you can actually try things out.
Fourc00h (7)
Why don't you download Visual Studio or DevC++ ? they're free

What I did to debug it was to add i's number in the loop to see where it stops moving ahead, like cout<<i<<". "<<j;

So it would print something like this out:

1. 0
2. 0
2. 1
3. 0
3. 1
3. 2

So on..
scotty (15)
.
Last edited on
Fourc00h (7)
Ok, when you have time tomorrow download gcc

Here's a guide that may help you out:

http://www.mkyong.com/mac/how-to-install-gcc-compiler-on-mac-os-x/
JLBorges (1756)
> I have a mac, and I haven't been able to fine anything free to download.

Xcode is free: http://developer.apple.com/xcode/

If you prefer a pure C++ IDE: http://codelite.org/LiteEditor/Download
Last edited on
scotty (15)
#include <iostream>

int main()
{
int n = 6 ;

// print numbers 1 to n
for( int i = 1 ; i <= n ; ++i ) std::cout << "*" ;

std::cout << '\n' ; // and print a new line at the end
}

so from here what would be the next step?
JLBorges (1756)
> so from here what would be the next step?

Now write two loops; with the inner one nested inside the outer loop.
In the outer loop, vary n from 1 to 6.
For each value of n in the outer loop, print a line containing n asterisks.
scotty (15)
#include <iostream>

intmain()
{
int n= 6;

// print numbers 1 to n

for (int T = 1; T <= num_entered; T++) std::cout<<"*" ;
{
for (int Y = 0; Y < T; Y++)
{

how would i finish this off?
JLBorges (1756)
Keep the inner loop and the line following it just the same as the earlier program.

Write an outer loop varying n from one to 6 around it

1
2
3
4
5
6
7
8
9
// TODO: write the outer loop here 
{
    // with this as the code to be executed each time through the loop 

    // print n asterisks
    for( int i = 1 ; i <= n ; ++i ) std::cout << "*" ;

    std::cout << '\n' ; // and print a new line at the end
}


vlad from moscow (3662)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <iomanip>
 
int main()
{
        
    while ( true )
    {
        std::cout << "Enter a non-negative number: ";

        unsigned int n = 0;
        std::cin >> n;
            
        if ( !n ) break;
            
        const char c = '*';
            
        std::cout.fill( c );

        std::cout << std::endl;
        for ( unsigned int i = 0; i < n; i++ ) std::cout << std::setw( i + 1 ) << c << std::endl;
        std::cout << std::endl;
    }
        
    return 0;
    
}
Last edited on
vlad from moscow (3662)
Another possible code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
 
int main()
{
        
    while ( true )
    {
        std::cout << "Enter a non-negative number: ";

        unsigned int n = 0;
        std::cin >> n;
            
        if ( !n ) break;
            
        std::string s;
        s.reserve( n );
    
        std::cout << std::endl;
        for ( unsigned int i = 0; i < n; i++ )
        {
            const char c = '*'; 
            s += c;        
            std::cout << s << std::endl;
        }
        std::cout << std::endl;
    }
        
    return 0;
    
}
Registered users can post here. Sign in or register to post.