Results 1 to 8 of 8
- 09-27-2010, 09:56 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 13
- Rep Power
- 0
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];
}
}
}
}
- 09-27-2010, 10:15 PM #2
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.
- 09-27-2010, 10:40 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 13
- Rep Power
- 0
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
- 09-27-2010, 10:48 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 13
- Rep Power
- 0
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];
}
}
}
}
- 09-27-2010, 10:56 PM #5
can you explain what that means?supposed to take negative integers, shift them to right, and add them as 0's
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???Please enter numbers:
3 5 -2 4 -3
Returned size of aray is 5
3
5
4
-3
-3
- 09-27-2010, 11:35 PM #6
- 09-27-2010, 11:57 PM #7
Member
- Join Date
- Sep 2010
- Posts
- 13
- Rep Power
- 0
yea but how do I do that?
- 09-28-2010, 12:17 AM #8
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
-
the values in the row of a JTable doesnt look right
By bigj in forum New To JavaReplies: 4Last Post: 01-30-2010, 04:49 PM -
PrintWriter doesnt work :(
By Addez in forum New To JavaReplies: 11Last Post: 01-17-2010, 05:59 PM -
why doesnt my insertion sort method not work?
By Jeremy8 in forum New To JavaReplies: 7Last Post: 11-15-2009, 02:56 AM -
Dll Call doesnt work
By INFACT in forum New To JavaReplies: 1Last Post: 10-04-2009, 09:31 PM -
My program doesnt display anything
By Bojevnik in forum AWT / SwingReplies: 2Last Post: 10-19-2007, 02:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks