tags:

views:

184

answers:

4

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);
}
+1  A: 
for(cnt2=0;cnt2<limit;cnt2++);
   {
      total=total+arrnum1[cnt2];
   }

; at the end of for-loop.

N 1.1
I don't write much C anymore, but I have to believe that modern compilers would display a warning about that extra semicolon. @Yanki - Did your compiler warn you about that?
JeffH
@JeffYes it did. I took it out but was still having problems
Yanki Twizzy
You can catch this by either running it in a debugger, or printing out what the value of your variables are at different parts of the loop.What this is doing is incrementing cnt2 up to 6, then adding arrnum1[6] (which is garbage) to total.
JohnMcG
gcc didn't warn me about it.
JohnMcG
A: 

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];
    }
}
DougN
+1  A: 

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

Seth M.
A: 

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

Neil