开发者

Help with bugs in a C code

This C code is giving me some unpredictable results. The program is meant to collect 6 nos and print out the max, position of the max no and the average. It's supposed to have only 3 functions - input, max_avr_pos and output for doing what the code is supposed to do but I am getting unpredictable results. Please what could be the problem

#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[],do开发者_运维知识库uble *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);
}


for(cnt2=0;cnt2<limit;cnt2++);
   {
      total=total+arrnum1[cnt2];
   }

; at the end of for-loop.


Loop through a single time and sum all the number and check your current maxval against the current number.

for (cnt = 0; cnt < limit; cnt++) {
      total += arrnum[cnt];

      if (maxval < arrnum[cnt]) { 
        maxval = arrnum[cnt];
        position = cnt;
      }
    }

maxval should be initialized to the minimum int value from limit.h


Seems like you can find the max much easier. How about:

maxval2 = -1;
post = -1;
for(cnt=0;cnt<limit-1;cnt++)   
{
   if(arrnum1[cnt] > maxval2)
   {
        post = cnt;
        maxval2 = arrnum1[cnt];
    }
}


Now I have written this blind, but hopefully this will help, it seems, don't take offence, the code suppled in the question is a little over complex.

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;
} 

Many thanks - Neil

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜