Results 1 to 8 of 8
  1. #1
    anonb is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default Method Doesnt turn negative number 0 and doesnt return 3

    Write a method that accepts an integer array of unspecified length as a parameter. It should remove all negative values from the array and return the size of the altered array. Unused values at the end of the array should be filled with zeroes. For example, the array {3, 5, -2, 4, -3} should become {3, 5, 4, 0, 0} and 3 should be returned as the new length. Do not copy the array, modify the array that was passed as a parameter. What is the Big-O of this method?

    import java.io.*;
    import java.util.*;

    public class untitled2
    {

    public static void main(String args[])
    {
    //creating
    int temp = 0;
    int [] number = new int[5];
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter numbers: ");
    int j;
    //first loop to
    for (int i=0; i<number.length;i++)
    {
    number[i] = input.nextInt();
    //System.out.print(number[i]);
    }
    System.out.println("Returned size of aray is " + shift(number));
    for (int i=0; i<number.length;i++)
    {
    System.out.println(number[i]);

    }
    }
    public static int shift(int []number)
    {
    int j=0;
    int temp=0;
    int k=0;
    for (int i=0; i<number.length;i++)
    {

    if (number[i] < 0)
    {
    for (j=i; j < number.length-1;j++)
    number[j]=number[j+1];
    }
    }
    }
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I have a few problems with what you've posted:
    1) No [code] tags around the code.
    2) There doesn't appear to be a specific question attached. What exactly is the problem with what you have? What does it do/doesn't it do that it shouldn't/should? What is the output of what you have?
    3) You don't have any return statement in your shift() method.

  3. #3
    anonb is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default

    Its supposed to take negative integers, shift them to right, and add them as 0's.

    Output is:

    Please enter numbers:
    3 5 -2 4 -3
    Returned size of aray is 5
    3
    5
    4
    -3
    -3

  4. #4
    anonb is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default

    import java.io.*;
    import java.util.*;

    public class untitled2
    {

    public static void main(String args[])
    {
    //inializing array and scanner
    int [] number = new int[5];
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter numbers: ");
    int j;

    for (int i=0; i<number.length;i++)
    {
    number[i] = input.nextInt();
    }
    //calls second method
    System.out.println("Returned size of aray is " + shift(number));
    for (int i=0; i<number.length;i++)
    {
    System.out.println(number[i]);
    }
    }
    //this method shifts negative numbers to right
    public static int shift(int []number)
    {
    int j=0;
    for (int i=0; i<number.length;i++)
    {

    if (number[i] < 0)
    {
    for (j=i; j < number.length-1;j++)
    number[j]=number[j+1];
    }
    }
    }
    }

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    supposed to take negative integers, shift them to right, and add them as 0's
    can you explain what that means?

    Please enter numbers:
    3 5 -2 4 -3
    Returned size of aray is 5
    3
    5
    4
    -3
    -3
    What does the column of numbers represent? The input had a -2 that is not in the column. The order of the numbers in the column is different from the order in the row above it???

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by Zack View Post
    I have a few problems with what you've posted:
    1) No [code] tags around the code.
    2) There doesn't appear to be a specific question attached. What exactly is the problem with what you have? What does it do/doesn't it do that it shouldn't/should? What is the output of what you have?
    3) You don't have any return statement in your shift() method.
    You still haven't dealt with #s 1 and 3...

    As for 2, all it does is shift the numbers backward, so you'd go from {1, -2, 3, 4} to {1, 3, 4, 4}. If you want to replace them with 0s, you have to set the last element of the array to 0 each time you shift it over one.

  7. #7
    anonb is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default

    yea but how do I do that?

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You already know how to change an element based on its index in the array. You also know how to get the length of the array. Putting those two concepts together, you can edit the last element in the array.

Similar Threads

  1. Replies: 4
    Last Post: 01-30-2010, 04:49 PM
  2. PrintWriter doesnt work :(
    By Addez in forum New To Java
    Replies: 11
    Last Post: 01-17-2010, 05:59 PM
  3. why doesnt my insertion sort method not work?
    By Jeremy8 in forum New To Java
    Replies: 7
    Last Post: 11-15-2009, 02:56 AM
  4. Dll Call doesnt work
    By INFACT in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 09:31 PM
  5. My program doesnt display anything
    By Bojevnik in forum AWT / Swing
    Replies: 2
    Last Post: 10-19-2007, 02:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •