-
int main()
-
{
-
int i, j, k, l, m, n, o, p, q, r;
-
-
for (i = 1; i < 10; i++)
-
{ for (j = 1; j < (i=1); j++)
-
{cout << "*";}
-
cout << endl;}
-
for (k = 10; k > 1; k--)
-
{ for (l = k; l < (k-1); l--)
-
{cout << "*";}
-
cout << endl; }
-
for (m = 1; m < 10; m++)
-
{ for (n = m; n < (m-1); m++
-
{ cout << "*";}
-
cout << endl; }
-
-
return 0;
-
}
-
Firstly Formating
If you format the layout of your code well it will be easier to read and maintain. Of particularly importance is putting you braces {} in a sensible place.
2 common methods are opening brace { on the same line of code that starts the new code block, closing brace } on a separate line like this
-
if (someCondition) {
-
// Some code here
-
}
-
and opening and closing brace on separate lines like this
-
if (someCondition)
-
{
-
// Some code here
-
}
-
Code inside the code clocks should be indented 1 level
Personally I like the second form.
Compile this and you get lots of errors on cout, you need to include the right header, iostream, and you need to use the right namespace.
Doing these 2 things to your code you get
-
#include <iostream>
-
-
using namespace std;
-
-
int main()
-
{
-
int i, j, k, l, m, n, o, p, q, r;
-
-
for (i = 1; i < 10; i++)
-
{
-
for (j = 1; j < (i=1); j++)
-
{
-
cout << "*";
-
}
-
-
cout << endl;
-
}
-
-
for (k = 10; k > 1; k--)
-
{
-
for (l = k; l < (k-1); l--)
-
{
-
cout << "*";
-
}
-
-
cout << endl;
-
}
-
-
for (m = 1; m < 10; m++)
-
{
-
for (n = m; n < (m-1); m++
-
{
-
cout << "*";
-
}
-
-
cout << endl;
-
}
-
-
return 0;
-
}
-
If you compile this you get a single error because you have left a ) of the end of the last for statement.
However this does not result in a working program.
There is a logic error in the inner for loop of the first nested loop pair. The end condition for the loop is (i=1) what this means is that everytime this is execute i is reset to a value of 1 and this in turn means that the outter loop, dependent on i never reaches its end condition so you have an infinite loop. I guess you meant (i-1).
There is a logic error in the inner for loop of the second nested loop pair
for (l = k; l < (k-1); l--)
if you set l to the value of k it will never be < (k-1) because k < (k-1) is always false, I think you meant l = 10 as your initialiser.
There are 2 logic errors in the inner for loop of the third nested loop pair
for (n = m; n < (m-1); m++)
if you set n to the value of m it will never be < (m-1) because m < (m-1) is always false, I think you meant n = 1 as your initialiser.
Additionally you have m++ as the 3rd expression of the for loop, since n is never changed if the loop condition is true the loop will execute infinately, I think you meant n++.
making all these changes gives
-
#include <iostream>
-
-
using namespace std;
-
-
int main()
-
{
-
int i, j, k, l, m, n, o, p, q, r;
-
-
for (i = 1; i < 10; i++)
-
{
-
for (j = 1; j < (i-1); j++)
-
{
-
cout << "*";
-
}
-
-
cout << endl;
-
}
-
-
for (k = 10; k > 1; k--)
-
{
-
for (l = 10; l > (k-1); l--)
-
{
-
cout << "*";
-
}
-
-
cout << endl;
-
}
-
-
for (m = 1; m < 10; m++)
-
{
-
for (n = 1; n < (m-1); n++)
-
{
-
cout << "*";
-
}
-
-
cout << endl;
-
}
-
-
return 0;
-
}
-
which produces the output
-
-
-
*
-
**
-
***
-
****
-
*****
-
******
-
*******
-
*
-
**
-
***
-
****
-
*****
-
******
-
*******
-
********
-
*********
-
-
-
*
-
**
-
***
-
****
-
*****
-
******
-
*******
I am not sure what output you wanted (you don't say) you indicate that you wish to space the output triangles but you have now code statements outputing space characters.