468,478 Members | 2,313 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,478 developers. It's quick & easy.

How to count numbers

#include <stdio.h>
int main()
{
float a[4]={.02, .05, .055, .01};

int i;
for (i=0; i<=300; i++)
if (a[i]>0.4 && a[i]<0.6) count++
printf("There are %d numbers between .4 and .6\n", some code???);
return 0;
}


I have to count how many numbers there are between .4 and .6 using c++ code and this is how far I got. There are actually 500 numbers in the program that I have to count, but to make things simple I put 4 numbers in there. Can anyone please tell me how to complete the program? Thanks!!
Sep 24 '06 #1
3 3249
risby
30
#include <stdio.h>
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. float a[4]={.02, .05, .055, .01};
  4.  
  5. int i;
  6. for (i=0; i<=300; i++)
  7. if (a[i]>0.4 && a[i]<0.6) count++
  8. printf("There are %d numbers between .4 and .6\n", some code???);
  9. return 0;
  10.  
}


I have to count how many numbers there are between .4 and .6 using c++ code and this is how far I got. There are actually 500 numbers in the program that I have to count, but to make things simple I put 4 numbers in there. Can anyone please tell me how to complete the program? Thanks!!

You just have to declare the variable "count" as an integer just as you have with the variable "i" and you must initialize to zero before you start incrementing it in your loop. Then you just reference that variable where you currently have "some code???". Also, why is your loop condition i < 300 when you have 500 numbers to check?
Sep 24 '06 #2
printf("There are %d numbers between .4 and .6\n", count);

everything else should work fine
Sep 24 '06 #3
Thanks everybody for your help! I got the program to work now. And sorry, I meant 300 numbers, not 500. Heres the program in case anyone will need it for their homework!


#include <stdio.h>
int main()
{
float a[300]={300 numbers in here};

int i;
int count=0;
for (i=0; i<=300; i++)
if (a[i]>0.4 && a[i]<0.6) count++;
printf("There are %d numbers between .4 and .6\n", count);
return 0;
}
Sep 24 '06 #4

Post your reply

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

Similar topics

2 posts views Thread by DrewKull | last post: by
2 posts views Thread by bearophileHUGS | last post: by
2 posts views Thread by Smokey Grindle | last post: by
14 posts views Thread by zufie | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.