Write a program that accepts a number and produces a pattern as shown in the
following sample outputs.

Line = 3
*.* .* *

Line = 8
.*.*.*.*
*.*.*.*
.*.*.*
*.*.*
.*.*
*.*
.*

i've just learnt to use the setw() command.....
but how do you fill up the alternate spaces with a different item as shown above?

appreciate any help rendered.
:cheesy:

Recommended Answers

All 13 Replies

Are you sure you have got the pattern right, coz it seems a bit..weird.

If your question is related to drawing [search]pascal triangle[/search] like structure you can search for it in the code snippets section by clicking on the keyword above.

Hmm... so post what you have attempted so far and I will try to see what can be done .

yup its correct....coz the previous few qns in my assignment are quite similar...just that the diplays consists of only *

just to correct my first qn, the output should be:

*.*
.*
*

sorry......

The codefor a previous qn:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int n,count;

    cout << "Input an integer: \n";
    cin >> n;
    

    for (count=0;count<=n+1;count++)
        cout << setw(count) << setfill('*') << " " << endl;
    

}

and heres the output:
For n = 3
*
**
***

to get this

*
**
***

you "counted up". How do you think you might get this?

***
**
*

okay...here goes:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int n,count;

    cout << "Input an integer: \n";
    cin >> n;
    
    for (count=n+1;count>=0;count--)
        cout << setw(count) << setfill('*') << " " << endl;
    
}

correct?

Hmm.. just to put you on the right path i am going to give a crude example of how it can be done using simple nested loops. Just understand the logic and implement it accordingly.

int main()
{
    int n = 0, count = 0;
    char first_char = '*', second_char = '+' ;

    cout << "Input an integer: ";
    cin >> n;

    for (count = n + 1; count > 1 ; count--)
    {
        for (int inner = count; inner > 1 ; inner--)
        {
            cout << ((inner % 2) ? second_char : first_char) ;
        }
        putchar('\n') ;
    }
}

Hope it helped, bye.

wow....thanks....
would never have though of this........
need lots more practice......lol

thanks..really appreciate it :)

Member Avatar for iamthwee

>cout << ((inner % 2) ? second_char : first_char) ;

Ha ha, I can't wait till he hands this in then his instructor ask him what this line does, whereupon he blankly looks up into space.

just to put you on the right path i am going to give a crude example of how it can be done using simple nested loops. Just understand the logic and implement it accordingly.

But it was not like i didnt warn him :D

Member Avatar for iamthwee

Sometimes it's difficult to decide how much help to give someone.

Would he have eventually used a nested for loop if left to his own devices?

Sometimes you have to fail to truly understand what went wrong. A-ha ha

Hmm.. I agree. Sad but true.

>cout << ((inner % 2) ? second_char : first_char) ;

Ha ha, I can't wait till he hands this in then his instructor ask him what this line does, whereupon he blankly looks up into space.

lol......

if (inner%2)
print second_char
else
print first_char


hope its correct? ;)
lol


i understand that its nice of you guys to help.
but i wouldnt want spoon feeding either....defeats the purpose of learning right?
its nice that in this forum, most help rendered is in the form of hints......
thanks for the help guys, appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.