代码之家  ›  专栏  ›  技术社区  ›  Yanki Twizzy

帮助解决C代码中的错误

c
  •  -1
  • Yanki Twizzy  · 技术社区  · 15 年前

    这个C代码给了我一些不可预测的结果。该程序旨在收集6个编号,并打印出最大编号、最大编号位置和平均值。它应该只有3个函数——输入、max_avr_pos和输出来完成代码应该做的事情,但我得到了不可预测的结果。请问有什么问题

    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    void input_vals(int arrnum[]);
    void max_ave_val(int arrnum1[],double *average,int *maxval,int *position);
    void print_output(double *average1,int *maxval1,int *position1);
    int main(void)    {
      int arrnum[6],maxval2,position2;
      double average2;
      input_vals(arrnum);
      max_ave_val(arrnum,&average2,&maxval2,&position2);
      print_output(&average2,&maxval2,&position2);
      _getche();
      return 0;
    }
    void input_vals(int arrnum[]){
        int count;
        printf("\n Please enter six numbers\n");
        for(count=0;count<6;count++) {
            scanf("%d",&arrnum[count]);
        }
    }
    void max_ave_val(int arrnum1[],double *average,int *maxval,int *position)    {
        int total=0;
        int cnt,cnt1,cnt2,limit,maxval2,post;
        limit=6;
        /* finding the max value*/
        for(cnt=0;cnt<limit-1;cnt++)
            for(cnt1=limit-1;cnt1>cnt;--cnt1) {
                if(arrnum1[cnt1-1]>arrnum1[cnt1]) {
                    maxval2=arrnum1[cnt-1];
                    post=(cnt-1)+1;
                }
                else {
                    maxval2=arrnum1[cnt1];
                    post=cnt1+1;
                }
            }
    
            *maxval=maxval2;
            *position=post;
            /* solving for total */
            for(cnt2=0;cnt2<limit;cnt2++);
            {
                total=total+arrnum1[cnt2];
            }
        *average=total/limit;
    }
    void print_output(double *average1,int *maxval1,int *position1)    {
        printf("\n value of the highest of the numbers is %d\n",*maxval1);
        printf("\n the average of all the numbers is %g\n",*average1);
        printf("\n the postion of the highest number in the list is %d\n",*position1);
    }
    
    4 回复  |  直到 15 年前
        1
  •  1
  •   N 1.1    15 年前
    for(cnt2=0;cnt2<limit;cnt2++);
       {
          total=total+arrnum1[cnt2];
       }
    

    ; 在for循环的末尾。

        2
  •  1
  •   Seth M.    15 年前

    循环一次,对所有数字求和,并对照当前数字检查当前maxval。

    for (cnt = 0; cnt < limit; cnt++) {
          total += arrnum[cnt];
    
          if (maxval < arrnum[cnt]) { 
            maxval = arrnum[cnt];
            position = cnt;
          }
        }
    

    maxval应初始化为limit中的最小int值。H

        3
  •  0
  •   DougN    15 年前

    看起来你可以更容易地找到max。怎么样:

    maxval2 = -1;
    post = -1;
    for(cnt=0;cnt<limit-1;cnt++)   
    {
       if(arrnum1[cnt] > maxval2)
       {
            post = cnt;
            maxval2 = arrnum1[cnt];
        }
    }
    
        4
  •  0
  •   Neil    15 年前

    现在,我已经写了这个盲,但希望这将有助于,似乎,不要生气,在问题中补充的代码有点过于复杂。

    void max_ave_val(int arrnum1[],double *average,int *maxval,int *position)    { 
    
        #define LIMIT 6 
    
        *maxval = 0;
        *average = 0.0f;
    
        for ( Int index = 0; index < LIMIT; index++ )
        {
            *average += arrnum1[ index ];
            if ( arrnum1[ index ] > *maxval )
            {
                *maxval = arrnum1[ index ];
                *position = index;
            }
        }
    
        *average /= LIMIT;
    } 
    

    非常感谢-尼尔