Hi Friends!

If I have number of arrays with fixed size (15), it consist of zeros and non-zeros.

eg:

     array1[15]={5,5,0,0,4,4,4,0,0,0,1,0,0,0,3}

     array2[15]={1,0,0,0,0,7,7,0,0,3,0,0,0,0,2}

     array3[15]={6,6,6,0,8,8,8,0,0,0,3,3,0,0,4}


...........

How can I count the common zero sequences and there indexes of arrays?

sample output for the above arrays:

Index Nim_of_zeros

3 1

7 2

12 2

Can any one help me to write a program for the above problem?

Recommended Answers

All 15 Replies

I don't understand that output, can you explain a little more why the output should be like that?

Start out simple and build on your code from there.
Use a for (int i = 0; ...) loop
Check if each array at index i has a value of zero,
if it does then print the index.
Once you've got that part done, you can use a while loop inside the for loop to calculate the number of consecutive zeroes.

It's quite easy to do, so have an honest go at coding it. If you get stuck, post the code and I or one of the many helpful people at this forum will be able to assist. :)

Member Avatar for iamthwee

Sample output = garbage given sample input.

Sample output = garbage given sample input.

It's poorly worded and formatted, but not garbage.
Look across all arrays.
At index 3, all arrays have a value of zero.
At index 4, not all arrays have a value of zero.
Therefore at index 3 the number of sequential zeroes is 1.
Similarly, at index 7 the number of sequential zeroes is 2.
At index 12, seq zeroes = 2

consider three arrays array1, array2 and array3.

There are number of zero and non-zeros., but when you consider common zeros sequence appear in each array.

those are;

1 zero in 3rd position,

2 zeros are in 7th position,

2 zeros are in 12th position.

So, it seems to be:

Index Nim_of_zeros

3 1

7 2

12 2

Why I need this information is I need to assign sequence of 99 to the appropriate slots where there are common zeros in every arrays.

After assigning 1 number of 99, the arrays could be like:

eg:array1[15]={5,5,0,99,4,4,4,0,0,0,1,0,0,0,3}

   array2[15]={1,0,0,99,0,7,7,0,0,3,0,0,0,0,2}

   array3[15]={6,6,6,99,8,8,8,0,0,0,3,3,0,0,4}

Since there is 1 zero at 3rd position commonly in all arrays.

Start out simple and build on your code from there.
Use a for (int i = 0; ...) loop
Check if each array at index i has a value of zero,

I wrote something like:

int index,length;
    int count;
    for(int i=0;i<15;i++)            
    {
        if(arr[i]==0)
        {
            index=i;
            count=0;
            while(arr[i]==0)
            {
                i++;
                count++;
            }

        }
        return index;
    }

And it returns the first index of the first zero sequence os arr[15].

Member Avatar for iamthwee

Is it just me or is that 'assignment of 99 statement' as clear as mud? Are you now matching zeros vertically as well?

What else are you forgetting to tell us?

I apologise.

for lenny equals blue to square; eggs are less than music; minus plus plus.

print eff, I wish I was in dixie.

commented: got a chuckle +14

Is it just me or is that 'assignment of 99 statement' as clear as mud? Are you now matching zeros vertically as well? What else are you forgetting to tell us?

He is matching zeros vertically for sure. And if a vertical colomn has all the elements as zeros, then change all that zeros to 99. Simple as that.

At OP! I think it will be much easier if you take into consideration only one colomn at a time rather than counting the number of subsequent zeros in a particular array.

So
1. Check every first element of every array. (A 2D array would be much better at having a proper loop working for multiple threads)
2. If all zero. Change all the element in that colomn to 99.
3. Incriment the index! Do 1 and 2.

Finding how many consecutive zeros are common to all the array will be really tough....

Can you please write a sample code for this problem?

Member Avatar for iamthwee

He is matching zeros vertically for sure. And if a vertical colomn has all the elements as zeros, then change all that zeros to 99. Simple as that.

Actually, no it isn't simple as that. The OP has changed the goal post in their original request. I will go so far as to say his first example had some logic behind it... But his sample output was only for array1, he didn't mention array2 or array3 with his 'nim_of_zeros.'

That's why I said the output was garbage given his input.

Yes this is important, we're here to help but one of my pet peeves is when half way through the question the OP suddenly changes the requirements. Now he has mentioned matching consecutive zeros vertically - and changing them all to '99'. Next he might mention the array needs to match 'wrap around rows and columns'.

It is absolutely important to ask questions the smart way and define all these details presented in the question from the get go otherwise... hell we're just wasting our time. (I do realise English my not be the OP's native tongue so we can be a little more understanding in that respect.)

All that being said the subsequent link looks useful and easy to adapt.

http://stackoverflow.com/questions/22877495/use-recursion-to-find-4-consecutive-1s-in-2d-array

If all you want to do is to change the zeros in a column to 99 if all of he arrays have a zero in that column then you can do the following. this is psudeo code so it wont compile.

for i = 0 until i < arraysize
{
    if  not array1[i] && not array2[i] && not array3[i]
    then array1[i] = array2[i] = array3[i] = 99
}

Its as simple as that. Then if you need to get the index and run of the consecutive zeros then you can just use on of the arrays and check for the 99's. Something like the following should work

int index = 0, run = 0;
for i = 0 until i < arraysize
{
    if array1[i] == 99
    then 
        if run not equal to 0
        then {
            index = i;
            run++;
        }
        else
            run++
    }
    else
    {
        if run > 0
        then {
            cout << index << "\t" << run << endl;
            index = 0;
            run = 0;
        }
    }

Well, this is for the fixed number of arrays as 3, suppose if the number of array changes at run time, so how could we insert those arrays?

Well I gave you a solution for the problem you asked for. You can't keep changing the goal post as iamthwee has said. I'll leave it to you to figure out how change it to work for a 2d array.

Member Avatar for iamthwee

The link I provided should be ample enough information to get you going. Noone is going to do your homework for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.