I have a part of code that cannot show the result as shown below:

patter#1
*****
****
***
**
*
pattern2
*
**
***
****
*****
pattern3
*****
****
***
**
*

Any idea to solve the incorrect code below:

#include <iostream.h>
using namespace std;
int main(){
int i=0 ;
int j=0 ;
for(i=0;i<5;i++){
for(j=0;j<1;j++){
cout<<" ";
}
for(j=0;j<5;j++){
cout<<"*";
cout<<endl;
}
}


i=0;
j=0;
for(i=0;i<5;i++){
for(j=i;j<4;j++){
cout<<" ";
}
for(j=0;j<1;j++){
cout<<"*";
}
cout<<endl;
}


i=0;
j=0;
for(j=0;j<5;j++)
for(i=0;i<4;i++){
cout<<" ";
}
for(i=0;i<1;i++){
cout<<"*";
}
cout<<endl;
system("pause");
}

Dani AI

Generated

The output problems come from three common mistakes in the original code: a newline placed inside the inner star loop (so each star prints on its own line), incorrect loop bounds that only ever print a single star, and missing braces/poor indentation that change which statements belong to which loop. s original loops show all three issues; correctly pointed out the bad loop design and suggested breaking patterns into functions, and suggested simplifying the control flow. A clear mental model: for each row compute how many stars to print, print those stars, then print a single newline.

A compact, robust approach is to compute the star count per row and print that many characters in one operation. The following example uses std::string to build each line (this is different from the code already posted):

#include <iostream>
#include <string>

void print_triangle(int rows, bool increasing) {
    for (int r = 0; r < rows; ++r) {
        int stars = increasing ? r + 1 : (rows - r);
        std::cout << std::string(stars, '*') << '\n';
    }
}

int main() {
    print_triangle(5, false); // pattern1
    std::cout << '\n';
    print_triangle(5, true);  // pattern2
    std::cout << '\n';
    print_triangle(5, false); // pattern3
}

Practical tips: always place the newline after the inner loop (not inside it); use braces around loop bodies to avoid accidental mis-association; prefer '\n' over std::endl unless you need an immediate flush; avoid system("pause") for portability (as noted); and choose clear variable names or small helper functions to make the intent obvious. These changes make the patterns easy to generalize (different sizes, different characters) and remove the off-by-one/loop-scoping bugs that caused the original incorrect output.

Recommended Answers

All 4 Replies

Try to simplify your algorithms. Remember that you can combine loop types. This is often useful ;) . Also don't forget the code tags when you post code, because with them it's much more readable. One possible solution I paste below. Enjoy.

#include <iostream>

using namespace std;

int main(void)
{
    short count=5;
    
    while(count) {
                 for(int i=count; i!=0; i--)
                         cout<<'*';
                 cout << endl;
                 --count;
    }
    
    cout << endl;
    count = 1;
    
    while(count < 6) {
                for(short i=0;i!=count;i++) 
                        cout << '*';
                
                cout << endl;
                ++count;
    }
    
    return 0;
}

Your main probleme is that you have bad design. Try to figure out how to do somthing before you try to code it :).
But, I rewritten your code:

#include <iostream> // use <iostream> instead for <iostream.h>
using namespace std;
int main()
{
    int i=0 ;
    int j=0 ;
    for(i=0;i<5;i++)
    {
        for(j=i;j<5;j++){ // j = 0 changed to j = i
            cout<<"*";
        }
        cout<<endl;//you dont want a newline after every *
    }
    
    for(i=0;i<5;i++)
    {
        for(j=0;j<i+1;j++)//  j < 1 changed to j < i+1 
        {
            cout<<"*";
        }
        cout<<endl;
    }

    for(j=0;j<5;j++)
    {

        for(i=j;i<5;i++) //  i=0;i<1 changed to i=j;i<5
        {
            cout<<"*";
        }
        cout<<endl;
    }   
    
    system("pause");
}

but I recomand you to use function, makes the code look mutch more clean. (im mine eyes at least)
somthing like:

#include <iostream>
using namespace std;

void patern1 ()
{
    for(int j = 0; j < 5; j ++) 
    {
         for (int i = j; i < 5; i++)
         {
             cout << '*';
         }
         cout << endl;         
    }
}

void patern2 ()
{
    for(int j = 0; j < 5; j ++) 
    {
        for (int i = 0; i < j+1; i++)
        {
            cout << '*';
        }
        cout << endl;        
    }     
}

int main()
{
    patern1();
    patern2();
    patern1();
    system("pause");
}

I would recommend that you don't use a system("pause").

I only recommanded that he used function, the system("pause ") was only becuse mixsir used it in his code.

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.