0

Basically what I'm trying to ask is, is there anyway to read ahead in an array so that you could create a 'case' for it.

For ex: you array has only integers such as: 0 0 0 0 4 0 1 0 0 0 0 0 0 0 0 0 0 0 0 3

and what you want to try to do is create a coutdown till the next non zero number. Basically display the countdown. Is there any way to do this?

  • 2
    Please explain more! i dont understand... – MeNa Nov 23 '13 at 20:07
  • Basically I want to display the countdown that it takes till the next non-zero number being for the first case... until it hits the number '4' show the countdown from the first 0 to that 4 so 4,3,2,1 "actual number". Then, from that 4 till 1 is only 1 zero so: 1, "actual number", etc. – Borys Ostapienko Nov 23 '13 at 20:10
  • Well, are you allowed to read the array several times? What is the purpose of your program? – Étienne Nov 23 '13 at 20:36
  • purpose of the program is to display a different text (relevant to the specified number). No text for 0. Basically to have it countdown till the next number in the array thats non-zero. But a certain condition has to be met when the non-zero is hit – Borys Ostapienko Nov 23 '13 at 21:35
  • So when 0 do nothing. When 4 (lets say): if (condition 1 meets array condition non-zero number) do this reset countdown and continue counting till next non-zero – Borys Ostapienko Nov 23 '13 at 21:37
  • Please update the question (rather than adding comments). Please show the expected output for the given input (it might be the printed sequence 4 1 12). Reference to a mysterious 'certain condition' should either be demysticized or deleted. – Jonathan Leffler Nov 23 '13 at 21:47

1 Answers1

1

This code:

#include <stdio.h>

int main(void)
{
    int array[] = { 0, 0, 0, 0, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, };

    int a_size  = sizeof(array) / sizeof(array[0]);

    int i = 0;
    while (i < a_size)
    {
        if (array[i] == 0)
        {
            int j;
            for (j = i; j < a_size; j++)
                if (array[j] != 0)
                    break;
            printf("%d\n", j - i);
            i = j;
        }
        else
            i++;
    }
    return 0;
}

produces this output:

4
1
12

If that's what you want, that's roughly what you need. If it is not what you want, you need to explain more clearly what it is that you want.


Revised code for revised expected output:

#include <stdio.h>

int main(void)
{
    int array[] = { 0, 0, 0, 0, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, };

    int a_size  = sizeof(array) / sizeof(array[0]);

    int i = 0;
    while (i < a_size)
    {
        if (array[i] == 0)
        {
            int j;
            for (j = i; j < a_size; j++)
                if (array[j] != 0)
                    break;
            int k = j - i;
            while (k > 0)
                printf(" %d", k--);
            i = j;
        }
        else
        {
            printf(" '");
            i++;
        }
    }
    putchar('\n');
    return 0;
}

Revised output:

 4 3 2 1 ' 1 ' 12 11 10 9 8 7 6 5 4 3 2 1 '
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Ah, sort of. The 12 I imagine is total arr size (20) - 4 - 3 - 1 = 12. I'll try to explain it a bit better: Couter:4 3 2 1 ' 1 ' 12 11 10 9 8 7 6 5 4 3 2 1 ' array: 0 0 0 0 4 0 1 0 0 0 0 0 0 0 0 0 0 0 0 3 Is there any way of representing that? – Borys Ostapienko Nov 23 '13 at 22:29
  • The 12 is the number of zeroes between the 1 and the 3 in the array (at least, that's how it is calculated). It is also the length (20) minus the lengths of the other two sets of zeroes, minus the number of non-zeroes. Yes, please try to explain again what you want (edit the question — mainly adding information), and how you determine the answer, and the expected answer for the given data. – Jonathan Leffler Nov 23 '13 at 22:32