Chapter V program design of circular structure

catalogue

Concept blank

5

6

Single choice questions

14

15

Reading program questions

1

2

 3

4

5

7

9

10

11

12

13

14

16

17

18

19

21

Perfect program questions

1

2

3

 4

10

11

12

Concept blank

5

code

#include<stdio.h>
int main()
{
	int k, x;
	for (k = 0, x = 0; k <= 9 && x != 10; k++) {
		x = x + 2;
	}
	printf("%d,%d", k, x);
	return 0;
}

Text description method

1. Define the integer k,x;
2.k=0,x=0
3. Judge K < = 9&&x= 10. If K < = 9 & &x= 10. Execute 3, 4 and 5, otherwise turn to 6;
4.x=x+2
5.k + + to 3
5. Output the value of k,x

flow chart

Operation results

6

code  

#include<stdio.h>
int main()
{
	char c;
	for (c = getchar(); getchar() != '#'; c = getchar())
		putchar(c);
	return 0;
}

Text description method

1. Define character c
2.c = getchar()
3. Judge getchar()! = '#', If getchar()! = '#', Execute 3,4,5, otherwise turn to 6
4.putchar(c)
5.c = getchar()
6. Output c

flow chart

Operation results

Single choice questions

14

code  

#include<stdio.h>
int main()
{
	char ch;
	for (; (ch = getchar()) != '\n';) {
		printf("%c", ch);
	}
	return 0;
}

Text description method

1. Define the character ch
2. Judge ch = getchar())! = '\ N ', if ch = getchar())! ='\ N ', execute 2, 3, otherwise turn to 3
3. Output ch
flow chart

  Operation results

15

code

#include<stdio.h>
int main()
{
	int i, a = 1;
	unsigned j;
	for (i=15;i>0;i-=2)
		a++;
	printf("output a=%d", a);
	return 0;
}

  Text description method

1. Define int i, a = 1;unsigned j;
2.i=15
3. Judge I > 0. If I < 0, execute 3 and 4, otherwise turn to 5
4.a + + to 3
5. Output a

flow chart

Operation results

Reading program questions

1

code

#include<stdio.h>
int main()
{
	int i;
	for (i = 1; i < 6; i++) {
		if (i % 2) {
			printf("*");
		}
		else {
			printf("#");
		}
	}
	return 0;
}

  Text description method

1. Define int i
2.i=1
3 judge I < 6, if I < 6, execute 3, 4 and 5, otherwise turn to 6
4. If i%2, output*
5. Otherwise output#
6. Conclusion

flow chart

Operation results

2

code

#include<stdio.h>
int main()
{
	int m = 1, n, i;
	for (i = 0; i < 5; i++) {
		int m = 1;
		m++;
		if (i == 4) {
			n = m;
		}
	}
	printf("%d,%d", m, n);
	return 0;
}

  Text description method

1. Definition: m=1,n,i
2.i=0
3. Judge I < 5. If I < 5, execute 3,4,5,6, otherwise turn to 7
4. Integer m=1
5.m++
6. If i=4, n=m
7. Output m, n

flow chart

Operation results

 3

code

#include<stdio.h>
int main()
{
	int i;
	for (i = 'a'; i < 'f'; i++, i++) {
		printf("%c", i - 'a' + 'A');
	}
	return 0;
}

Text description method

1. Define integer i
2.i='a'
3. Judge I <'f '. If I <'f', execute 3, 4 and 5, otherwise turn to 6
4. Output the value of I -'a '+'a'
5.i++,i++
6. Conclusion

flow chart

Operation results

4

code

#include<stdio.h>
int main()
{
	int f, f1, f2, i;
	f1 = 1; f2 = 1;
	printf("%2d %2d", f1, f2);
	for (i = 3; i <= 5; i++) {
		f = f1 + f2;
		printf("%2d", f);
		f1 = f2;
		f2 = f;
	}
	return 0;
}

  Text description method

1. Define the integers f,f1,f2,i
2.f1=1,f2=1
3. Enter the values of F1 and F2
4.i=3
5. Judge I < = 5, if I < = 5, execute 5, 6, 7, 8, 9, otherwise turn to 10
6.f=f1+f2
7. Enter the value of f
8.f1=f2;f2=f
9.i++
10. End

flow chart

Operation results

5

code

#include<stdio.h>
int main()
{
	int n = 5;
	do
	{
		switch (n % 2) {
		case 0:
			n--;
			break;
		case 1:
			n--;
			continue;
	    }
		n--;
		printf("%2d", n);
	} while (n > 0);
		return 0;
}

  Text description method

1. Define integer n=5
2. When n%2
3.case0: n --, turn 7
4.case1:n -- execute 2, 3, 5, 6, 7
5.n--
6. Output the value of n
7. Judge n > 0. If n > 0, execute 2, 3, 4, 5, 6 and 7, otherwise turn to 8

8. Conclusion

  flow chart

Operation results

7

code

#include<stdio.h>
int main()
{
	int x = 10;
	while (x--);
	printf("x=%d\n", x);
	return 0;
}

  Text description method

1. Define integer x=10
2. Calculate x--
3. Output the value of x

flow chart

Operation results

9

code

#include<stdio.h>
int main()
{
	int m = 9;
	for (; m > 0; m--) {
		if (m % 3 == 0) {
			printf("%d", --m);
		}
	}
	return 0;
}

  Text description method


1. Define integer m=9
2. Judge m > 0. If M < 0, execute 2, 3 and 4, otherwise turn to 5
3. If m%3==0
4. Output the value of -- m, otherwise turn to 5
5. End
flow chart

Operation results

10

code

#include<stdio.h>
int main()
{
	int x = 8;
	for (; x > 0; x--) {
		if (x % 3 == 0) {
			printf("%d,", x--);
			continue;
		}
		printf("%d", --x);
	}
	return 0;
}

  Text description method

1. Define integer x=8
2. Judge x > 0. If x < 0, execute 2, 3 and 4, otherwise turn to 5
3. If x%3==0
4. Output the value of x -- to 2, 3 and 5
5. Output the value of --x

flow chart

Operation results

11

code

#include<stdio.h>
int main()
{
	int x = 3;
	do {
		printf("%3d", x = x - 3);
	} while (!x);
	return 0;
}

  Text description method

1. Define integer x=3
2. Output x=x-3
3. Judgment! x. If! X execute 2, 3, otherwise turn 4
4. End

flow chart

Operation results

12

code

#include<stdio.h>
int main()
{
	int x = 2;
	do {
		printf("%3d", !x - 2);
	} while (--x);
	return 0;
}

  Text description method

1. Define integer x=2
2. Output! x-2
3. Judge -- x, if -- x executes 2, 3, otherwise turn to 4
4. End

flow chart

Operation results

13

code

#include<stdio.h>
int main()
{
	int n = 12345, d;
	while (n != 0) {
		d = n % 10;
		printf("%d", d);
		n = n / 10;
	}
	return 0;
}

  Text description method

1. Define integer n=12345,d
2. When n= 0
3.d=n%10
4. Value of output d
5.n=n/10
6. Conclusion

flow chart
 

  Operation results

14

code

#include<stdio.h>
int main()
{
	int m = 0, sum = 0;
	char c, oldc = '+';
	do {
		c = getchar();
		if (c <= '9' && c >= '0') {
			m = 10 * m + c - '0';
		}
		else {
			if (oldc == '+') {
				sum += m;
			}
			else {
				sum -= m;
			}
			m = 0;
			oldc = c;
			printf("%3d", sum);
		}
	} while (c != '=');
	return 0;
}

  Text description method

1. Define integer m=0,sum=0, character c,oldc = '+'
2.c=getchar()
3. If C < ='9 '& & C > ='0', execute m=10*m+c-'0 '
4. Otherwise, if oldc = = '+', execute sum+=m
5. Otherwise sum-=m
6.m=0
7.oldc=c
8. Output the value of sum
9. Judgment c! = '=' Execute 2, 3, 4, 5, 6, 7, 8 and 9, otherwise turn to 10
10. End

flow chart

Operation results

16

code

#include<stdio.h>
int main()
{
	int t = 1, n = 235;
	do {
		t *= n % 10;
		n /= 10;
	} while (n);
	printf("%d\n", t);
	return 0;
}

  Text description method

1. Define integer t=1,n=235
2.t*=n%10
3.n/=10
4. Judge n, execute 2, 3, 4, otherwise turn to 5
5. Value of output t
6. Conclusion

flow chart

Operation results

17

code

#include<stdio.h>
int main()
{
	int m = 5, n = 0;
	while (m > 0) {
		switch (m) {
		    case 1:
			case 3:n += 1; m--; break;
			default:n = 0; m--;
			case 2:
			case 4:n += 2; m--; break;
		}
		printf("%2d", n);
	}
	return 0;
}

  Text description method

1. Integer m=5,n=0
2. When m > 0
3. Judgment m
4. Execute case1:
5. Execute case3:
                n+=1,m --, if n+=1 turns 9
6. Default n=0, m--
7. Execute case2:
8. Execute case4:
                n+=2, m --, if n+=2 turns 9
9. Output the value of n
10. End

flow chart

Operation results

 

18

code

#include<stdio.h>
int main()
{
	int i, m = 0;
	for (i = 0; i < 5; i++) {
		switch (i) {
		    case 0:
			case 1:m++;
			case 3:m++;
			case 4:m--; break;
		}
	}
	printf("%d\n", m);
	return 0;
}

  Text description method

1. Define integer i,m=0
2.i=0
3. Judge i, otherwise turn 10
4. Execute case0:
5. Execute case1:
              m++
6. Execute case3:
              m++
7. Execute case4: m -- if M -- turn to 8
8. Output the value of m
9.i + + to 3
10. End

flow chart

Operation results

19

code

#include<stdio.h>
int main()
{
	int i, b = 0, c = 2;
	for (i = 0; i < 2; i++) {
		switch (++b, b * c) {
		    case 1:printf("1");
			case 2:printf("2");
			case 3:printf("3"); break;
			default:printf("other\n");
		}
	}
	return 0;
}

  Text description method

1. Define integer b=0,c=2
2.i=0
3. Judge I < 2, otherwise turn to 10
4. When + + B, b*c
5. Execute case1:
              Output 1
6. Execute case2:
              Output 2
7. Execute case3:
            Output 3

8. Default output other
9.i + + to 3
10. End

flow chart

Operation results

21

code

#include<stdio.h>
int main()
{
	char c;
	while ((c = getchar()) != '?')
		putchar(--c);
	return 0;
}

  Text description method

1. Define character c
2. When c = getchar())! = '?'
3.putchar(--c);
4. End

flow chart

Operation results

Perfect program questions

1

code

#include<stdio.h>
int main()
{
	int denominator, flag, i, n;
	double item, sum;
	printf("Please enter n Value of:");
	scanf_s("%d", &n);
	flag = 1;
	denominator = 1;
	sum = 0;
	for (i = 1; i <= n; i++) {
		item = flag * 1.0 / denominator;
		sum = sum + item;
		flag = -flag;
		denominator += 2;
	}
	printf("sum=%f\n", sum);
	return 0;
}

  Text description method

1. Define integer denominator, flag, i, n, decimal item, sum
2. Enter the value of n
3.flag = 1
4.denominator = 1
5.sum = 0
6.i=1
7. Judge I < = n, execute 7, 8, 9, 10, 11, 12, otherwise turn to 13
8.item = flag * 1.0 / denominator;
9.sum = sum + item
10.flag = -flag
11.denominator += 2
12.i++
13. Output sum value
14. Conclusion

flow chart

Operation results

2

code

#include<stdio.h>
int main()
{1
	int i, n;
	float sum = 0, flag = 1;
	scanf_s("%d", &n);
	for (i = 1; i <= n; i++) {
		sum = sum + (flag * i) / (2 * i - 1);
		flag = -flag;
	}
	printf("Sum=%f\n", sum);
	return 0;
}

  Text description method

1. Define integer i,n, decimal sum = 0, flag = 1
2. Output the value of n
3.i=1
4. Judge I < = n, if I < = n, execute 4, 5, 6, 7, otherwise turn to 8
5.sum = sum + (flag * i) / (2 * i - 1)
6.flag = -flag
7.i++
8. Output the value of sum
9. Conclusion
 

flow chart

Operation results

3

code

#include<stdio.h>
int main()
{
	int n, s;
	printf("Please enter a number:");
	scanf_s("%d", &n);
	printf("Output:");
	do {
		s = n % 10;
		printf("%d", s);
		n /= 10;
	} while (n != 0);
	return 0;
}

  Text description method

1. Define the integer n,s
2. Please enter a number for n
3. Output
4.s = n % 10;
5.printf("%d", s);
6.n /= 10;
7. Judge n= 0 if n= 0 execute 4, 5, 6, 7, otherwise turn to 8
8. Conclusion


flow chart

  Operation results

 4

code

#include<stdio.h>
int main()
{
	int k,n;
	double s;
	s = 1.0;
	k = 1;
	scanf_s("%d", &n);
	while (k <= n) {
		s = s + 1.0 / (k * (k + 1));
		k++;
	}
	printf("s=%f\n\n", s);
	return 0;
}

Text description method

1. Define the integer k,n; Decimal s
2.s = 1.0;
3.k = 1;
4. Enter the value of n
5. Judge K < = n, if K < = n, execute 5, 6, 7, otherwise turn to 8
6.s = s + 1.0 / (k * (k + 1))
7.k++
8. Output the value of s
9. Conclusion
 

flow chart

Operation results

 

10

code

#include<stdio.h>
int main()
{
	int i;
	for (i = 100; i < 200; i++) {
		if ((i - 2) % 4 == 0) {
			if (!((i - 3) % 7)) {
				if ((i - 5) % 9 == 0) {
					printf("%d", i);
				}
			}
		}
	}
	return 0;
}

  Text description method

1. Define integer i
2.i=100
3. Judge I < 200. If I < 200, execute 3, 4, 5, 6, 7 and 8, otherwise turn to 9
4. If (i - 2)% 4 = 0), execute 5, otherwise i + + to 3
5. If! ((i - 3)% 7 execute 6, otherwise i + + to 3
6. If (i - 5)% 9 = = 0, execute 7, otherwise i + + to 3
7. Value of output i
8.i + + to 3
9. Conclusion

flow chart

  Operation results

11

code

#include<stdio.h>
int main()
{
	int count, i, n;
	double grade, total;
	printf("Enter n:");
	scanf_s("%d", &n);
	total = 0;
	count = 0;
	for (i = 1; i <= n; i++) {
		printf("Enter grade #%d:", i);
		scanf_s("%lf", &grade);
		total = total + grade;
		if (grade < 60) {
			count++;
		}
	}
	printf("Grade average=%.2f\n", total / n);
	printf("Number of failures=%d\n", count);
	return 0;
}

  Text description method

1. Define integer count, i, n, decimal grade, tota
2. Enter a value for Enter n
3.total = 0
4.count = 0
5.i=1
6. Judge I < = n, if I < = n, execute 6, 7, 8, 9, 10, otherwise turn to 11
7. Output the value of grade
8.total = total + grade
9. If grade < 60, execute count++
10.i + + to 6
11. Output the value of total
12. Output the value of count
13. Conclusion

flow chart

Operation results

 

12

code

#include<stdio.h>
int main()
{
	int i, j;
	for (i = 0; i < 10; i++) {
		j = i * 10 + 6;
		if (j % 3 == 0) {
			printf("%3d", j);
		}
	}
	return 0;
}

  Text description method

1. Define the integer i,j
2.i=0
3. Judge if I < 10, execute if I < 10, otherwise turn to
4.j = i * 10 + 6
5. If J% 3 = 0, execute 6
6. Value of output j
7.i + + to 3
8. Conclusion

flow chart

Operation results

 

 

Keywords: C

Added by phpMover on Sat, 27 Nov 2021 20:59:42 +0200