鱼C论坛

 找回密码
 立即注册
查看: 4127|回复: 7

[已解决]清大佬帮我看看哪里错了

[复制链接]
发表于 2022-9-29 21:27:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目是这样的:输入十个整型数,从小到大排序取中间八个求均值
我的代码
#include<stdio.h>

int main()
{
        int a[10];
        int n, j, num, flag = 0;
                for (n = 0;n <= 9;n++)
                {
        printf("Please enter ten number which you want to calculate:(n-n)");
        scanf("%d", &a[n]);
        if(a[n] < a[n-1])
        {
                a[n] = j;
                a[n - 1] = a[n];
                a[n - 1] = j;
        }
                }
                for(flag = 1;flag <= 8;flag++)
                {
                num = num + a[flag];
                }
                printf("The result is:%d\n", num/8);

                return 0;
}
最佳答案
2022-9-29 22:01:39
本帖最后由 jackz007 于 2022-9-29 22:08 编辑
  1. #include<stdio.h>

  2. int main(void)
  3. {
  4.         int a[10] , n , j , num , flag = 0                                          ;
  5.         for(n = 0 ; n <= 9 ; n ++) {
  6.                 printf("Please enter ten number which you want to calculate:(n-n)") ;
  7.                 scanf("%d", &a[n])                                                  ;
  8.                 if(a[n] < a[n-1]) {                                                    // 这个排序不彻底
  9.                         a[n] = j                                                    ;  // j = ?这一句不是在砸场子吗
  10.                         a[n - 1] = a[n]                                             ;
  11.                         a[n - 1] = j                                                ;
  12.                 }
  13.         }
  14.         for(flag = 1 ; flag <= 8 ; flag ++) num = num + a[flag]                     ;  // num 没有初始化为 0
  15.         printf("The result is:%d\n" , num / 8)                                      ;
  16.         return 0                                                                    ;
  17. }
复制代码

        请参考我修改的代码:
  1. #include<stdio.h>

  2. int main(void)
  3. {
  4.         int a[10] , i , j , n , num , t                                                 ;
  5.         for(n = 10 , i = 0 ; i < n ; i ++) {
  6.                 printf("Please enter ten number which you want to calculate : (n-n) ")  ;
  7.                 scanf("%d", & a[i])                                                     ;
  8.                 for(j = i ; j && a[j] < a[j - 1] ; j --) {
  9.                         t = a[j - 1]                                                    ;
  10.                         a[j - 1] = a[j]                                                 ;
  11.                         a[j] = t                                                        ;
  12.                 }
  13.         }
  14.         for(num = 0 , i = 1 ; i < n - 1 ; i ++) num += a[i]                             ;
  15.         printf("The result is : %.2f\n" , 1.0 * num / 8.0)                              ;
  16. }
复制代码

        编译、运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. Please enter ten number which you want to calculate : (n-n) 7
  4. Please enter ten number which you want to calculate : (n-n) 4
  5. Please enter ten number which you want to calculate : (n-n) 8
  6. Please enter ten number which you want to calculate : (n-n) 2
  7. Please enter ten number which you want to calculate : (n-n) 5
  8. Please enter ten number which you want to calculate : (n-n) 9
  9. Please enter ten number which you want to calculate : (n-n) 10
  10. Please enter ten number which you want to calculate : (n-n) 5
  11. Please enter ten number which you want to calculate : (n-n) 7
  12. Please enter ten number which you want to calculate : (n-n) 3
  13. The result is : 6.00

  14. D:\[00.Exerciese.2022]\C>
复制代码

这样子出错了

这样子出错了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-9-29 22:48:25 | 显示全部楼层
桃花飞舞 发表于 2022-9-29 22:37
第一 :你的结果是由于  int num;这里一开始没有给num 初始值,num里面是随机值,造成的。可以改为int num ...

哦哦哦好的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-9-29 22:49:32 | 显示全部楼层

你好,学生,膜拜,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-9-29 22:52:45 | 显示全部楼层
jackz007 发表于 2022-9-29 22:01
请参考我修改的代码:

        编译、运行实况:

谢谢杰克
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-4-1 23:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表