2018.4.23 Computer class homework

1. Find all prime numbers within 100, store the results in an array and output them in the form of 10 numbers per line.

C language source code:

#include <stdio.h>
int main() {
	int a[101];
	int i, j;
	for (i=1;i<=100;i++) {
		a[i] = 1;
	}
	i = 2;
	do {
		j = 2 * i;
		while (j <= 100 && a[i] == 1) {
			a[j] = 0;
			j += i;
		}
		i++;
	} while (i <= 100);
	int s = 0;
	for (i=2;i<=100;i++) {
		if (a[i] == 1) {
			printf("%d  ", i);
			s++;
			if (s % 10 == 0) {
				printf("\n");
			}
		}
	}
	return 0;
}
2. Write a program to sort the data in the array in ascending order using the insertion method.

C language source code:

#include <stdio.h>
int main() {
	int a[10];
	printf("Please enter 10 numbers in sequence: ");
	int i, j, k, input;
	for (i=0;i<10;i++) {
		scanf("%d", &input);
		if (i == 0) {
			a[0] = input;
		} else {
			for (j=0;j<=i-1;j++) {
				if (a[j] >= input) {
					for (k=i;k>=j;k--) {
						a[k+1] = a[k];
					}
					break;
				}
			}
			a[j] = input;
		}
	}
	for (i=0;i<10;i++) {
		printf("%d ", a[i]);
	}
	return 0;
}

3. There is a two-dimensional array a[4][4], input all the elements from the keyboard and find the saddle point. The saddle point means the number at this position is the largest in the row and the smallest in the column, and there may be no saddle point. .

C language source code:

#include <stdio.h>
int main() {
	int a[4][4];
	int i, j;
	printf("Please enter 16 numbers: ");
	for (i=0;i<4;i++) {
		for (j=0;j<4;j++) {
			scanf("%d", &a[i][j]);
		}
	}
	int max, f;
	for (i=0;i<4;i++) {
		max = 0;
		for (j=1;j<4;j++) {
			if (a[i][j] > a[i][max]) {
				max = j;
			}
		}
		f = 0;
		for (j=0;j<4;j++) {
			if (a[j][max] < a[i][max] && j != i) {
				f = 1;
				break;
			}
		}
		if (f == 0) {
			printf("(%d,%d)\n", i, max);
		}
	}
	return 0;
}
4. There is a two-dimensional array a[4][4], write a program to find:
(1) The sum of all elements;
(2) The sum of the elements on the diagonal from the upper left corner to the lower right corner;
(3) The sum of elements on the diagonal from the upper right corner to the lower left corner;

(4) The sum of all side elements.

C language source code:

#include <stdio.h>
int main() {
	int a[4][4];
	int i, j, sum;
	printf("Please enter 16 numbers: ");
	sum = 0;
	for (i=0;i<4;i++) {
		for (j=0;j<4;j++) {
			scanf("%d", &a[i][j]);
			sum += a[i][j];
		}
	}
	printf("Sum of all elements:%d\n", sum);
	sum = 0;
	for (i=0;i<4;i++) {
		sum += a[i][i];
	}
	printf("Sum of elements on the diagonal from the upper left corner to the lower right corner: %d\n", sum);
	sum = 0;
	for (i=0;i<4;i++) {
		sum += a[i][3-i];
	}
	printf("Sum of elements on the diagonal from the upper right corner to the lower left corner: %d\n", sum);
	sum = 0;
	for (i=0;i<4;i++) {
		for (j=0;j<4;j++) {
			if (i==0 || i==3 || j==0 || j==3) {
				sum += a[i][i];
			}
		}
	}
	printf("Sum of all side elements: %d\n", sum);
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324800719&siteId=291194637