#include <stdio.h>
int main()
{
int scores[10]={0}, count=0;
scores[10]={16,19,20,22,25,39,40,41,49,50};
for(count=0;count<9;count++)
{
if(scores[count] > 20 && scores[count] < 40)
printf("%d",scores[count]);
}
return 0;
}
How do I assign numbers to an array?
Hello Everyone!
I'm having a problem assigning numbers to an int array may be it's because I don't know how to do it right:
Option 1 is to specify the contents at creation time:
Option 2 is to specify the contents 1 by 1:
#include <stdio.h>int main(){ int scores[10]={16,19,20,22,25,39,40,41,49,50}; int count=0; for(count=0;count<9;count++) { if(scores[count] > 20 && scores[count] < 40) printf("%d",scores[count]); } return 0;}
Option 2 is to specify the contents 1 by 1:
#include <stdio.h>int main(){ int scores[10]; scores[0] = 16; scores[1] = 19; . . . scores[8] = 49; scores[9] = 50; int count=0; for(count=0;count<9;count++) { if(scores[count] > 20 && scores[count] < 40) printf("%d",scores[count]); } return 0;}
Quote:Original post by drjulio2002
Hello Everyone!
I'm having a problem assigning numbers to an int array may be it's because I don't know how to do it right:
*** Source Snippet Removed ***
The second line of this
int scores[10]={0}, count=0;scores[10]={16,19,20,22,25,39,40,41,49,50};
won't compile IMO because you can only initialize an array like that. Once it's been initialized, this syntax doesn't work anymore. What you can do is assign value to individual positions in the array like so
scores[ 0 ] = 16;scores[ 1 ] = 19;// and so on ...
Make sure you array is large enough though or you'll get an access violation if you try to index out of bounds.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement