Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Please help
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Please help



Title: Re: Please help
when you access a

a[i]

cast i to an int

a[(int)i]

Better yet, don't declare i as a float.  From the look of your code you only need it as an int.

Will

At 10:03 PM +0100 3/24/05, Yuan wrote:
I'm a beginner in C programming language, we use TurboC2.0 compiler in the labs but I have apple so I use Xcode at home to practice.  We solved an array program in the lab today, the program read in at most 20 real numbers into an array, and then print the number closest to the average of the 20 numbers. But when I try the same problem at home Xcode compiles with errors saying that (array subscript is not an integer) also (stray '\32' in program), I never had this kind of problems with other programs which works in TC2.0 . The source code is as follows:

#include <stdio.h>
#include <math.h>
int main()
{
float a[20],sum,i,aver,k,ref,diff;
sum=0;
i=0;
while(i<20 && scanf("%f", &a[i]) ==1)
{
sum+=a[i];
i+=1;
}
aver=sum/i;
printf("\n");
ref=a[0];
diff=fabs(aver-a[0]);
for(k=0;k<i;k+=1)
{
if(fabs(a[k]-aver)<diff)
{
ref=a[k];
diff=fabs(a[k]-aver);
}
}
printf("ave:%g  closest:%g\n",aver,ref);
}

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Student-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
>.com

This email sent to email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Student-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Please help (From: Yuan <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.