3 Questions - basic c++

scotty (14)
can anyone help me - if I personal message 3 questions i'm struggling with. Need the c++ code for them..
Chervil (1203)
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 (1752)
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 (14)
void main()
{
for(int count=0; count<=n; count++)
{
cout<<"Enter a positive number"<<cout+1 <<"pass."<<endl;
}
}
JLBorges (1752)
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 (14)
{
int n=1;
cout<<i;
while (i<n)
{
i++
}
}
JLBorges (1752)
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 (14)
would you change the 1 after cout<< to an *

I don't have the software until tomorrow, so am currently doing everything by hand which is why i'm getting quite confused.
JLBorges (1752)
> 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 (14)
sorry, that is what I meant.

I don't really have time, but I will try it all tomorrow.

I have a mac, and I haven't been able to fine anything free to download.
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 (1752)
> 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 (14)
#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 (1752)
> 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 (14)
#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 (1752)
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
}


Registered users can post here. Sign in or register to post.