By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

help a newbie in c

P: 1
Hi i am just starting to learn c and i was doing a exercise from the book i am using but when i try to complie this program i get an error message. for the life of me i can't figure out whats wrong if you could at least point me in the right direction that would be great. i am getting a invaild initializer error when trying to compile this:

Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3.     float iq [] = ( 0.1, 0.2, 0.3, 0.4, 0.5,
  4.                 0.5, 0.3,  0.1,  0.4, 0.2, 
  5.                       0.3, 0.2,  0.4,  0.5, 0.1,  
  6.                       0.1, 0.3,  0.5,  0.2, 0.4 );
  7.     int workers;
  8.  
  9.     puts("Iq of workers");
  10.     for(workers=0;workers<9;workers++)
  11.     {
  12.         printf("Worker iq %d, %f\n",workers+1,iq[workers]);
  13.     }
  14.     return 0;
  15. }
Feb 25 '07 #1
Share this Question
Share on Google+
1 Reply


100+
P: 1,646
Hi. 2 things to change. Firstly the syntax for initializing an array like this is to use curly brackets. Secondly, to avoid truncation warnings, you should use the letter f to indicate a float rather than a double.
Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3.     float iq [] = { 0.1f, 0.2f, 0.3f, 0.4f, 0.5f,
  4. 0.5f, 0.3f, 0.1f, 0.4f, 0.2f, 
  5. 0.3f, 0.2f, 0.4f, 0.5f, 0.1f, 
  6. 0.1f, 0.3f, 0.5f, 0.2f, 0.4f };
  7. int workers;
  8.  
  9. puts("Iq of workers");
  10. for(workers=0;workers<9;workers++)
  11. {
  12. printf("Worker iq %d, %f\n",workers+1,iq[workers]);
  13. }
  14. return 0;
  15. }
Feb 25 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.