## 661 - Blowing Fuses

Moderator: Board moderators

Congratulations, you've just started the fifth thread on this problem!
- You just dump your code, without any explanation;
- You don't use code tags, so it will be printed in a proportional letter type, all lines cramped to the left without indentation;
- Your code is completely non-descriptive, using variables a, b, c, etc., and goto label ss.

The biggest problem with most problems is not how to solve the problem, but how to not solve what is not the problem.

little joey
Guru

Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

### 661 - Blowing Fusues! Runtime Error.

Can anyone help me? My code is giving Runtime Error. Here's my code:
Code: Select all
`#include<stdio.h>main(){   int n,m,c,i,power_device[21],operations[21],consumption,Max,turn[21],sequence,state;   for (sequence=1;;sequence++)   {      scanf("%d %d %d",&n,&m,&c);      if (!n && !m && !c) break;      for (i=1;i<=n;i++)      {         scanf("%d",&power_device[i]);      }      consumption=0;Max=0;      for (i=1;i<=20;i++) turn[i]=0;      for (i=1;i<=m;i++)      {         scanf("%d",&operations[i]);         state=operations[i];         if (state<=n)         {         if (turn[state]==0)         {            consumption += power_device[state];            turn[state]=1;         }         else         {            turn[state]==0;            consumption-=power_device[state];         }         if (c < Max) break;         if (Max < consumption) Max = consumption;         }      }      printf ("Sequence %d\n",sequence);      if (c < Max) printf ("Fuse was blown.\n");      else      {         printf ("Fuse was not blown.\n");         printf("Maximal power consumption was %d amperes.\n",Max);      }   }   return 0;}`

The OJ tells me:
Your program has died with signal 11 (SIGSEGV). Meaning:

Invalid memory reference!

Solving for fun..

linux
Learning poster

Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm

### I've modified source-code! but Giving TLE. Why?

My modified source-code is
Code: Select all
`#include<stdio.h>main(){   int n,m,c,i,power_device[21][2],operations[21],consumption,Max,sequence,state;   for (sequence=1;;sequence++)   {      scanf("%d %d %d",&n,&m,&c);      if (!n && !m && !c) break;      for (i=1;i<=n;i++)      {         scanf("%d",&power_device[i][1]);         power_device[i][2]=0;      }      consumption=0;Max=0;      for (i=1;i<=m;i++)      {         scanf("%d",&operations[i]);         state=operations[i];         if (state<=n && c>Max){         if (!power_device[state][2])         {            consumption += power_device[state][1];            power_device[state][2]=1;         }         else         {            power_device[state][2]=0;            consumption-=power_device[state][1];         }         if (Max < consumption) Max = consumption;         }      }      printf ("Sequence %d\n",sequence);      if (c < Max) printf ("Fuse was blown.\n");      else      {         printf ("Fuse was not blown.\n");         printf("Maximal power consumption was %d amperes.\n",Max);      }   }   return 0;}`
What's the problem in my code?
Solving for fun..

linux
Learning poster

Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm

Why do you think m, the number of operations, is limited to 20?
Try to make your program independant of m, so it can handle hundreds or thousands of operations.

little joey
Guru

Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

### The problem in your code was....

Got AC. Thanks for all posts.
Code: Select all
`Removed`
Last edited by linux on Thu Sep 11, 2008 9:41 pm, edited 2 times in total.
Solving for fun..

linux
Learning poster

Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm

### The problem in your code was....

Hey! frostina, I've solved this program got an error in your code.
You've broken your for-loop when s>c.
Definitely It's your fault. Make a correction and get accepted. Wish you success.
Solving for fun..

linux
Learning poster

Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm

### so. Frostina!

I don't think there's any problem in your vector. It's quite okay.
Solving for fun..

linux
Learning poster

Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm

### so. Frostina!

Thanks to Little joey for his help!
Solving for fun..

linux
Learning poster

Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm

### 661

thank you very much..

Code: Select all
`       >> del  forum\\code  `
Last edited by newton on Sat Jul 14, 2007 12:58 pm, edited 2 times in total.

newton
Experienced poster

Posts: 162
Joined: Thu Jul 13, 2006 7:07 am

Hi,

Code: Select all
`if(amp>l)  max=amp;l=amp; `

I think you mixed something up there. You don't use max at all. Think it over.
Code: Select all
`      if(amp>=c)         {         printf("Sequence %ld\nFuse was blown.\n\n",s);         break;         } `

The fuse does not blow when the currect reaches c. It blows when the current goes beyond c.
Secondly, according to the break; you don't continue reading the missing gadget-switches. Hence you might get in trouble as you read the next a, b and c but you are still in the middle of gadgets of the previous testcase.

Cu, Erik
Erik
Learning poster

Posts: 67
Joined: Fri Jul 01, 2005 11:29 am
Location: Germany

problem solved
Last edited by newton on Sat Jul 14, 2007 1:00 pm, edited 1 time in total.

newton
Experienced poster

Posts: 162
Joined: Thu Jul 13, 2006 7:07 am

Hi,

this is a multiple input problem. There are many testcases in it. Hence if you break from the loop that way, you might read wrong data when processing the next testcase.

Cu, Erik
Erik
Learning poster

Posts: 67
Joined: Fri Jul 01, 2005 11:29 am
Location: Germany

yeah
i do agee it.

newton
Experienced poster

Posts: 162
Joined: Thu Jul 13, 2006 7:07 am

Thanx everybody.
there was a chilly mistake.

Code: Select all
`                me [spoiler] too`

newton...................................................simply the best.
Last edited by newton on Tue Mar 13, 2007 7:33 am, edited 1 time in total.

newton
Experienced poster

Posts: 162
Joined: Thu Jul 13, 2006 7:07 am

Try the cases...

Input:
Code: Select all
`5 3 9766723223 9 105353233211320 0 0`

Output:
Code: Select all
`Sequence 1Fuse was blown.Sequence 2Fuse was not blown.Maximal power consumption was 10 amperes.`

Hope these help.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru

Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm