Logic To Find Adjacency Of Numbers 0 And 1 In Array

Oct 3, 2013

I should mention here that number of elements can be from 1....10'000. And it has to be done as O(N)

I attempted at the problem like so :

public static int adjacencies(int[] A) {
int count = 0;
boolean found = false;
for(int i = 0; i < A.length-1; i++) {
if (A[i] == A[i+1]) {

[Code] .....

To me it looks right and it seems to work.

View Replies


ADVERTISEMENT

Find Out Logic Behind Bubble Sort

Mar 4, 2014

I am trying to find out the logic behind the Bubble sort,

for(a=1; a < size; a++)
for(b=size-1; b >=a ; b--){
if(nums[b-1] > nums[b]){
t=nums[b-1];
nums[b-1] = nums[b];

[code]....

View Replies View Related

Find Arithmetic Mean Of The Numbers Stored In Array

Feb 23, 2014

The program below is intended to find the arithmetic mean of the numbers stored in the array q in two ways: once by storing the numbers in an ArrayList d, where you allow all the necessary conversions to be performed automatically; and once by storing them in an ArrayList e, where you perform all the conversions by hand. Complete the program.

Here is what I have so far:

double[] q = { 0.5, 2.4, 7.4, 2.8, -6.2 };
ArrayList<Double> d = new ArrayList<Double>();
ArrayList<Double> e = new ArrayList<Double>();
for ( double x : q ) {
d.add( x );
e.add ( new Double ( x ) );

[Code] .....

Why does it still show "a / d.size?" I thought I fixed that. Whatever, it's supposed to be "dTotal / d.size()", etc.

View Replies View Related

Prim's Algorithm Using Adjacency Matrix Array Indexing

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

Trying To Find 3 Largest Numbers In Array Using Single For Loop

Feb 5, 2014

I am trying to find 3 largest numbers in an array using a single For loop but my following code is still showing randomly sorted numbers.

public class largest3 {
static int m, n, o;
static int array[] = new int[100];
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
array[i] = (int) (Math.random() * 100);

[Code] ....

View Replies View Related

Array Is A Set Of Numbers In Triangle - Find Maximum Path

Aug 12, 2014

If you aren't familiar with the euler problem 67, you are given an array that is a set of numbers in a triangle. Like this

3
7 4
2 4 6
8 5 9 3

and you have to find the maximum path, which for this one is

(3)
(7) 4
2 (4) 6
8 5 (9) 3

I have solved this problem iteratively with the code below

depth = depth-2;
while (depth >=0) {
for (int j = 0; j <= depth; j++) {
values[depth][j] += Math.max(values[depth+1][j], values[depth+1][j+1]);
}
depth -= 1;
}

depth is a variable for the row in the triangle. My problem is that i need the solution to be recursive and i am having trouble doing this. So far i have

public static int findMax(int[][] array,int depth) {
if (depth==0)
return array[0][0];
else if
}

View Replies View Related

User To Input 10 Numbers - Find Smallest Element In Array

Dec 2, 2014

I am trying to write a code that asks the user to input ten numbers and then finds and displays the smallest number out of the ones given. I am supposed to implement arrays into the program to do this. But the problem I have run into is that when I compile the code in jgrasp, I am given several error messages and I am not quite sure what I have done wrong. I'm assuming it is either a syntax or a logical error on my part but reading over the code I do not understand what is causing these errors.

This is the most current draft of my code:

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

[Code] .... 
 
/* Sample Run:
Enter ten numbers: 1.9, 2.5, 3.7, 2, 1.5, 6, 3, 4, 5, 2
*/

And these are the exact error messages:

----jGRASP exec: javac -g Exercise7_9.java
Exercise7_9.java:35: error: '.class' expected
if (double m > list[i]) {
^
Exercise7_9.java:35: error: illegal start of expression
if (double m > list[i]) {

[Code] .....

View Replies View Related

Clarification On Array Logic

Mar 3, 2014

explain me the logic behind this array..how this logic flows

min=max=nums[0];
for(int i=1;i<5;i++){
if(nums[i] < min) min=nums[i];
if(nums[i] > mzx) max=nums[i];

//Find the minimum and maximum values in an array.

class Minmax{
public static void main(String[] args){
int nums[] = new int[10];
int min, max;

[code]....

View Replies View Related

Generate 100 Numbers Using Arrays - Sort Even Numbers Into Separate Array And Display Both

Apr 24, 2014

I've just written a program that generates 100 numbers that range from 0 ~ 25 using arrays, the program calls another method that sorts the even numbers into a separate array and returns the array. I need it to display both arrays, however, when I run my program, the numbers of both arrays are mixed together, and I'm not sure how to separate them.

[ public class Array1
{
public static void main(String[] args)
{
int array [ ] = new int[100];
for (int i = 0; i < array.length; i++)
{
array[i] = (int) (Math.random() * 26);

[Code] .....

View Replies View Related

Adjacency List Graph

May 4, 2014

I'm trying to read a text file of a graph and print information about the graph including the order and size of the graph, rather it is a directed or undirected graph, if it is directed the in and out degree, and the and a list of all vertices for which it is adjacent. The problem is the adjacency list is just printing out the list of vertices instead of the adjacency list. Is there something in my code that is problematic? Also I'm unsure of how to go about the in and out degree

"graphs.txt"
6,1
0,2 0,4 1,4 1,5 2,1 2,3 2,5 3,2 3,4 4,1 4,5 5,1 5,3 5,2
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner; 
public class AdjList {
private Node first; // beginning of list

[Code

View Replies View Related

How To Find Biggest Two Int Numbers From Four

May 23, 2014

So, if you are a football/soccer fan, you know that in every FIFA World Cup you have 4 nations. From those 4, 2 will qualify to the Knockout stages.

Let's say, after all the group fixtures are played, the standings are as follows :

1. England - 6 points
2. Spain - 4 points
3. Brasil - 3 points
4. Germany - 1 point

I want to know how I can select ONLY England and Spain for the Knockout stage of the FIFA World Cup (The class I am working on).

View Replies View Related

Find Sum Of Numbers In A Random String?

Jun 17, 2014

Writing a Java program to find sum of numbers in a random string.

(Input=abc235^%$q!12&3 output=250)

If a number has - sign then u should consider it as a negative number.(Input=abc-12t%^&$dcf22 Output=10)

View Replies View Related

Write A Program That Find The Sum Of Two Numbers 62 And 99?

Jan 23, 2015

I have to write a program that find the sum of two numbers 62 and 99 and stores them in a variable named total. However, I have one error that I just can't get rid of and can't tell what it is. I'm using jGrasp and here's what it says:

Programming Challenge #5.java:14: error: class SumofTwoNumbs is public, should be declared in a file named SumofTwoNumbs.java
public class SumofTwoNumbs {
^
1 error
  ----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

and here: is my code:

import java.util.Scanner; 
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
 /**
// This program has variable of several of the integer types.
public class SumofTwoNumbs {
public static void main(String[] args) {

[code]....

View Replies View Related

Program To Find If Numbers Are Consecutive Or Not

Feb 23, 2014

im trying to do a program to find if numbers are consecutive or not! if they are consecutive i need a true as a return and a false when they are not... i did this program and im sure i did something wrong because i keep only true returns ..

Example: (3,5,7) (1,2,2) (7,7,9) should return a false!
Java Code: import java.util.*;
public class Consecutive{
public static void main (String [] args){
Scanner console= new Scanner(System.in);
System.out.println("Enter three numbers");
String numbers = console.nextLine();
System.out.println( "The numbers (" + numbers + ") is '" + consecutive( numbers ) + "'" );
}//end of main

[code]...

View Replies View Related

Find Maximum Between Three Numbers Using Ternary Operator?

Aug 23, 2014

Write a program to find maximum between three numbers using ternary operator.

View Replies View Related

Sieve Code To Find Prime Numbers

Jan 16, 2015

I am doing a sieve code to find prime numbers.

public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println(" Enter the maximum number ");
int maxNumber = scanner.nextInt();
int[]numberList = createList (maxNumber);

[code]....

I have a problem with the output. It only get as far as marking the multiples of 2. It does not mark the multiples of 3 and so on.Example output: If the range is from 2 to 15 the output is :2, 3, 0, 5, 0, 7, 0, 9, 0, 11, 0, 13, 0, 15 note: 0 means its marked.As you can see it only marks multiples of 2 but it does not mark multiples of 3 above. I think the problem lies in the for loop inside my main method, because the index does not increment.

View Replies View Related

Find Largest Num Of Numbers In Scanned File

Jul 10, 2014

I need to find the largest value in a scanned file.I've gotten the count, sum, average, evens, and odds myself.The code above the while loop is not mine and my professor said I may no edit it or other wise mess with it. I also may not use arrays.Also I've realized that the largest/smallest are recording the value of count. I've tried the following:

Scanner infile = new Scanner ( new FileReader(args[0]) );
int count=0,sum=0, largest=Integer.MIN_VALUE,smallest=Integer.MAX_VALUE, evens=0, odds=0;
double average=0.0;

[code]....

View Replies View Related

Reading In A File With BufferedReader / Using Tokenizer For Adding Into Adjacency Matrix

Mar 6, 2015

I am having trouble adding numbers read from a file with BufferedReader, using Tokenizer to split the numbers up by " " - space and adding them to an adjacency matrix.Below is the text file and my code, that I have at the moment.

0 1 0 0 1 1 0 0
1 0 0 0 0 1 1 0
0 0 0 1 0 0 1 0
0 0 1 0 0 0 0 1
1 0 0 0 0 1 0 0
1 1 0 0 1 0 0 0
0 1 1 0 0 0 0 1
0 0 0 1 0 0 1 0

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
public class Foo
{
@SuppressWarnings("null")
public static void main(String[] args) throws Exception
{
String line, token = null, delimiter = " ";

[code]....

View Replies View Related

How To Find Smallest Number In A Group Of 3 Numbers Using While Loop

Mar 23, 2015

import java.util.Scanner;
public class AvgLrgSml{
public static void main(String[]args){
System.out.print("Hello there. Please enter any three numbers.");
Scanner keyboard = new Scanner(System.in);
double num1 = keyboard.nextDouble();
double num2 = keyboard.nextDouble();

[Code]...

View Replies View Related

How To Get Shortest Path Between Two Nodes In Adjacency Matrix Using Undirected Weighted Graph

Apr 26, 2014

how to get shortest path between two nodes in adjacency matrix using with undirected weighted graph using BFS algoritham java program??

View Replies View Related

Unable To Find Prime Palindrome Numbers Less That A Number By Program

Oct 10, 2014

I want to find the prime palindrome numbers less that a given number by my program. Here is my code, I am trying to find the method to solve the errors when I compile it. It said variable a might not have been initialized in line 41,62,86.

import java.util.Scanner;
public class Lab5{
public static void main (String[] args){
System.out.println("Please input a number");
Scanner Input=new Scanner(System.in);
int num = Input.nextInt();

[Code]...

View Replies View Related

Prime Numbers - Find N From List That Is Not Crossed And Cross Out All Its Multiples

Jun 20, 2014

the prime numbers from 1 to 2500 can be obtained as follows. From a list of the numbers of 1 to 2500,cross out al multiples of 2 (but not 2 itself). Then, find the next number (n, say) that is not crossed out and cross out all multiples of n (but not including n).

Repeat this last step provided that n has not exceeded 50 (the square root of 2500). The numbers remaining in the list (except 1) are prime. Write a program which uses this method to print all primes from 1 to 2500. Store your output in a file called primes.out.

View Replies View Related

Create 2D Array Out Of CSV File And Find Number Of Elements To Determine Array Size

Mar 24, 2015

I am taking the Class Algorithms and Datastructures and got an assignment for Lab that really throws me off. The goal is to create an Array out of a given CSV file, implement several Methods that get the size of array, etc.

I am still stuck in the first part where the CSV has to be imported into the Array. My problem is that I need a mechanism that figures out the needed size for the Array, creates the array, and only then transfers the data from the CSV.

The list consists of the following wifi related values:

MAC-Adress, SSID, Timestamp, Signalstrength.

These are on the list, separated by comma. The Columns are each of these, and the rows are the four types of values making up the information on a certain wifi network.

The catch is, we are not allowed to use any of the following:

java.util.ArrayList
java.util.Arrays
and any class out of java.util.Collection.

So far I used the BufferedReader to read in the file and tried to implement the array, but I get an arrayindexoutofboundsexception.

Below is my Code (Its still an active construction zone):

public class WhatsThere {
public WhatsThere(String wifiscan) throws IOException {
}
public static void main(String[] args) throws IOException {
// WhatsThere Liste = new WhatsThere(String wifiscan);
String[][] arrayListe = new String[0][0];

[Code] ....

View Replies View Related

If / Else Expression Logic

Oct 12, 2014

The program i am working on is to take string from the user which should be a phone number then it will return true or false based upon the method that checks to see if it meets the criteria of a certain format if does not it will return false otherwise return true. I am working on a if/else structure that will print this number is valid if it meets the criteria of the method i previously mentioned or return the number is not valid if it is false. the logic of the expression i put in the parentheses of the if/else statement. This is what i have so far:

if(){
System.out.println("The phone number is valid");
}
else {
System.out.println("This isn't a valid phone number");
}

Do i need to compare it based upon the method that checks if it is true?

View Replies View Related

How To Find Corners Of Array

Nov 10, 2014

So as part of my program I have to find the corners of an array (the array can vary in size but is always square) but I'm not exactly sure how to do this. I thought of one possible way but I am not sure if I can do it or how to write it,

if array[row][col] = [0][0] or [row length][col length] or [0][col length] or [row length][col length].

I'm just giving examples of what I am trying to find here I know none of these are actual syntax. I don't even know if I can check whether an array's coordinates are equal to coordinates on that array or not. How I can write this or if there is another way to do it? If yes, what about edges of the array?

View Replies View Related

Find The Longest Decreasing Sub-array

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







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