661 - Blowing Fuses

All about problems in Volume VI. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Postby little joey » Tue Feb 07, 2006 3:59 pm

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.

Do you realy expect anyone to actually help you?
The biggest problem with most problems is not how to solve the problem, but how to not solve what is not the problem.
User avatar
little joey
Guru
 
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

661 - Blowing Fusues! Runtime Error.

Postby linux » Mon Jul 03, 2006 2:14 pm

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..
User avatar
linux
Learning poster
 
Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm
Location: Dhaka (CSE, DU), Bangladesh

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

Postby linux » Sat Jul 08, 2006 11:31 am

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..
User avatar
linux
Learning poster
 
Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm
Location: Dhaka (CSE, DU), Bangladesh

Postby little joey » Sat Jul 08, 2006 12:08 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.
User avatar
little joey
Guru
 
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

The problem in your code was....

Postby linux » Sat Jul 08, 2006 12:52 pm

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..
User avatar
linux
Learning poster
 
Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm
Location: Dhaka (CSE, DU), Bangladesh

The problem in your code was....

Postby linux » Sat Jul 08, 2006 12:54 pm

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..
User avatar
linux
Learning poster
 
Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm
Location: Dhaka (CSE, DU), Bangladesh

so. Frostina!

Postby linux » Sat Jul 08, 2006 12:59 pm

I don't think there's any problem in your vector. It's quite okay.
Solving for fun..
User avatar
linux
Learning poster
 
Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm
Location: Dhaka (CSE, DU), Bangladesh

so. Frostina!

Postby linux » Sat Jul 08, 2006 1:02 pm

Thanks to Little joey for his help!
Solving for fun..
User avatar
linux
Learning poster
 
Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm
Location: Dhaka (CSE, DU), Bangladesh

661

Postby newton » Thu Sep 07, 2006 6:36 am

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.
User avatar
newton
Experienced poster
 
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh

Postby Erik » Thu Sep 07, 2006 7:44 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

Postby newton » Sun Oct 08, 2006 7:21 am

problem solved
Last edited by newton on Sat Jul 14, 2007 1:00 pm, edited 1 time in total.
User avatar
newton
Experienced poster
 
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh

Postby Erik » Sun Oct 08, 2006 8:39 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

Postby newton » Mon Mar 12, 2007 10:47 am

yeah
i do agee it.
User avatar
newton
Experienced poster
 
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh

Postby newton » Mon Mar 12, 2007 11:14 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.
User avatar
newton
Experienced poster
 
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh

Postby Jan » Mon Mar 12, 2007 8:47 pm

Try the cases...

Input:
Code: Select all
5 3 9
7
6
6
7
2
3
2
2
3 9 10
5
3
5
3
2
3
3
2
1
1
3
2
0 0 0

Output:
Code: Select all
Sequence 1
Fuse was blown.

Sequence 2
Fuse 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
Location: Dhaka, Bangladesh

PreviousNext

Return to Volume VI

Who is online

Users browsing this forum: No registered users and 0 guests

cron