Solved

making pi array makePi

Posted on 2015-02-12
26
105 Views
Last Modified: 2015-02-25
Hi,
I was working on below challenge to find 6 at first and last position of array.

http://codingbat.com/prob/p167011

public int[] makePi() {

  return null;
}

Open in new window


i am failing tests. not sure how to approach this challenge. please advise
0
Comment
Question by:gudii9
  • 10
  • 7
  • 5
  • +1
26 Comments
 
LVL 13

Assisted Solution

by:Mark Bullock
Mark Bullock earned 167 total points
ID: 40606556
If your test checks that the method returns an array with {3, 1, 4}, then the simplest way to make it pass is to declare an int array, initialize it and return it.
int[] piDigits={3, 1, 4};
return piDigits;

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40606592
public int[] makePi() {

  int[] piDigits={3, 1, 4};
return piDigits;
}

Open in new window

above passed all the tests. Not sure if something needs to be done with 22/7 which is pi calculation value right?
please advise
0
 
LVL 13

Expert Comment

by:Mark Bullock
ID: 40606628
I would think the next lesson would be to get the value of pi and return each digit.
You can get the value of pi from Math.PI
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 166 total points
ID: 40614445
Not sure if something needs to be done with 22/7
Why do you think that? It are exercises to let you create and work with arrays. Not to let you calculate pi!

But in case you would like to:

public int[] makePi() {
  int[] result = new int[3];
  int value = 2200/7;
  int divisor = 100;
  for (int i=0; i<3; i++) {
     result[i] = value / divisor;
     value = value % divisor;
     divisor /= 10;
  }
  return result;
}

Open in new window


or with the use of Math.PI:

public int[] makePi() {
  int[] result = new int[3];
  int value = (int)(Math.PI * 100);
  int divisor = 100;
  for (int i=0; i<3; i++) {
     result[i] = value / divisor;
     value = value % divisor;
     divisor /= 10;
  }
  return result;
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40615054
i wonder why we need to multiply by 100 and then devide by hunded and for loop?
please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40615175
So that the digits can be expressed in an int
0
 
LVL 13

Expert Comment

by:Mark Bullock
ID: 40615850
Multiplying pi by 100 and casting to int gives you the first three digits in an integer, 314.
Then doing integer division by 100 gives the first digit, 3.
Modulo 100 gives you the last two digits in an integer, 14.
and so on...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40616019
i wonder why we need to multiply by 100 and then devide by hunded and for loop?
If you wonder, why don't you just try without doing that? I encourage you to learn by experimenting with the code.
( You will see that your result will be the array [3,0,0] )
If you perfrom 22/7 you get just the int 3. If you take 2200/7 you get 314.
But - although interesting - that's all about calculation, which I can't imagine was the purpose of that exercise...
0
 
LVL 7

Author Comment

by:gudii9
ID: 40617297
>>>
for (int i=0; i<3; i++) {
     result[i] = value / divisor;
     value = value % divisor;
     divisor /= 10;
  }

in this for loop we are getting 3 when i=0 
then 1 when i=1
then 4 when i=2
then loop exists right.

I see how first element of array being pouplated by 3 using  [quote]result[0] = value / divisor;[/quote]

but not clear on how 1 and 4 being populated into result[1] and result[2]
>>>value = value % divisor;
this results 14 right which is the reminder due to this modulo operation


[b]divisor /= 10;[/b]

what above line means and does?
is it like
divison =divison/10; which is 1?
please advise

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40617342
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40618337
These are the passes of the for-loop:

pass one (i=0)
divisor = 100
314 / 100 = 3
314 % 100 = 14

pass two (i=1)
divisor = 10
14 / 10 = 1
14 % 10 = 4

pass three (i=2)
divisor = 1
4 / 1 = 4
4 % 1 = 0

>> not clear on how 1 and 4 being populated into result[1] and result[2]
by this line of code:
result[i] = value / divisor;

Open in new window


Btw, I don't know which IDE you use, but debugging your application step-by-step is a great way to learn how things work.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40619216
public class Test50 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("pi aray us"+makePi().toString());

	}
	public static int[] makePi() {
		  int[] result = new int[3];
		  int value = 2200/7;
		  int divisor = 100;
		  for (int i=0; i<3; i++) {
		     result[i] = value / divisor;
		     value = value % divisor;
		     divisor /= 10;
		  }
		  return result;
		}
}

Open in new window


i am using RAD IDE. I tried as above getting gibberish output. please advise.

How to see and print all the pass values while debugging.
pass one (i=0)
divisor = 100
314 / 100 = 3
314 % 100 = 14

pass two (i=1)
divisor = 10
14 / 10 = 1
14 % 10 = 4

pass three (i=2)
divisor = 1
4 / 1 = 4
4 % 1 = 0

Open in new window


By putting breaki point and step over step into will help me to see each value individually but i want to see all the values of all pass same time like above you have given preferably without system outs all over the places. please advise
0
 
LVL 13

Expert Comment

by:Mark Bullock
ID: 40619668
I think RAD is based on Eclipse. In Eclipse:
Set your breakpoint. Double-click to the left of the line number and you'll see a blue circle.
Run your app with the debugger.
Click the Variables tab. Here you can watch the values of variables change as you step through the statements.
Step over each statement to execute it and stop.
Repeat until you understand the code.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 7

Author Comment

by:gudii9
ID: 40619712
i wonder why the divisor showed 0 just after coming out of for loop as attached.

I thought i supposed to be 0.1(1/10). please advise
Divisor.jpg
0
 
LVL 84

Expert Comment

by:ozo
ID: 40619720
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40620636
>> By putting breaki point and step over step into will help me to see each value individually
Right. That's what I meant by debugging.
That way you can see the moment (the place in your code) where the program doesn't react the way you had in mind.

>> but i want to see all the values of all pass same time (...) without system outs all over the places.
That's not possible (unless you store them in a list/array/map/whatever and print that out after the loop)

>> i wonder why the divisor showed 0 just after coming out of for loop
That's because we're working with int's.
2200 / 7 = 314  (and not 314.285714... as it would be when we were using double's)
1 / 10 = 0 (and not 0.1 as it would be when we were using variables of the type double)
0
 
LVL 7

Author Comment

by:gudii9
ID: 40621940
6 / 10 = 1 (and not 0.6) right since it is greater than 0.5 ?

System.out.println("pi aray us"+makePi().toString());

above not prinitng array elements clearly. How to print array elements. please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40622085
No, (int) 6 / 10 == 0
0
 
LVL 84

Expert Comment

by:ozo
ID: 40622305
import java.util.Arrays;

System.out.println("pi array is "+Arrays.toString(makePi()));
0
 
LVL 7

Author Comment

by:gudii9
ID: 40622325
Does not it round off to nearest int. Do we need to explicitly import util package. Please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40622337
(int) conversion does not round to nearest
http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3
Which is fortunate for the operation of the http:#a40614445 algorithm.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40626557
http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3
i was going through above link. Not able to find yet where it says how int rounding happens?
0
 
LVL 13

Expert Comment

by:Mark Bullock
ID: 40626584
Try writing some code to get the value of (int)0.6
If the value is zero, it rounds down. If the value is 1, it rounds to the nearest integer.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40626720
public class Test50 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("pi aray us"+makePi().toString());
		System.out.println("rounding is"+(int)0.6);
	}
	public static int[] makePi() {
		  int[] result = new int[3];
		  int value = 2200/7;
		  int divisor = 100;
		  for (int i=0; i<3; i++) {
		     result[i] = value / divisor;
		     value = value % divisor;
		     divisor /= 10;
		  }
		  return result;
		}
}
/*pass one (i=0)
divisor = 100
314 / 100 = 3
314 % 100 = 14
pass two (i=1)
divisor = 10
14 / 10 = 1
14 % 10 = 4
pass three (i=2)
divisor = 1
4 / 1 = 4
4 % 1 = 0*/

Open in new window


i see it prints 0 only. But i still wonder that documentation is so elaborate which makes hard to find what i want

pi aray us[I@74a174a1
rounding is0
0
 
LVL 84

Accepted Solution

by:
ozo earned 167 total points
ID: 40626971
Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (§4.2.3). Then there are two cases:

Integer division is pretty standard, (at least for positive integers)
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17.2
http://mathworld.wolfram.com/IntegerDivision.html
http://en.wikipedia.org/wiki/Division_%28mathematics%29#Of_integers
0
 
LVL 7

Author Comment

by:gudii9
ID: 40630897
I confused with round method

long round(double d)

int round(float f)
which does rounding to nearest integer.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now