1

our prof. gave us this pseudocode a while ago...and i'm having a hard time with this part

Pseudocode

and the array index value is 14 with 14 string names

Shell_Sort(Arr,N)

{ If(N=1) then

{

Exit

}

Set Interval to N

while(Interval is not 1)

{

Set Interval to ((Interval/3)+1) If(Interval is not a whole number) then <--------- here is the part i'm having trouble with

{

Truncate Interval <------------ and here how can i truncate the interval into a whole number?

}

Set start to 1

while(start <= Interval)

{

Sort()

Increment start

}

}

}

sort()

{

set Unsrt_Indx to (start + interval)

while(unsrt_Indx <= N)

{

if (Arr[Unsrt_Indx - Interval] > arr[unsrt_Indx] then

{

set str_indx to unsrt_indx

set temp to arr[srt_indx]

while((str_indx >start && (arr[str_indx-Interval] > temp))

{

set arr[set_indx] to arr[str_index- interval]

set str_indx to (crt_index - interval)

}

set arr[str_indx] to temp

}

set unsrt_indx to (unsrt_indx + interval)

}

}

how can i use a if statement in determining if it is not whole number??

and how do i use truncate?? truncating it into ones twos or threes.

ex.
4.6666666666667
how can i truncate it into
4.67
4.6
4

something like that.

5 Answers5

1

If you know you're dealing with a number you can do:

if(x == (int)x) {
    //x is an int (ie: a whole number)
}

otherwise, if your number is a String, you can use a try/catch statement in which you perform an Integer.parseInt() on the String. If the parseInt() is successful then you're dealing with an int. Otherwise you can catch a NumberFormatException. In that case, the conversion was not successful and you're not dealing with an int.

You'll want to look at Math.floor() to do the truncating.

Memento Mori
  • 3,327
  • 2
  • 22
  • 29
0

Try DecimalFormat.

DecimalFormat df = new DecimalFormat("0.##");
String result = df.format(4.6666666666667);
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
0

I think your Prof. means Math.floor(interval)

Zutty
  • 5,357
  • 26
  • 31
  • didn't teach us that yet...what does Math.floor(interval) do?? –  Mar 05 '13 at 11:03
  • It rounds down http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#floor%28double%29 – Zutty Mar 05 '13 at 11:18
0

check for a whole number

if(Math.floor(interval) == interval)
{
    //interval is a whole number
}

and to round up :

import java.math.RoundingMode;
import java.text.DecimalFormat;

public class Main
{
  final public static void main(String[] args)
  {
    double i = 4.6666666666667;
    DecimalFormat format = new DecimalFormat("#.#"); 
    format.setRoundingMode(RoundingMode.FLOOR); // *
    String s = format.format(i);
    i = Double.parseDouble(s);
    System.out.println(i); //should be 4.6
  }
}

* instead of RoundingMode.FLOOR use appropriate RoundingMode which suits your need.

in case if you want to make it to a whole number (integer)

import java.math.RoundingMode;
import java.text.DecimalFormat;

public class Main
{
  final public static void main(String[] args)
  {
    double i = 4.6666667;
    DecimalFormat format = new DecimalFormat("#"); 
    format.setRoundingMode(RoundingMode.FLOOR); // *
    String s = format.format(i);
    int j = Integer.parseInt(s);;
    System.out.println(j);// should be 4
  }
}
codeMan
  • 5,730
  • 3
  • 27
  • 51
  • I have edited my answer to include code to you above question. Check out! – codeMan Mar 05 '13 at 11:27
  • ok.. then.. chat here but make sure u delete all the chats after we are done! – codeMan Mar 05 '13 at 11:34
  • sir...have you check the pseudocode i edited a while ago?? in the top –  Mar 05 '13 at 11:35
  • please no sir, you need to add the following 2 import statements : import java.math.RoundingMode; import java.text.DecimalFormat; – codeMan Mar 05 '13 at 11:49
  • still an error..."cannot resolve symbol class RoundingMode" and "cannot resolve symbol variable RoundingMode" –  Mar 05 '13 at 11:52
  • import java.math.RoundingMode; import java.text.DecimalFormat; –  Mar 05 '13 at 11:58
0

Use

java.lang.Math.round(double);
NPKR
  • 5,368
  • 4
  • 31
  • 48