View previous topic :: View next topic |
Author |
Message |
zengrz Cheater
Reputation: 0
Joined: 19 Mar 2009 Posts: 33 Location: ca
|
Posted: Thu Feb 04, 2010 12:41 am Post subject: Array help! Find longest continuous sequence!! |
|
|
I have an array of 1s and 0s and I want to find the longest continuous sequence of 0s in the array. How do i do that? Thanks in advance.
|
|
Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Thu Feb 04, 2010 2:58 am Post subject: |
|
|
i guess you can just iterate through with a counter ?
|
|
Back to top |
|
 |
Xenico Advanced Cheater
Reputation: 0
Joined: 27 Dec 2009 Posts: 94
|
Posted: Thu Feb 04, 2010 10:17 am Post subject: |
|
|
make a loop until you find the first zero and then start another loop with a variable that counts the zeros. The second loop is stopped by the first 1. Then you will have a second var which gets the value of the first variable only if the first is greater than the second.
Got it?
_________________
|
|
Back to top |
|
 |
zengrz Cheater
Reputation: 0
Joined: 19 Mar 2009 Posts: 33 Location: ca
|
Posted: Fri Feb 05, 2010 12:42 am Post subject: |
|
|
i have an idea but my code doesnt work out. i thought that there is a more nifty way to do it. guess have to get back to debugging.
thank you guys for the help
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri Feb 05, 2010 2:05 am Post subject: |
|
|
hurf
Code: | int main()
{
char arr[15] = { 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 }; //pulled out of my ass
int i, ctr = 0, highest = 0;
for(i = 0; i < 15; i++)
{
while(arr[i] == 0) { ctr++; i++; }
if(ctr > highest)
highest = ctr;
ctr = 0;
}
return 0;
} |
|
|
Back to top |
|
 |
|