Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!
I need to write function which finds first and second element equal 0, and then sum elements between these two zeros.
For example:
If I have array like this
{1,0,5,7,9,0,7,0,8,9}
then function need to output 21,
because first 0 is in second place, second 0 is in sixth place and sum between is 5+7+9=21.
If there have not elements equal 0 or there have not elements between two zeros function should output 0.
I have code like this right now, but it doesn't work at all.

What I have tried:

My code:
C++
double minim (double m[], int A)
{	
    double min=m[0];
    int indeks;
    int i;
    for ( i=0; i<a; i++)
    { 
        if (m[i]=0)
        {
            min=m[i]; 
            indeks=i; 
            printf("first 0=%d ",indeks) 
            return indeks; 
        } 
        
        double max=min; 
        int indeks1; 
        for (i=min+1; i<a; i++) 
        { 
             indeks1=i; (second=" ", ); 
             sum=0; 
             i<indeks1-1; 
             printf("\nelement %d", sum);
        }
    }
}
Posted
Updated 19-Nov-16 17:02pm
v4
Comments
Patrice T 19-Nov-16 20:54pm    
Try to paste,again your code, a CP bug trashed your code.
Use Improve question to update your question.
Chris Maunder 19-Nov-16 22:16pm    
Which bug?
Patrice T 19-Nov-16 22:28pm    
All the ="" are coming from the sanitizer sometimes when validating the question.
Chris Maunder 19-Nov-16 23:03pm    
Gotcha. Weird - that will only happen inside markup, and not inside PRE blocks. Time to switch to markdown completely?
Patrice T 20-Nov-16 1:38am    
Nice correction !
The problem is that newbies tend to not use the PRE block.
Personally, I don't like markdown, preference is to have the possibility to disactivate it with a switch.

1 solution

Quote:
I have code like this right now, but it doesnt work at all.

Well, no. It won't.
It won't even compile, much less run or do anything useful.

Throw that lot away, and start again from scratch.
Do it in "easy stages".
Write a function to find the first 0 value, and return it's index.
When that works, change it to find the first and second zero value.
When that works, add the code to sum the values between them.

But ... this is your homework, so I'll give you no code: it simple enough that if you do it in easy stages and think before you start code each time, you will do it in very little time at all.
 
Share this answer
 
Comments
George Swan 20-Nov-16 0:51am    
Why not try to solve this by lateral thinking? Instead of finding zero directly, detect its presence by using the unique property of adding zero to a total. Is that method more efficient than using index values? It's always a good idea to look for alternative solutions, the obvious ones are not always the best.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900