Arithmetic Sequences program - java

I want to write a program Sequence that reads an arbitrary number of command­line ints and stores them inside an array.
The program then looks for 3 numbers in the array that form an arithmetic sequece of length 3.
For example:
% java Sequence 20 8 27 19 10 56 7 12 98
The numbers 8, 10, 12 located at indices 1, 4, 7 form an arithmetic sequence
This is my code until now but it doesn't work:
public class Sequence {
public static void main(String[] args){
int[] arr = new int[args.length];
for(int i = 0; i < arr.length; i++){
arr[i] = Integer.parseInt(args[i]);
}
// Process
for(int a = 0; a < arr.length; a++){
for(int b = 1; b < arr.length; b++){
for(int c = 2; c < arr.length; c++){
if (arr[b] - arr[a] == arr[c] - arr[b]){
System.out.println(a + " " + b + " " + c);
}
}
}
}
}
}
When I run this it shows me this:
1 4 7
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8

You have two major issues:
You are not printing the numbers that you find, but their indexes instead. You need to change System.out.println(a + " " + b + " " + c); to System.out.println(arr[a] + " " + arr[b] + " " + arr[c]);
Your indexing is not correct. For each index from the second on, you end up getting the same number three times, which always builds a sequence.
Here is the correct code:
public class Sequence {
public static void main(String[] args){
// 20 8 27 19 10 56 7 12 98
int[] arr = new int[args.length];
for(int i = 0; i < arr.length; i++){
arr[i] = Integer.parseInt(args[i]);
}
for(int a = 0; a < arr.length; a++){
for(int b = a + 1; b < arr.length; b++){
for(int c = b + 1; c < arr.length; c++){
if (arr[b] - arr[a] == arr[c] - arr[b]){
System.out.println(arr[a] + " " + arr[b] + " " + arr[c]);
}
}
}
}
}
}

What you want is tot start index b at a+1 and index c at b+1 in order to avoid using certain numbers double. Although I must say, the sequences you printed are in fact correct as "5 5 5" is an arithmetic sequence. Adding this little change leads to:
public class Sequence {
public static void main(String[] args){
int[] arr = new int[args.length];
for(int i = 0; i < arr.length; i++){
arr[i] = Integer.parseInt(args[i]);
}
// Process
for(int a = 0; a < arr.length; a++){
for(int b = a+1; b < arr.length; b++){
for(int c = b+1; c < arr.length; c++){
if (arr[b] - arr[a] == arr[c] - arr[b]){
System.out.println(arr[a] + " " + arr[b] + " " + arr[c]);
//print the sequence and not the index
}
}
}
}
}
}

Related

Array sum rows and colums of an array

import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
int n, m, sumRows= 0, sumColumns= 0, i = 0, j = 0; //rows(n), Columns(m)
n = Integer.parseInt(JOptionPane.showInputDialog(null, "Rows"));
m = Integer.parseInt(JOptionPane.showInputDialog(null, "Columns"));
int[][] a = new int[n][m];
int[] b = new int[n];
int[] c = new int[m];
for(i = 0; i < a.length; i++) {
for(j = 0; j < a[i].length; j++) {
a[i][j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Type"
+ "an int. A[" + i +"]" + "[" + j + "] = "));
sumRows+= a[i][j];
sumColumns+= a[j][i];
if(j == a[i].length-1) {
b[i] = sumRows;
sumRows= 0;
}
if(i == a.length-1) {
c[j] = sumRows;
sumRows= 0;
}
System.out.println("Sum Rows: " + sumRows+ " Vector B" + i + ": " + b[i]);
System.out.println("Sum Columns: " + sumColumns + " Vector C" + j + ": " + c[j]);
}
}
}
}
So i have to sum the rows and colums and store them on two vectors, i have to store the sum of the rows in Vector B, and the sum of the columns in Vector C.
The sum of the rows works perfectly, but i can't get to work the sum of the columns.
Try this:
public class Main {
public static void main(String[] args) {
int n = Integer.parseInt(JOptionPane.showInputDialog(null, "Rows"));
int m = Integer.parseInt(JOptionPane.showInputDialog(null, "Columns"));
int[][] a = new int[n][m];
int[] b = new int[n];
int[] c = new int[m];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = Integer.parseInt( JOptionPane.showInputDialog(null, "Type" + "an int. A[" + i + "]" + "[" + j + "] = "));
b[i] += a[i][j];
c[j] += a[i][j];
}
}
// USED FOR PRINTING
// -------------------------
for (int i = 0; i < b.length; i++) {
System.out.println("Sum Row " + (i + 1) + " is " + b[i]);
}
for (int i = 0; i < c.length; i++) {
System.out.println("Sum Column " + (i + 1) + " is " + c[i]);
}
// -------------------------
}
}
INPUT:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
OUTPUT:
Sum Row 1 is 10
Sum Row 2 is 35
Sum Row 3 is 60
Sum Row 4 is 85
Sum Column 1 is 30
Sum Column 2 is 34
Sum Column 3 is 38
Sum Column 4 is 42
Sum Column 5 is 46

How to make the for inner loop more efficient?

I am new to Java and there's this one question that makes me wonder. How to make the for inner loop more efficient in this code?
for (int i = 2; i <= 100; i++) {
System.out.print("Factors of " + i + " is: ");
for (int j = 2; j < i; j++) {
if (i % j == 0) System.out.print(j + " ");
}
System.out.println();
}
I was just trying to get the factors of numbers from 2 to 100 but how can i make the inner loop more efficient?
It's a little bit number theory involved here but if you do this it would be efficient specially when the 100 is replaced with something much bigger:
for (int i = 2; i <= 100; i++) {
System.out.print("Factors of " + i + " is: ");
for (int j = 2; j <= (int) Math.sqrt(i); j++) {
if (i % j == 0) System.out.print(j + " " + i / j + " ");
}
System.out.println();
}
You could use the fact that for every divisor a of i there is a number b such that a * b = i.
Find all divisors a <= sqrt(i) and save b = i/a and print these values later.
final int num = 100;
int[] divisors = new int[(int) Math.sqrt(num)];
for (int i = 2; i <= num; i++) {
System.out.print("Factors of " + i + " is: ");
int j = 2;
int index = 0;
for (; j * j < i; j++) {
if (i % j == 0) {
System.out.print(j + " ");
divisors[index++] = i / j;
}
}
if (j * j == i) {
// print sqrt(i) only once, if it's integral
System.out.print(j + " ");
}
while (--index >= 0) {
System.out.print(divisors[index] + " ");
}
System.out.println();
}
This way your inner loop needs only O(sqrt(i)) instead of O(i) operations.
This code time complexity is O(N2).
public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
System.out.print("Factors of " + i + " is: ");
for (int j = i/2; j > 1; j--) {
if (i % j == 0) System.out.print(j + " ");
}
System.out.println();
}
}
Try this,as your code output will be displayed as follows (ascending order)
Factors of 24 is: 2 3 4 6 8 12
please be noticed, but this given code will be displayed output as follows (descending order )
Factors of 24 is: 12 8 6 4 3 2

How to multiply the elements of a column in a 2D array in Java?

i have a 2D array 3X5 and i need the to multiply each element in column one, and so forth. This is what ive attempted without any luck. the result is not correct. i tried storing each column into an array and multiply each element from that array but i get the same results.
edit: yes i am aware there is no multiplication in this code, that is because it yields an incorrect product.
for(int j = 0; j < 5; j++){
double v = 0.0;
double[] ex = new double[3];
double volumeBox1 = 0.0;
for(int i = 0; i < 3; i++){
v = d[i][j];
System.out.println(v);
for(int z = 0; z < 3; z++){
ex[z] = v;
}
}
System.out.println("The volume of box " + (j+1) + " is: " + volumeBox1);
I will assume your matrix is 5 x 3, which is more logical and convenient than 3 x 5 for this use case :
for (int i = 0 ; i < d.length ; j++) {
double vol = 1;
for (int j = 0 ; j < d[i].length ; j++) {
vol *= d[i][j];
}
System.out.println("The volume of box " + (j + 1) + " is: " + vol);
}
This can of course be done with a 3 x 5 matrix but I think it makes less sense to iterate on the columns :
for (int j = 0 ; j < d[0].length ; j++) {
double vol = 1;
for (int i = 0 ; i < d.length ; i++) {
vol *= d[i][j];
}
System.out.println("The volume of box " + (j + 1) + " is: " + vol);
}

how to add two 1-d array and arrange it using for loop?

The user should input 10 integer values for list1 and 10 integer values for list2. Your program should add the contents of list1 and list2 then store the sum to list3. Your program should display horizontally the values of list1, list2, and list3. Use loops.
here is the output:
List1 : 1 3 2 5 7 8 5 6 9 4
List2 : 2 1 4 3 2 1 4 2 0 2
List3 : 3 4 6 8 9 9 9 8 9 6
here is my code
import java.io.*;
public class List {
public static void main(String[] args) {
int list1[] = new int[10];
int list2[] = new int[10];
int i, j, k, num = 0, num1 = 0, num2 = 0;
String input = " ";
BufferedReader in = new BufferedReader(new
InputStreamReader(System. in ));
for (i = 0; i < 10; i++) {
list1[i] = 0;
}
for (j = 0; j < 10; j++) {
list2[j] = 0;
}
try {
for (i = 0; i < 10; i++) {
System.out.print("Input value for list[" + i + "] = ");
input = in .readLine();
num = Integer.parseInt(input);
list1[i] = num;
}
for (j = 0; j < 10; j++) {
System.out.print("Input value for list[" + j + "] = ");
input = in .readLine();
num1 = Integer.parseInt(input);
list2[j] = num1;
}
} catch (IOException e) {}
System.out.print("list1: ");
for (i = 0; i < 10; i++) {
System.out.print(list1[i] + "\t");
}
System.out.println();
System.out.print("list2: ");
for (j = 0; j < 10; j++) {
System.out.print(list2[j] + "\t");
}
System.out.println();
int list3[] = new int[10];
list3[0] = list1[0] + list2[0];
list3[1] = list1[1] + list2[1];
list3[2] = list1[2] + list2[2];
list3[3] = list1[3] + list2[3];
list3[4] = list1[4] + list2[4];
list3[5] = list1[5] + list2[5];
list3[6] = list1[6] + list2[6];
list3[7] = list1[7] + list2[7];
list3[8] = list1[8] + list2[8];
list3[9] = list1[9] + list2[9];
System.out.print("list3: ");
for (k = 0; k < 10; k++) {
System.out.print(list3[k] + "\t");
}
}
}
my program is working correctly but i need to put the third list in loop can you help me?
In place of using sequential addition like in your code...
list3[0] = list1[0] + list2[0];
list3[1] = list1[1] + list2[1];
list3[2] = list1[2] + list2[2];
list3[3] = list1[3] + list2[3];
list3[4] = list1[4] + list2[4];
list3[5] = list1[5] + list2[5];
list3[6] = list1[6] + list2[6];
list3[7] = list1[7] + list2[7];
list3[8] = list1[8] + list2[8];
list3[9] = list1[9] + list2[9];
use this for() loop
for (int l = 0; l < list3.length; l++) {
list3[l] = list1[l] + list2[l];
}
Your first two for loops that take in values are fine, now what you will need is a for loop that does something like this:
for(int i = 0, int length = list1.lengthl; i < length; i++)
{
//add the stuff indexes by doing list1[i]+list2[i]
//make sure to assign it to a new array
}
Edit: If wondering why I use a variable to store the length, it is because a regular loop is usually O(n) [big-oh] this means the time it takes to finish is directly proportional to how large the loop is (aka, 100 iterations will take more than 10, it has a slope of one). However, if we were to do i

How to add each element into a queue

How do I add each element into an arrayqueue? Basically, if I have an array of queues where each index is an arrayqueue that holds the 1s, 10s, 100s, etc. place of a corresponding 6 digit number in another index of array a. For example, if a[1] is 123456 then how can I make the code below hold arr[1] 654321? I've posted a question similar to this before but I'm just trying to get this right.
public static void radixSort(int[] a) {
//Create an array of 10 empty array queues
Queue[] arr = new Queue[a.length];
for (int i = 0; i < arr.length; i++)
arr[i] = new ArrayQueue();
for (int place = 1; place <= 100000; place *= 10) {
for (int i = 0; i < a.length; i++) {
arr[i].add(selectDigit(a[i],place));
// System.out.println("i: " + i + " a[i]: " + a[i] + " place: " + place + " digit: " + selectDigit(a[i],place));
}
}
// for (int i = 0; i < arr.length; i++)
// System.out.print(arr[i].remove()+ " ");
//for (int j = 0; j < arr.length; j++)
// a[j] = (Integer) arr[j].remove();
}
This tutorial might help:
http://www.sourcecodesworld.com/articles/java/java-data-structures/Radix_sort.asp
It seems a good walk through.

Categories

Resources