Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (anonymous):

c programming

OpenStudy (anonymous):

how do i do this using while loops?

OpenStudy (anonymous):

http://gyazo.com/ac6df48f177f71d51ecf9e04520918b3

OpenStudy (anonymous):

you can iterate over array in a same way you'd do it with for(): int array_size = sizeof(array); int i = 0; while ( i < array_size) { ... insert all the code to check for various conditions in the task... i++; }

OpenStudy (anonymous):

``` void classify(int array[], int n){ int i = 0; while (i < n) { int element = array[i++]; // rest of code } } ```

OpenStudy (anonymous):

Do you need further help?

OpenStudy (anonymous):

yes, i do..i dont to to know what im doing here ... http://codepad.org/5R5moyZY

OpenStudy (anonymous):

this is the solution with for loops... http://codepad.org/iyh1O3Hx

OpenStudy (anonymous):

You need two variables, one to remember the first 0 and another to remember the second 0.

OpenStudy (anonymous):

``` void classify(int array[], int n){ int i = 0, first = -1, second = -1; while (i < n) { int element = array[i]; if (element == 0) { if (first == -1) { first = i; } else if (second == -1) { second = i; } } i++ } // figure out the classification here } ```

OpenStudy (anonymous):

The first part just finds the position of the first two zeros

OpenStudy (anonymous):

You could even improve the code a bit by doing: ``` void classify(int array[], int n){ int i = 0, first = -1, second = -1; while (i < n) { int element = array[i]; if (element == 0) { if (first == -1) { first = i; } else if (second == -1) { second = i; break; } } i++ } // figure out the classification here } ``` Since after you find the second zero, you don't need to continue the loop.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!