Please run this

i am supposed to come with a output
***
****
*****
******
*****
****
***
for entering 3 and 6 as input but i am having problems with my output it seems as if i have an infinite for loop some where can some 1 help me out thank you
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 

 #include <iostream>
using namespace std;
    
void Triangle(int x, int y)
 {
    
 
  if (x >= y) 
  {
     

         for(int i =1;i<=y;i++)
         {  
              cout << '*';
           }
cout<<endl;

y--;
       Triangle(x, y);
       }
      
    else if (x<y)
    {

           for (int i = 1; i <= x; i++)
           
 {  
              cout << '*';
           }
    cout << endl;
    x++;

    Triangle(x, y); //recursive function
}

}
int main()
{
    int x,y;
    
cin>>x;
cin>>y;
Triangle(x,y);
system("pause");
return 0;
}

ive found a way 2 do it but i need a way to keep tempx the same throught the program so it doesnt change.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

 
 #include <iostream>
using namespace std;
    
void Triangle(int x, int y)
 {
    
    
    const int tempx = x;
        const int tempy= y;
     
 if(x==0||y==0)
 return;
 if(y<tempx)
 return;
  if (x >= y) 
  {
     
         for(int i =1;i<=y;i++)
         {  
              cout << '*';
           }
           
cout<<endl;
y--;
       Triangle(x, y);
       }
      
    else if (x<y)
    {
           for (int i = 1; i <= x; i++)
           
 {  
              cout << '*';
           }
    cout << endl;
    x++;
    Triangle(x, y); //recursive function
}
}
int main()
{
    int x,y;
    
cin>>x;
cin>>y;
Triangle(x,y);
system("pause");
return 0;
}

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>

void triangle(int x, int y);

int main()
{
    int x = 0, y = 0;
    
    std::cout << "Enter lowest amount: ";
    std::cin >> x;
    std::cin.ignore();
    
    std::cout << "Enter highest amount: ";
    std::cin >> y;
    std::cin.ignore();
    
    triangle(x, y);
    
    std::cin.get();
    return 0;
}

void triangle(int x, int y)
{
   const int lower = x;
   const int upper = y;
   
   for(upper; upper>=x; x++)  
     for(int i=0; i<x; i++) {
       if(i==x-1) {
         std::cout <<"*\n";
         i = 0;
         break;
       }
       std::cout << '*'; 
     }  
   
   x = upper -1;
   
   for(lower; lower<=x; x--)
     for(int i=0; i<x; i++) {
       if(i==x-1) {
         std::cout << "*\n";
         i = 0;
         break;
       }
       std::cout << '*';
     }      
     
   return;
}
Looks awfully complicated.

With that method, there should not be any need for if() statements inside the loops.

1
2
3
4
5
6
7
void Triangle( int small, int large ) {
    for( int i = small; i <= large; ++i )
        std::cout << std::string( i, '*' ) << std::endl;

    for( int i = large - 1; i >= small; --i )
        std::cout << std::string( i, '*' ) << std::endl;
}


Now all you need to do is write a loop that outputs N asterisks followed by a newline
(to replace my std::string( i, '*' ), which constructs a string of i asterisks.)
Last edited on
I was thinking that my program was a bit over the top, and that it probably could be shortend but my mind went a blank!

To be fair though, I didn't even know your use of std::string was possible. Edit* Yes i have just checked the documentation before you tell me its there! :(
Last edited on
WOW! The difference in speed of these two programs is incredible :O
Topic archived. No new replies allowed.