Can someone help me with writing C++ program using loop for the following fragment..
I tried damn hard to do so, but nothing i achieved.

The fragment looks like this:

            &
          &   &
        &       &
      &           &
    & & & & & & & & &

Recommended Answers

All 8 Replies

Post your hard work, I'll give you a hand finding the problem.
but am I wrong to assume you have nothing to post?

first you show some code then only we can help you

commented: Chairman of the Department of Redundancy Department -3

Consider the algorithm you need to apply to achieve this. Express the steps in natural language first, to get it straight in your mind. When you think you have that sorted, try coding the steps. If you can't get your code to work, post it here.

You know you need a main() function. You know you need a loop of some kind. You know that inside the loop you need some form of output statement.

If you look at the desired output, some lines have different characteristics from others, so you need to cater for this in your output statement(s).

You cant just use gotoxy(num, anothernum); and increment or decrement them in the for(int i) loop??

You cant just use gotoxy(num, anothernum); and increment or decrement them in the for(int i) loop??

What if gotoxy() isn't supported by the compiler?

Try to make a general formula for printing 'spaces' and '&'.

For example :

  1. The number of spaces that are to be printed in 1st line (outside the triangle) is one less than the number of lines given by user. It decreases by 1 as we move from one line to another.

Divide your task, which means, keep one loop that increments line, one that prints spaces outside the triangle, one that prints inside the triangle, and one that prints &

Also here is a hint : Try to print last line separately, that is outside loops that prints the upper part ( in your example, lines (1 - 4) means upper part, so lines (1 - 4) and 5 to be done separately ).

Try this and post your work.
Hope this helps :-)

do

#include <conio.h>
#include <windows.h>

This is what I did for my Tic-Tac-Toe Game in C++. These 2 lines helped me create gotoxy and resize screen with

system("mode num,another_num");

do
#include <conio.h>
#include <windows.h>

The rest of the world may not use the same compiler and OS as you do. Both conio.h and windows.h are dependent on the implementation. Further, this problem can be solved without resorting to non-portable libraries. You don't have to call gotoxy() when it's a simple matter of printing a few space characters.

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.