Thread: Array Question

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Array Question


    Lets say i make an array with 5 variables.
    1, 1, 2, 3, 1
    how do i check the array and display their are 3 ones ?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    take one loop and compare all element with 1 and take one count variable and increment it on success.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    Lets say i make an array with 5 variables.
    1, 1, 2, 3, 1
    how do i check the array and display their are 3 ones ?

    Easy.

    Code:
    #include<stdio.h>
    
    int main()
    {
    	int numbers[5] = {2,1,3,1,1};
    	short ones = 0,i = 0;
    	for(i;i <= 4;i++)
    	{
    		if(numbers[i] == 1)
    		{
    			ones++;
    		}
    	}
    	printf("%d",ones);
    	getchar();
    	return 0;
    }
    try that out

    Comments on this post

    • ptr2void disagrees : Don't just hand people answers to their homework!
    • eramit2010 disagrees : dont give ready made code just give some logic.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    Thank you , i got it all figured out :cheers:
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    New homework assignment,
    rewrite the given program without the if statement. No reserved words are permitted in the for loop block.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    Originally Posted by b49P23TIvg
    New homework assignment,
    rewrite the given program without the if statement. No reserved words are permitted in the for loop block.
    it can be possible by conditional operator :D
    or that is also not allowed :confused:
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    Good point, no ternary operator allowed!
    (But OK, I didn't think of that. You win.)

    ?: /* NO! */
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    Originally Posted by b49P23TIvg
    Good point, no ternary operator allowed!
    (But OK, I didn't think of that. You win.)

    ?: /* NO! */

    Code:
    ones=ones+!(numbers[i]^1);

    Comments on this post

    • b49P23TIvg agrees : ones += numbers[i] == 1; /* may be simpler, I like ! and !! to convert to Boolean */

IMN logo majestic logo threadwatch logo seochat tools logo