News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

Arrays

Started by Danny, October 18, 2004, 01:12:18 PM

Previous topic - Next topic

Danny

This code s ment to accept 12 user inputs and compute the result can someone tell mewhat the hell is wrong with it any help would be highly appriciated.

#include <stdio.h>
#include <math.h>
#define lenght 12
int main()
{
   float x[lenght];
   float a;
   int i;
   printf("Enter the average amount of rainfall for each month: ");
   for(i=0; i<= 12; i++)
   {
      scanf("%f", x);
                a=x[0]+x[1]+x[2]+x[3]+x[4]+x[5]+x[6]+x[7]+x[8]+x[9]+x[10]+x[11]/12;
                printf("The average rainfall for the year was %f\n",a);
            }

             return 0;
}

Pelle

You can't get input values and calculate the result in the same loop. Try this:


#include <stdio.h>
#include <math.h>
#define lenght 12

int main()
{
   float x[lenght];
   float a;
   int i;

   printf("Enter the average amount of rainfall for each month:\n");

   for (i = 0; i < lenght; i++)
   {
       printf("Enter value for month %d (and press Enter): ", i+1);
       scanf("%f", &x[i]);
   }

   a=(x[0]+x[1]+x[2]+x[3]+x[4]+x[5]+x[6]+x[7]+x[8]+x[9]+x[10]+x[11])/12;
   printf("The average rainfall for the year was %f\n",a);

   return 0;
}


Pelle
/Pelle