Prepare for your Next Interview
This is a discussion on An array of 100 elements consists of only 0's and 1's within the C and C++ forums, part of the Software Development category; An array of 100 elements consists of only 0's and 1's. One should count the number of 1's or 0's with a very few lines of ...
|
|||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
|||
|
An array of 100 elements consists of only 0's and 1's
An array of 100 elements consists of only 0's and 1's. One should count the number of 1's or 0's with a very few lines of C program. One method is to sum all the elements to get the number of 1's. But there should be a built-in command to get the answer in a single line of program, I suppose. Please notify me if anyone knows the answer.
Question asked by visitor Vanitha |
| Sponsored Links |
|
|||
|
Re: An array of 100 elements consists of only 0's and 1's
in C you can do it in one line
for(i = 0; i < ARR_SIZ ; i++) (num[i] == 1) ? ones++: zeros++; a sample program could be... ------------------ #include <stdio.h> #define ARR_SIZ 100 int main() { int num[ARR_SIZ], i, ones, zeros; ones = 0; zeros = 0; /* array fill */ for (i = 0; i < ARR_SIZ; i++) num[i] = (i % 2) == 0 ? 1: 0; /* array processing */ for(i = 0; i < ARR_SIZ ; i++) (num[i] == 1) ? ones++: zeros++; printf("\nones: %d\tzeros: %d\n", ones, zeros); return 0; } ------------ hth |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| convert one dimansional array to two dimansional array | Geek_Guest | C and C++ | 6 | 12-22-2007 04:17 AM |
| Winrunner does not recognize all elements of the screen | Geek_Guest | WinRunner | 0 | 04-12-2007 01:40 PM |
| Sorting elements | JobHelper | C and C++ | 0 | 01-05-2007 06:45 AM |
| XML - Elements in Document Type Definitions (DTD) | Lokesh M | AJAX & XML | 0 | 06-14-2006 11:51 AM |
| Learning Series - XML Elements, Attributes, Entities | Lokesh M | AJAX & XML | 0 | 05-15-2006 11:11 AM |