NO

Author Topic: Arrays  (Read 3519 times)

Danny

  • Guest
Arrays
« on: October 18, 2004, 01:12:18 PM »
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;
}

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Arrays
« Reply #1 on: October 18, 2004, 03:17:54 PM »
You can't get input values and calculate the result in the same loop. Try this:

Code: [Select]

#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