Help with bugs in a C code

Posted by Yanki Twizzy on Stack Overflow See other posts from Stack Overflow or by Yanki Twizzy
Published on 2010-03-31T14:15:08Z Indexed on 2010/03/31 14:23 UTC
Read the original article Hit count: 251

Filed under:

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[],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);
}

© Stack Overflow or respective owner

Related posts about c