Page 1 of 1

I have problem with code ...

Posted: Sat Jun 10, 2006 10:27 am
by fixit

Code: Select all

#include <stdio.h>

typedef struct{int sum,start,stop;}maxi;
int main()
{
int liczba=0,suma=0;
maxi now,old;
int n=0,ile=0,j=0,i=0;
scanf("%d",&ile);
for (j=1;j<=ile;j++)
  {
  old.sum=old.start=old.stop=suma=now.sum=now.start=now.stop=0;
    scanf("%d",&n);
      for (i=1;i<n;i++)
        {
           scanf("%d",&liczba);
           suma+=liczba;
              if (suma>now.sum)
                {
                   now.sum=suma;
                   now.stop=i+1;
                }
              if (suma<=0 || (i==n-1 && suma<=0))
              {
                suma=0;
                if (old.sum<now.sum) old=now;
                now.sum=0;
                now.stop=0;
                now.start=i+1;
              }
           }
       printf(("The nicest part of route %d is between stops %d and %d\n"),j,old.start,old.stop);

    }

return 0;
}
The problem is in here:
if (old.sum<now.sum) old=now; <-- it doesn't work :(
I trying to change to old.sum=now.sum ; old.start=now.start ...
but it doesn't work too :(
I trying change to work with only integers - not struct type.
But if (old.sum<now.sum) old=now; don't work :(
but if i put this old=now; before the printf the old=now is works;

ehh i'm confused :(
Plz help.

Re: I have problem with code ...

Posted: Sat Jun 10, 2006 6:05 pm
by Martin Macko
I don't know, but the following line looks a little bit strange:
fixit wrote:
code wrote:.................
..............if (suma<=0 || (i==n-1 && suma<=0))
..............{
.....................
..............}
.................
as it is equivalent to:
.................
..............if (suma<=0)
..............{
.....................
..............}
.................