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
ADVERTISEMENT
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
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
View Related
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
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
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
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
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
Apr 27, 2014
I am working with an Adjacency Matrix to try to find the MST of a graph. Along the way I have hit a snag that I am not sure how to get around. When running the program I will parse through each row of the matrix and find the smallest weight. However when trying to reset the row at the end of the lowest sort I cannot move to the next row.
The graph looks like this:
My Matrix was created from the graph and I have determined by starting at Vertex W my path should looks like this:
W->R->D->H->G->S->C->B->A
View Replies
View Related
May 6, 2014
I'm trying write a Dijkstra's implementation in java. First off, here is the algorithm:
package lab3;
import java.util.HashMap;
import java.util.Stack;
/**
* Compute shortest paths in a graph.
*
* Your constructor should compute the actual shortest paths and maintain all the information needed to reconstruct them. The returnPath() function should use this information to return the appropriate path of edge ID's from the start to the given end.
*
* Note that the start and end ID's should be mapped to vertices using the graph's get() function.
*/
class ShortestPaths {
Multigraph graph;
final int INF = Integer.MAX_VALUE;
PriorityQueue<Integer> Q;
[Code] ....
I followed someone else psuedocode very closely but for whatever reason, my edge[] array is just full of null data, which means I can't actually return the shortest path. Why that's happening/how to fix it? Maybe I'm not understanding dijstra's correctly.
View Replies
View Related
Mar 24, 2014
What would be a good and simple algorithm to find the shortest route between two points in a 2D array[grid] ? There can be certain obstacles in the grid i.e. some of the cells may be inaccessible. I tried googling for it and found that A* is the best for this but I am just a beginner and would like to start with something much simpler.
View Replies
View Related
May 11, 2014
I am using the shortest path algorithm to determine the connection between individuals within a given array. The array is written into the code and not read from external files.
When I am having problem is .. i am having problems how to prompt the user for the starting point or vertex and read that prompt to determine the starting point in the array. I know that this code :
computePaths(v0);
determines the starting point. i want to read "v0" from the user prompt.
total code being used
import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.io.IOException;
import java.util.Scanner;
class Vertex implements Comparable<Vertex>
[Code] .....
View Replies
View Related
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
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
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
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
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
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
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
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
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
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
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
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
Feb 7, 2015
I am trying to create an empty array that has no assigned length as the amount of elements it needs to hold will be dependent on another value. I then want to use a while loop to assign values to it. Here is an example of what im looking for it doesnt work. Iam trying to do:
int x = 12;
int i = 1;
int k = 0;
int[] factors = {}
while (i<x) {
if (x%i==0) {
factors[k] = i;
k++;
i++;
View Replies
View Related
May 24, 2014
I got a task from my teacher and the restriction is we are not able to modify this class (and that is the problem).This is the given class:
public class Jobs {
private intcounter= 0;
private final intnoElements= 20;
private final int[]a= { 11, 28, 31, 42, 49, 66, 67, 75, 89, 100, 102, 103, 114, 125, 130, 135, 140, 145, 150, 155 };
private final int[]s= { 20, 9, 7, 6, 12, 15, 4, 7, 30, 22, 11, 45, 20, 6, 6, 5, 5, 5, 5, 5 };
[code]...
I need to compare some of the values of the given arrays. For example: if(a[4]<a[2])... etc.
How can I do these kind of operations to a private array? I have to compare the values in an new classPS: I have to compare the values in a new class
View Replies
View Related