• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

NZEC (Non Zero Error Code) error

 
Sakshi Jaine
Greenhorn
Posts: 26
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting nzec error in this java code.
Please Help


Here is the code:-

 
Piet Souris
Saloon Keeper
Posts: 3416
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tell us what Exception you encounter (full details please).

When I ran your code, the first time was oke (T = 5, and soe values for A, B and N). The second time, with other values for A, B and N, I got this Exception:

Exception in thread "main" java.lang.ArithmeticException: / by zero
at javaranch11.TWONMS.main(SakshiJaine.java:59)
C:\Users\Sylvia\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1

Line 59 being:


So, check out why the min of A and B is 0.

It helps if you add this to your code:


What are you trying to achieve with this code?
 
Sakshi Jaine
Greenhorn
Posts: 26
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it is a solution to this question:-

Alice and Bob are playing a game. Alice initially has the number A and Bob has the number B. There are a total of N turns in the game, and Alice and Bob alternatively take turns. In each turn the player whose turn it is, multiplies his or her number by 2. Alice has the first turn.
Suppose after all the N turns, Alice's number has become C and Bob's number has become D. You want to calculate the integer division of the maximum number among C and D by the minimum number among C and D.
Input
• The first line of the input contains an integer T denoting the number of test cases. The description of each testcase follows.
• Each test case contains a single line with 3 integers A, B, and N.
Output
For each test case output a new line with a single integer which should be the answer


I tried to restrict the least value of A and B to one but it is not working.



Here is what I did.....


 
Knute Snortum
Sheriff
Posts: 6128
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a specific question about this code?
 
Piet Souris
Saloon Keeper
Posts: 3416
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,  in your code if A <= 0 then B = 0 and so min(A, B) could be 0 (depending on whether N is even or odd).

But suppose that A and B are positive, then after N turns:
if N is even, say 2M, then A -> A * 2^M, B -> B * 2^M, and so max / min is A / B or B / A.
if N is odd, then max / min = 2A / B or B / 2A

and likewise when either A or B < 0.

So, what is the actual challenge? Or have I misunderstood something?
 
Sakshi Jaine
Greenhorn
Posts: 26
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the program is working all right for T=1, but it is giving runtime error for T>1.
I think that is the reason for the NZEC. But i am not sure.
 
Campbell Ritchie
Marshal
Posts: 65113
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody uses error codes any more; they are a feature which ought to be ignored. If anything goes wrong, you shou‍ld suffer an Exception which carries the details of the error. You would only get an error code if you execute something like System.exit(123); and I have only ever seen mention of error codes in NetBeans.
Please explain what the nested loops are supposed to do. Please edit your code and your template as I suggested in your other thre‍ad. The poor style is likely to obscure other errors, line 11 being a particularly bad example. Please explain what is supposed to happen and what is actually happening. What does line 41 do? And line 11? I suspect they do something different from what you think they do.
 
Sakshi Jaine
Greenhorn
Posts: 26
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes there was a problem in line 11.

I rewrote the program and got it correct.



 
Knute Snortum
Sheriff
Posts: 6128
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of notes on your program:

* You don't have to import java.lang, that's done for you automatically.  You're also not using import java.io so remove it.

* Use a little white space between operators and after commas, etc.  See http://www.javaranch.com/style.jsp This isn't the only style that is acceptable, but it's a good starting place.
 
Carey Brown
Saloon Keeper
Posts: 6054
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simplify this with a compound logical expression.

becomes
Where '&&' is logical AND.
 
Piet Souris
Saloon Keeper
Posts: 3416
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sakshi Jaine wrote:I rewrote the program and got it correct.
(...)


Did you test for N > 61? But maybe you can simplify the calculations (and thus the code) a little?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!