I have problem with code ...

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
fixit
New poster
Posts: 5
Joined: Tue Jun 06, 2006 7:39 pm

I have problem with code ...

Post 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.
Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Re: I have problem with code ...

Post 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)
..............{
.....................
..............}
.................
Post Reply

Return to “C++”