Rearranging Array Values From Negative To Positive

Feb 7, 2015

I have a problem where I am trying to re arrange the values in an array from negative to positive. I have it re arranged but I cannot figure out how to re arrange them in numerical order. I have to use O(n) and O(1) operations.

Java

import java.util.Arrays;
public class Task7 {
public static void main(String[] args){
int[] numbers = {-19, 6, 34, -3, -8, 23, 5, 678, -45, -12, 76}; //array of positive and negative numbers
int next = 0; //in no particular order

[Code] .....

View Replies


ADVERTISEMENT

Determine Positive And Negative Values Have Been Read

Feb 4, 2015

The program work somehow, but it can't count the first input when user key in.

import java.util.Scanner;
public class DetermineValues {
public static void main( String[] args ) {
int sum;
int minus;
int data;

[code]....

View Replies View Related

Generate Random Number Between Two Values That Can Be Negative Or Positive

Apr 30, 2015

I am trying to make a method that generated a random number between two values that can be negative or positive.

So:

rand(-0.2, 0.2);

would give one of these: -0.2, -0.1, 0, 0.1, 0.2

View Replies View Related

Byte Array Negative Values

Mar 30, 2015

I am new to Android. I have byte array of size 10. I am passing the Decimal values (131 - 140) to byte array. But while printing I get Negative (-) values with decreasing order .

How can I get same value as positive values?

Or How can I store positive value e.g. 131 as byte array element.

Please not my requirement is array must be ByteArray only

View Replies View Related

MaxSum Algorithm With Negative Values In Array

Apr 2, 2014

How would I go about inputting the negative values in the array in case 1 the array comes from the user, case 2 from the text file? The sum prints perfectly fine with positive values but if I input negative values it just completely ignores them.

case 1:
int sum;
System.out.print("Enter list of comma-delimeted integers: ");
Scanner scan = new Scanner(System.in);
String input2=scan.next();
String[] num = input2.split(",");
int[] a= new int[num.length];

[Code] ....

View Replies View Related

Method That Returns Positive / Negative Or Zero Depending On Sum Of Two Variables?

Dec 10, 2014

I'm taking a class in object oriented programming and we have a task to write a method that returns positive, negative or zero depending on the sum of two variables.

I've had a go at it and i've got to a certain point but i'm struggling to get past this on error - return outside method.

public class Test {
public int sumArgs;
public int arg1;
public int arg2;

[Code] ....

The error occurs on the line 'return "Positive";

View Replies View Related

Adding And Subtracting Positive Or Negative Two Numbers Of Any Length With Strings?

Feb 3, 2014

You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.

The program must use a "Number" class which includes at least the following methods:

Number ( );
Number (double n);
Number add (Number RHS);
Number subtract (Number RHS);
String toString ( );

This is what i have but it only adds positive numbers and it doesn't subtract problems like 7.05-8.96. Also some of it was what our teacher gave us like alignwhole method

import java.util.Scanner; 
public class Number{
private String whole;
private String decimal;
private String sign;
  public static void main (String[] args){
 System.out.println("Enter two numbers");

[code]....

View Replies View Related

Adding And Subtracting Positive Or Negative Two Numbers Of Any Length With Strings

Feb 5, 2014

You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.

The program must use a "Number" class which includes at least the following methods:

Number ( );
Number (double n);
Number add (Number RHS);
Number subtract (Number RHS);
String toString ( );

The below code is what our teacher gave us to start with, but it needs to add and subtract positive or negative numbers of any length. This code only adds positive numbers. Need to write code for subtraction .

Java Code:

import java.util.Scanner;
public class Number{
private String whole;
private String decimal;
private String sign;

[Code] .....

View Replies View Related

Rearranging Array With Offsets?

Feb 22, 2014

I'm trying to take an input string of the alphabet and create an offset on it so that it starts with say 'D' and ends with XYZABC. Simply starting from an offset from A and finishing with whatever the offset skipped. I have no problem skipping the first few letters but it is tagging on what was skipped that I can't seem to get.

Here's my code:

public static void main(String[] strings) {
char[] ABC = new char[25];
char[] cipher=new char[25];
boolean correctvalue=false;
while(!correctvalue){
TextIO.putln("Please enter the shift value (between -25..-1 and 1..25)");
TextIO.putln("0 is not a valid shift value.");

[code]...

I ask for the offset and the string (The user can input the alphabet already offset). I put the string into an array and work on it from there. Here is where I'm stuck:

for(int i=0; i<= cipher.length-offset; i++){
cipher[i]= (char) (ABC[i] + offset);
}
for(int i=0;i<=offset;i++){
cipher[25-offset+1]=(char) (65 + i);
}

It seems to me that I should be able to subtract my offset from the end of the array then start from A (ASCII 65) and add up until I've hit the end of the array. However, right now if I put the correct (non-offset) alphabet in and create offset of 3 instead of the alphabet ending with WXYZABC. It ends with WXYZD. I don't understand why my code doesnt work. I can make it say WXYZA by putting in manual offsets in the for loop but it wont print out anything beyond the first character.

View Replies View Related

Generate Two Arrays - One With All Positive Numbers And Another With Negative Numbers

Mar 10, 2015

Create an integer array with 10 numbers, initialize the array to make sure there are both positive and negative integers. Write a program to generate two arrays out of the original array, one array with all positive numbers and another one with all negative numbers. Print out the number of elements and the detailed elements in each array.

public class problem3 {
public static void main(String[]args){
int[] numbers = {1, 2, 3, 4, 5, -1, -2, -3, -4, -5};
for (int i = 0; i<numbers.length;){
if(i>0){
System.out.println(numbers);
}
else
System.out.println(numbers);
}
}
}

View Replies View Related

Change Negative Number To Positive Number?

Nov 20, 2014

I am 4 weeks into my Intro to Java course and I am having a bit of trouble with my code. I need to make a program that will take a user inputted number, space the numbers out, then add them to a total sum. What I am having a hard time with is when I enter a negative number. I can't figure out what I need to do to have my program ignore the "-" in the string.

import java.util.*;
public class Week4_Programming_Problem
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
int number, digit, sum=0;
char digitPos;

[code]....

View Replies View Related

Can't Get Negative Values Working

Nov 11, 2014

when i input a positive integer it works but when i input a negative number it doesn't work

my pseudo code:

READ input
WHILE( NOT CORRECT INPUT)
READ INPUT AGAIN;
ENDWHILE
DECLARE array arr[input]
FOR(i=0 to input-1)
arr[i]= Random number from 0 to 100;
ENDFOR
DISPLAY ARRAY

error message when i input -5 : Exception in thread "main" java.lang.NegativeArraySizeException atPosNeg.main<PosNeg.java:36>
 
import java.util.*;
class PosNeg{
 public static void main(String args[]) {
  Random generator = new Random();
Scanner scan = new Scanner(System.in);

[code]....

View Replies View Related

Removing Negative Number From Array

Jul 20, 2014

Ask the user to enter a sequence of at most 20 nonnegative integers. Your program should have a loop that reads the integers into an array and stops when a negative is entered (the negative number should not be stored). Invoke the average method to find the average of the integers in the array (send the array as the parameter).

how can I remove the negative number from the array and calculate the average of the posive elements without the negative ones? This is my code so far...

import java.util.Scanner;
import javax.swing.JApplet;
public class Parameters
{
//-------------------------------------
//Calls the average and minimum methods
//with different numbers of parameters

[code]....

View Replies View Related

Accept Array Of Non-negative Integers And Return Second Largest Integer

May 22, 2015

Write a function that accepts an array of non-negative integers and returns the second largest integer in the array.

Return -1 if there is no second largest.

The signature of the function is int f(int[ ] a)

Examples:

if the input array isreturn{1, 2, 3, 4}3{{4, 1, 2, 3}}3{1, 1, 2, 2}1{1, 1}-1{1}-1{}-1 

In the signature what I understood is, I should write my function with the given signature,

The return type is "int"

method name is "f"

parameter is "a" right ?
 
Writing my doubts beside the particular line in the code

public static void main()  // In the answer why they didn't use the class ?

In main method why they didn't use parameters ?(String[] args)

{
a1(new int[]{1, 2, 3, 4}); // what is "a1" here is it array name ? this line initializing the array ?
a1(new int[]{4, 1, 2, 3});
a1(new int[]{1, 1, 2, 2});
a1(new int[]{1, 1});
a1(new int[]{1});
a1(new int[]{});


static int a1(int[] a) // what is "a" here parameter ? and "a1" is method name ? why they used the array name and method name same ?

{
int max1 = -1;
int max2 = -1;
for (int i=0; i<a.length; i++)

[Code] .....

View Replies View Related

Method That Returns New Array By Eliminating Duplicate Values In Array

Jun 15, 2014

Write a method that returns a new array by eliminating the duplicate values in the array using the following method header: public static int[] eliminateDuplicates(int[] list). The thing is that I found the working solution that is written below, but how it works. How to eliminateDuplicates method done with flag and flag2.

Here is the code:

Java Code:

import java.util.Scanner;
public class Exercise06_15 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Enter ten numbers: ");

[code]....

View Replies View Related

Transfer Random Array Values To A Separate Array?

Feb 16, 2015

filling out a Random array: An Array of Specific Length Filled with Random Numbers This time what I need to do is take the elements from this Random array and assign them to a new Byte array:

for(int i = 0; i < limit-10; i++) {
Random dice = new Random();
int randomIndex = dice.nextInt(array.length);
if (array[randomIndex] < 128) {
System.out.print(array[randomIndex] + " ");
} else if (array[randomIndex] >= 128) {
System.out.print(array[i] + " ");
}

byte[] noteValues = new byte[]

{ 64, 69, 72, 71, 64, 71, 74, 72, 76, 68, 76 }; //This is the byte array filled manually!

I've tried amending the manual input to fit in with the Random array, as follows:

byte[] noteValues = new byte[]
{ array[randomIndex] };

In this case, however, the Byte array can't interpret the int values. Also, if the Byte array is outside the 'for' loop, array[randomIndex] cannot be resolved.

View Replies View Related

Compare Int With Array Of Values

Dec 24, 2014

I need to compare an int with an array of values generated with a for loop previously. I have something like that for the search..

for( int i = 0; i < 5; i++){
System.out.print("Indovina.. inserisci un valore: ");
// I memorize the value taken input
n = sc.nextInt();

[Code] ....

Also, I need to print the array each time I insert a value that is in the array. But, hiding the values still not "guessed".

View Replies View Related

Isolate Values From Array?

Feb 14, 2015

I'm trying to isolate specific values produced from that array at random. For example, if I were to have an array whose starting inputs are 5 & 10, the output is 5, 10, 15, 25, 40, 65 (the array stops before exceeding 100). Following this, I would generate 6 random numbers from this array (if the array is longer or shorter an equal number of random values from those arrays are generated) allowing for possible repetition of numbers.

So far, I have imported the Random utility and placed the following code below yesterdays code:

System.out.println();
for(int i = 0; i < limit; i++) {
if (array[i] < 100) {
System.out.println();
System.out.println("Rand. no. from array");
Random dice = new Random();
System.out.print(dice.nextInt(array[i])); //Call the Fibonacci array & generate rando numbers from it!!
}
}

Using the above (5, 10) array as an example, the output seems to generate 6 results for each position, but the random element is localised to each number, rather than the whole array. So, at position one we have number 5 and 'any' number between 1 & 5 is generated, rather than any 'specific' number from the 'whole' array. At the second position we have 10 and the printout will give the 2nd random number as anything between 1 & 10, and so on for the rest of the array. Ideally, I'd be looking for something like: 5, 40, 5, 65, 40, 15.

View Replies View Related

Increment Array Values

Jul 5, 2014

I'm attempting to increment the values by 1 in an array of objects but I'm not able to increment with the increment operator.

for(int i=1;i<a.length;i++){
a[i].getHour(); hour = hour++;
a[i].getMin(); miinute = minute++;
a[i].getSec(); sec = sec++;
}

It just loops the value of hour without incrementing.

View Replies View Related

Values Are Not Being Passed To Array

Sep 29, 2014

I have this class:

package model;
import java.awt.Color;
import shapes.Oval;
import shapes.Rectangle;
import shapes.Shape;
import java.awt.Container;
import java.lang.reflect.Array;

[Code] .....

And as it is now, the values are not being passed into the shapeArray array. If I "hard code" two shapes into the array in this class, everything works fine later on, but I do not manage to pass values into the array from the createShape() method. I tried several approaches, nothing works.

View Replies View Related

Values In Array Not Stored?

Oct 6, 2014

I just forgot to increment n while trying to store the humidity... I do this every time and I suddenly realize what I did wrong ...

My problem is that after printing humidity[n] in the "Humidity(%)" row, it seems that humidity[n] becomes 0. I checked like this:

System.out.println(humidity[5] + " " + humidity[6]);

In the "Humidity(%)" row, they come out fine, but when I do this, they come out as 0, which I think would explain why my heat indices are consistently lower than the temperature when the temperature is over 80.

My code:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class HeatIndex {
public static void main(String[] args) throws IOException {
System.out.printf("%70s", "Heat Index: Key West, Florida");

[Code] ......

View Replies View Related

Replacing Old With New Values In Array

May 24, 2014

how to replace the values in my array with the results of my function factorial.

public static void main(String[] args) {
//this is my main function:
int[] array = {5,4,3,2,1};
int i = 0;
System.out.print("results: ");
for (i = 0; i < array.length; i++){
System.out.print(factorial(array[i]));

[code]....

So, what I'm trying to do is change the contents of the array "array" into their factorial value. So, they should be replaced with {120,24,6,2,1}. then add those using linear sum but that's a different story.

View Replies View Related

Better Way To Remove Null Values From Array

Dec 1, 2014

Is there a better way to remove null values from an array than what I have tried? This works just fine, but I just get the feeling that there is a better way to do this, without using the JCF.

private static String[] removeNullValues(String[] list){
int count = 0;
for(int i = 0; i < list.length; i++){
if(list[i] == null)
count++;

[Code] ....

I technically dont need to remove the null values for the project that I'm working on (since I just print it out and I can avoid null values with a simple statement like

if(update[i] != null) SOP(update[i])
),

but I'm just curious.

View Replies View Related

Putting Values From 2D Array To HashMap

Apr 9, 2014

I have a 2D array and the elements are listed as follows:

outlook temperature humidity windy gooutside
sunny hot high false n
overcast hot high false y
....

I need to put these values into a HashMap, where the elements of the first row are the keys and the elements from row 1 to n-1 are the values. What would be the best way to make sure the key and values are matched correctly?

Here is what I have:

String[][] array = new String[numberOfRows][numberOfCols];
HashMap<String, String> map = new HashMap<String, String>();
for(int rows = 0; rows < (numberOfRows * numberOfCols); rows++) {
for(int cols = 0; cols < array[i].length; cols++} {
map.put(array[0][cols], array[rows*cols][col];
}
}

I keep getting the out of bounds error.

View Replies View Related

Array - How To Iterate Through Only Values Greater Than 0

Feb 9, 2014

I have an 46x9 array. I only have 108 values in the array for which I need to perform preliminary computations. How do I get the read to only read the 108 values whose values are greater than 0 and skip the other 495 whose values are 0?

View Replies View Related

Printing Random Values From Array List

Jun 15, 2014

I am having a hard time trying to figure out how to print random numbers from a an array list. I tried google but nothing worked. I have to pick certain values from two lists and print them on the screen. I have included comments in the code to facilitate the explanation.

import java.util.Random;
public class Parachute {
public static void main(String[] args) {
Random randomNumbers=new Random();
int number;
int array []={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};
char A[] = {'a', 'b', 'c','d','e','f','g','h', 'i','j','k','l','m','n','o','p','q'};

[Code]...

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved