c programming
how do i do this using while loops?
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++; }
``` void classify(int array[], int n){ int i = 0; while (i < n) { int element = array[i++]; // rest of code } } ```
Do you need further help?
yes, i do..i dont to to know what im doing here ... http://codepad.org/5R5moyZY
You need two variables, one to remember the first 0 and another to remember the second 0.
``` 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 } ```
The first part just finds the position of the first two zeros
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.
Join our real-time social learning platform and learn together with your friends!