• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 123
  • Last Modified:

making pi array makePi

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
gudii9
Asked:
gudii9
  • 10
  • 7
  • 5
  • +1
3 Solutions
 
Mark BullockQA Engineer IIICommented:
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
 
gudii9Author Commented:
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
 
Mark BullockQA Engineer IIICommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
zzynxSoftware engineerCommented:
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
 
gudii9Author Commented:
i wonder why we need to multiply by 100 and then devide by hunded and for loop?
please advise
0
 
ozoCommented:
So that the digits can be expressed in an int
0
 
Mark BullockQA Engineer IIICommented:
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
 
zzynxSoftware engineerCommented:
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
 
gudii9Author Commented:
>>>
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
 
ozoCommented:
0
 
zzynxSoftware engineerCommented:
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
 
gudii9Author Commented:
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
 
Mark BullockQA Engineer IIICommented:
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
 
gudii9Author Commented:
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
 
ozoCommented:
0
 
zzynxSoftware engineerCommented:
>> 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
 
gudii9Author Commented:
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
 
ozoCommented:
No, (int) 6 / 10 == 0
0
 
ozoCommented:
import java.util.Arrays;

System.out.println("pi array is "+Arrays.toString(makePi()));
0
 
gudii9Author Commented:
Does not it round off to nearest int. Do we need to explicitly import util package. Please advise
0
 
ozoCommented:
(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
 
gudii9Author Commented:
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
 
Mark BullockQA Engineer IIICommented:
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
 
gudii9Author Commented:
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
 
ozoCommented:
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
 
gudii9Author Commented:
I confused with round method

long round(double d)

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 10
  • 7
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now