How To Find Longest Descending Sequence Without Arrays
Oct 24, 2014
I am trying to find the longest descending sequence without arrays. So 65124976231 would output 9762.
import java.util.*;
public class HelloWorld {
public static void main(String[] args){
String num = "";
int longestLen = 0;
int currLen = 0;
String max = "";
[Code]...
I keep getting: The longest descending sequence is: 6512 In an infinite loop.
View Replies
ADVERTISEMENT
Oct 26, 2014
From a given array of positive and negative numbers, I have to find the longest SUB-Array which represents a Zig-Zag sequence...
A Zig-Zag sequence means that one number is possitive, the next one negative, the next one possitive, and so on and so on...
Like this: -1, 4, -5, 6, -9, 2, -9 etc....
So that means, if the array looks like this:
1, 4, -2, -5, 6, -9, 1, -4, 9, -8, 7, 4, -3
the longest sub-array which fulfills the requirement is (the part in bold):
1, 4, -2, -5, 6, -9, 1, -4, 9, -8, 7, 4, -3
and I only need it's length, which in this case is: 8
View Replies
View Related
Jul 13, 2014
My problem is that I can't even run the program, because it gives me
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at domashno.ten.longestSortedSequence(ten.java:37)
at domashno.ten.main(ten.java:17)
Code :
public static void main(String[] args) {
int[] arr = {3, 8, 10, 1,9, 14, -3, 0, 14, 207, 56, 98, 12};
longestSortedSequence(arr);
System.out.println(longestSortedSequence(arr));
}
public static int longestSortedSequence(int[] arr) {
[Code] ....
View Replies
View Related
Jan 28, 2015
Okay so I need to be able to read a file and then be able to find the longest sequence of increasing numbers.
So if the file was this, (this is Ass1Q2_test4.txt)
97 47 56 36 60 31 57 54 12 55
35 57 41 13 82 80 71 93 31 62
89 36 98 75 91 46 95 53 37 99
25 45 26 17 15 82 80 73 96 17
75 22 63 96 96 36 64 31 99 86
12 80 42 74 54 14 93 17 14 55
14 15 20 71 34 50 22 60 32 41
90 69 44 52 54 73 20 12 55 52
39 33 25 31 76 45 44 84 90 52
94 35 55 24 41 63 87 93 79 24
the output should be,
(5,0) with cost 12
(6,0) with cost 14
(6,1) with cost 15
(6,2) with cost 20
(7,2) with cost 44
(7,3) with cost 52
(7,4) with cost 54
(6,3) with cost 71
(5,3) with cost 74
(4,3) with cost 96
Greatest path is of length 10.
Now, the code that I have works, kind of. Instead of recurring several times at each point, it only recurs once.
So say I'm looking at (1,1). With (1,1) being 57. The area around it looks like this.
97 47 56
35 57 41
89 36 98
Now when I look at it, there are several paths it can take. It can go 57, 97 or 57, 89 or 57, 98. However, I'm pretty sure that it just uses the first one that corresponds with the first if statement that is valid. So I start checking north of the value, then northeast, then east, then southeast, which at southeast is where I find my first greater than value. After it finds it's first valid number, it then continues from that number, instead of checking if there are other longer paths stemming from the original value.
In conjunction with that, you can see that the printout just returns all paths from each value. Which isn't what I want. I need a way to store the longest current path, then check each path after to see if it's longer. If it is, it's replaced, if not, it stays the same.
I've also attached Ass1Q2_test4.txt
import java.util.*;
import java.io.*;
public class MaxIncreasingSub {
[Code].....
View Replies
View Related
Jun 28, 2014
I am trying to create a java program to sort an array in ascending and descending order. Here is the program I created :
import java.util.Arrays;
import java.util.Collections;
public class ArraySort
{
public static void main(String [] args) {
int [] num = {5,9,1,65,7,8,9};
Arrays.sort(num);
[Code]...
BUT I GET THE FOLLOWING EROOR ON COMPILATION
ArraySort.java:12: error: no suitable method found for reverseOrder(int[])
Arrays.sort(num,Collections.reverseOrder(num));
^
method Collections.<T#1>reverseOrder(Comparator<T#1>) is not applicable
[Code] .....
What's wrong with my program?
View Replies
View Related
Feb 18, 2015
I have some N*M matrix or N*N matrx , and there's a "worm" that can start from any index in the first column, or, any index in the first row. i need to choose the longest worm that satisfying this :
The index that comes after the index before him must be greater then 1. and i must use recursion, also for helper methods. no loops at all. that's it. i'm having a problem to find the longest worm in the matrix.
My idea was to create an helper array and assign to the array indexes a variable that will count the steps of the worm and finally assigns in to the helper array index. then i used findMax method to find the max value in an index. and thats will be the longest worm. i wrote a lot of code so i wont put it here. i will say that i'm close. if the longest worm is 6 i get in my output 7.
View Replies
View Related
Oct 30, 2014
I have a given array of numbers, and I have to find the longest decreasing sub-array, but the thing is that the elements don't have to be next to each other!
For example, the longest decreasing sub-array for this array : 546 -156 165 -156 -56 -13 5
is 3 (check the bold numbers)
Until now, I have nearly finished my code, but I have one problem...
private static int decreasing(int[] a) {
int result=1, temp=0, br=1;
//Checking all the elements one by one:
for(int i=0;i<a.length;i++){
temp=a[i]; //placing the element on a temp value
[Code] ....
The problem with this code is that it's not smart.. let's say I have : 100 -500 90 80 70
Once it hits -500, none of the other if's with pass....
View Replies
View Related
Feb 12, 2014
public class Fibonacci {
public static void main(String[] args) {
int[] numbers;
numbers = new int[20];
numbers[0] = 0;
numbers[1] = 1;
System.out.println("
[Code] ....
I wrote this program for my Java class to print out the first 20 numbers of the Fibonacci series. My assignment then tells me to Rewrite your program using dynamic array. I'm not sure how to do this.
View Replies
View Related
May 14, 2015
I am not sure what is wrong with my code? The code produces a negative number when I type in 4000000 into the fib method parameter.
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Java Code:
public static void fib(int k) {
int result = 0;
int[] array = new int[k];
array[0] = 1;
array[1] = 2;
for (int i = 2; i < k; i++) {
[Code] ....
View Replies
View Related
Feb 19, 2015
I have created an object of arrays, and i need to find the max value...
View Replies
View Related
Dec 31, 2014
i want a simple,beginner program to accept a string from the user and display the longest word in the string.i thought of taking the length of the string and choosing the longest string.but if the user enters a big paragraph,how can i store each word in a variable??
View Replies
View Related
May 6, 2014
I have not a clue how to convert this to ascending from its current descending....
class Node
{
private int iData; // data item (key)
//-------------------------------------------------------------
public Node(int key) // constructor
{ iData = key; }
//-------------------------------------------------------------
public int getKey()
{ return iData; }
[Code] .....
View Replies
View Related
Apr 10, 2014
I am sorting an array in ascending and descending order. I am using the nethods in Arrays as below
Arrays.sort(myArray)
I want to print both input and output array in sysout.
Object[] ipArray = [45,8,32,41,11,7];
Object[] opArray;
Object mArray = ipArray;
Arrays.sort(mArray); This is changing the ipArray too ?
How can I get my input array unmodified ?
View Replies
View Related
Oct 7, 2014
I am working on descending after I assign values. And everything is sorted, but one value. I don't know why always I get one value is not sorted.
public class SeunghunJunHw6 {
public static void main(String[] args){
double[] randomArray = new double[9];
//assign random double values between 0 to 100 but exclude 100
for(int i = 0; i<randomArray.length;i++){
randomArray[i] = Math.random()*100;
[Code] ....
View Replies
View Related
Sep 12, 2014
I have the following double linked list and I'm supposed to order it descending (reverse) using the printInReverse() method; since the list orders itself ascending when the numbers are added, how could I order it descending in this method? Here's the code without implementing descending/reversing methods:
package test;
import java.util.Scanner;
public class MyList {
private Scanner userInput;
private Integer selectedOption;
private MyListElement firstElement = null;
private boolean exitRequested = false;
[Code] ....
I tried to declare a previousElement variable but I didn't figure out how I'd do that.
View Replies
View Related
Jun 13, 2014
How to input 10 integer elements in an array & sort them in desc. order using the bubble sort technique.
View Replies
View Related
Sep 12, 2014
I wrote displayAscending() and displayDescending() methods to this double linked list and it is not working at all. Logically it seems fine to me. I positioned the head in the beginning in the ascending method; created a variable named data1 as an auxiliar variable so it can store the values that are going to be moved; and moved the values. Same thing for the descending method but instead of the head I put the tail and move left the list, instead of right.
import java.util.*;
class node {
int data;
node left;
node right;
node(int d, node l, node r) {
data = d;
[Code]...
View Replies
View Related
Nov 23, 2014
I had to make a program that allowed the user to enter the number of students and the students names and grades and then print out the name with the grade in descending order. right now I only have it where it prints the names out in descending order. how do I get it the print out with the grade?
Here is my code
import java.util.*;
public class Grades {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int numofstudents = input.nextInt();
[Code] .....
View Replies
View Related
Jun 28, 2014
I mainly would like to know is a int[4][4] or a int [4*4] is more efficient to use. Which takes more storage? Which requires more to process? that kind of stuff.
View Replies
View Related
Mar 28, 2014
I know my problem is my lack of understanding of OOP but I have searched for a solution and can't even seem to get close.This is a very simplified version of what I will need to do. I have a button and six textboxes. When the button is pressed I want to take the text from textbox6 and place it in textbox1, textbox2, textbox3, textbox4, and textbox5.
I need sometthing better then this brut force method.Java Code:
package textboxes;
public class TextBoxesGUI extends javax.swing.JFrame {
public String NewText;
public TextBoxesGUI() {
initComponents();
[code]....
View Replies
View Related
Jun 12, 2014
I what to check if value is not equal to CharSequence value
I try this but getting error:
Java Code:
private CharSequence testvalue = "";
if (!isEqualTo("BCDEF".contains(testvalue))){
}
public boolean isEqualTo(String s_src, String s_compareTo) {
boolean flag = false;
[Code] .....
View Replies
View Related
Mar 23, 2014
Fibonacci Sequence Code:
public class FibSeqByIanNeumann {
public static void main(String[] args) {
Scanner get = new Scanner(System.in);
int ctr, num1, num2, fib, maxTimes;
System.out.print("How many sequences do you want?: ");
maxTimes = get.nextInt(); //inputs the maxium limit of the fib sequence
[code]....
how to do a simple YES/NO Loop so I can try to have it work on my code if I want to try to do the Fibonacci Sequence again.Now I think it might have something to do with a do/while loop.
View Replies
View Related
Sep 17, 2014
I want to declare integers, while the program is running.
I run the program and then I give it via System.in.println an integer and repeat this as long as I want. I want the program to give those integers a name of a certain type for, for example a(i) or a[i], dunno, (it should be handy) and then a(i) represents the the i'th integer I gave the program. My idea is then that I can use those elements by their name just like, if I had declared them in the first place. For example add two integers together. For example I defined a method add+, which waits for 2 integer and then adds them. For example I write:
add
a(2)
a(47)
(then I would get here the result.)
However I don't know, how to let the program count the number of inputs or how to let it declare and use variables.
View Replies
View Related
Sep 17, 2014
I want to declare integers, while the program is running.
I run the program and then I give it via System.in.println an integer and repeat this as long as I want. I want the program to give those integers a name of a certain type for, for example a(i) or a[i], dunno, (it should be handy) and then a(i) represents the the i'th integer I gave the program. My idea is then that I can use those elements by their name just like, if I had declared them in the first place. For example add two integers together. For example I defined a method add+, which waits for 2 integer and then adds them. For example I write:
add
a(2)
a(47)
(then I would get here the result.)
I don't think implementing the add function is difficult. However I don't know, how to let the program count the number of inputs or how to let it declare and use variables.
View Replies
View Related
Jan 6, 2014
Jdeveloper 11.1.1.7.0
Currently I am having an arrayList which consits of the yearweekno.
Example: 201303, 201402, 201401,201352,201351,201350,201349.
which means 01, 02,52,....48 are the weeknumbers.
No my question is if I have today's date i.e new java.util.Date...i.e week 01.
How do I arrange the ArrayList as below
201403 201402 201401 201352 201351 201350 201349
That is if current yearweek is 201401 then display should be as above(current week+2, current week, currentweek -4).
View Replies
View Related
Jan 30, 2015
I'm trying to figure out how to print the Major Scale of any give note in music. It works as follows:
There are 12 notes and each note is assigned a number
C = 0, C# = 1, D = 2, D# = 3, E = 4, F = 5, F# = 6, G = 7, G# = 8, A = 9, Bb = 10, B = 11.
After picking a random note, say F, the idea is to add the following sequence to move through the scale:
0, +2, +2, +1, +2, +2, +2, +1.
So, with F being 5, we move through the scale as 5, 7, 9, 10, 0, 2, 4, 5.
The theory is unimportant but, as you can see, the sequence loops back on itself, rather than continue past 11. This is where I'm stuck, though. I'm not clear on how to loop my numbers around in code. I'll show my attempt here:
public static void main(String[] scale) {
majorScaleOf(0); //0 represents C!!
majorScaleOf(5); //5 represents F!!
majorScaleOf(10); //Bb = B flat!!
[Code] ....
For the sake of simplicity I haven't shown the entire script of 'if' statements, but you get the idea.
The print out reads as:
0, 2, 4 (For C)
5, 7, 9 (For F)
10 (For Bb)
The last line demonstrates the problem, as I need it read as 10, 0, 2.
I've tried different approaches, but wanted to convey the basic problem with this post.
View Replies
View Related