To illustrate:
Like printing an inverted triangle.
* * * *
* * *
* *
*
The first loop is the number of rows, the second loop is the number of * printed per row, and as the number of rows changes, the number of printed * varies.
This is the double loop of associative variables. My approach is to first design the first layer variable I=4;i>=1;i--, This ensures that 4 rows of rows are printed correctly.
Then, design the second layer variable j=i,j>=1,j--, so that J can accompany the number of changes, and print a different number of stars. The third layer, tied to the second layer, is used for output spaces.
For example: The first line i=4,j=4, so print 4 * number, the second line i=3, then j=4, then print 3 * number, and so on, and so on, and finally printed in addition to the inverted star chart.
The complete procedure is as follows:
#include <stdio.h>
int main (void)
{
for (int i=4;i>=1;i--) {
for (int j=i;j>=1;j--) {
printf ("*");
}
printf ("\ n");
for (int k=i-1;k<4;k++) {
printf ("");
}
}
printf ("\ n");
return 0;
}
Pay attention to the judgment of the greater than and less than sign in the condition. In addition: If k<=4, then 2 spaces will be output in the second line, resulting in asymmetry.
Another example: Ten turn octal
/* Decimal to octal normal edition.
* Note the use of the two-tier for loop for this associative variable
*
*/
#include <stdio.h>
int main (void)
{
int number;
int jinzhi=8;
int weishu=5;
printf ("Enter a number (5wei):");
scanf ("%d", &number);
printf ("%o\n", number);
for (int i=weishu;i>=1;i--) {
int temp=number;
for (int j=i-1;j>=1;j--) {
Temp/=jinzhi;
}
Temp%=jinzhi;
printf ("%d", temp);
}
printf ("\ n");
return 0;
}
Linux C: Double for loop for associative variables