- 求大神幫忙看看,好像是指針用錯了,但我找不出錯
-
#include<stdio.h>
int main() {
int max(int *a);
int min(int *b);
int aver(int *c);
int a[8];
printf("please type eight num:");
for (int i = 0; i < 8; i++) {
scanf_s("%d", &a[i]);
}
printf("max=%d min=%d aver=%d",max(a),min(a),aver(a));
for (int i = 0; i < 7; i++) { //冒泡排序
for (int j = 0; j < 7 - i; j++) {
int k;
k = a[j];
a[j] = a[j + 1];
a[j + 1] = k;
}
}
for (int i = 0; i < 8; i++) {
printf("%d", a[i]);
}
}
//最大值
int max(int *a) {
int *p;
for (p = a; p < p + 7; p++) {
if (*p > *(p + 1)) {
int k;
k = *p;
*p = *(p + 1);
*(p + 1) = k;
}
}
return a[8];
}
//最小值
int min(int *b) {
int *p;
for (p = b; p < p + 7; p++) {
if (*p <*(p + 1)) {
int k;
k = *p;
*p = *(p + 1);
*(p + 1) = k;
}
}
return b[8];
}
//平均值
int aver(int*c) {
int sum = 0, aver;
for (; c < c + 8; c++) {
sum += *c;
}
aver = sum / 8;
return aver;
}
最佳回答:
#include<stdio.h>
int main() {
int max(int *a);
int min(int *b);
float aver(int *c);
int a[8];
printf("please type eight num:");
for (int i = 0; i < 8; i++) {
scanf_s("%d", &a[i]);
}
printf("max=%d min=%d aver=%f\n",max(a),min(a),aver(a));
for (int i = 0; i < 7; i++) { //冒泡排序
for (int j = 0; j < 7 - i; j++) {//修改
if(a[j]>a[j+1]){
int k;
k = a[j];
a[j] = a[j + 1];
a[j + 1] = k;
}
}
}
for (int i = 0; i < 8; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
//最大值
int max(int *a) {
int i,k;
k=a[0];
for (i=1; i<8; i++) {
if (a[i]> k) {
k=a[i];
}
}
return k;
}
//最小值
int min(int *a) {
int i,k;
k=a[0];
for (i=1; i<8; i++) {
if (a[i]< k) {
k=a[i];
}
}
return k;
}
//平均值
float aver(int*c) {
float sum = 0, aver;
for (int i=0; i<8; i++) {
sum += c[i];
}
aver = sum / 8;
return aver;
}