用c语言,写个用加减乘除求随机十道题,并输出结果,以及将结果排序,并输出最大数,最小数,以及平均数,还有正确的数量
时间: 2023-11-14 20:07:03 浏览: 80
好的,我来帮您实现这个程序。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void generateQuestions(int questions[], int n);
void calculate(int questions[], int n, int *correct, int *sum);
void sortResult(int questions[], int n);
int main() {
int questions[10];
int correct = 0;
int sum = 0;
srand(time(NULL)); // 设置随机数种子
generateQuestions(questions, 10);
calculate(questions, 10, &correct, &sum);
sortResult(questions, 10);
printf("十道随机题的结果如下:\n");
for (int i = 0; i < 10; i++) {
printf("%d ", questions[i]);
}
printf("\n");
printf("正确的数量为:%d\n", correct);
printf("平均数为:%d\n", sum / 10);
printf("最大数为:%d\n", questions[9]);
printf("最小数为:%d\n", questions[0]);
return 0;
}
void generateQuestions(int questions[], int n) {
for (int i = 0; i < n; i++) {
int a = rand() % 100;
int b = rand() % 100;
int operator = rand() % 4;
switch (operator) {
case 0: questions[i] = a + b; break;
case 1: questions[i] = a - b; break;
case 2: questions[i] = a * b; break;
case 3: questions[i] = a / b; break;
}
}
}
void calculate(int questions[], int n, int *correct, int *sum) {
int answer;
for (int i = 0; i < n; i++) {
printf("第%d题:", i + 1);
scanf("%d", &answer);
if (answer == questions[i]) {
(*correct)++;
}
(*sum) += answer;
}
}
void sortResult(int questions[], int n) {
int temp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (questions[j] > questions[j + 1]) {
temp = questions[j];
questions[j] = questions[j + 1];
questions[j + 1] = temp;
}
}
}
}
```
这个程序会生成10道随机的加减乘除题目,等待用户输入答案,然后计算正确的数量和所有答案的和,最后对答案进行排序,并输出最大数、最小数、平均数和正确的数量。
阅读全文