Finding the smallest value from an array

Hi there :)

I started learning Java a few days ago and have now run into my first problem :p

I am using Netbeans on Mac OS X.

I need to find the smallest value from an array. So far I've had no luck. Any suggestions would be fantastic.

The code so far:

/*

* Math Problems

*

* Created on May 4, 2007, 10:54 AM

*

* PROJECT 1: - IN PROGRESS

* Create a program that allows you to create an integer array of 18 elements with the following values

* 3, 2, 4, 5, 6, 4, 5, 7, 3, 2, 3, 4, 7, 1, 2, 0, 0, 0

*

* - The program computes the sum of elements 0 to 14 and stores it in element 15// COMPLETED

* - The program computes the average and stores it in element 16 // COMPLETED

* - The program finds the smallest value from the array and stores it in element 17

*

* PROJECT 2: - TO DO

* Write a program that accepts from the command line and prints them out. Then use a for loop to print

* the next 13 numbers in the sequence where each number is the sum of the previous two. FOR EXAMPLE:

*

* - input>java prob2 1 3

* - output>1 3 4 7 11 18 29 47 76 123 322 521 843 1364

*

* PROJECT 3: - TO DO

* Write a program that accepts from the command line two numbers in the range from 1 to 40. It then

* compares these numbers against a single dimension array of five integer elements ranging in value

* from 1 to 40. The program displays the message BINGO if the two inputted values are found in the array

* element. FOR EXAMPLE:

*

* - input>java prob3 3 29

* - output>Your first number was 3

* -Your second number was 29

* -Its Bingo! // This message if 3 and 29 are found in the array

* -Bokya!// This message if 3 and 29 are not found in the array

* -The array was 7 5 25 5 19 30

*

* PROJECT 3 EXTENSION: - OPTIONAL

* Generate the array of 5 unique integers using random numbers

*

*/

package mathproblems;

/**

*

* @author Mohammad Ali

*/

publicclass Main{

/** Creates a new instance of Main */

public Main(){

}

/**

* @param args the command line arguments

*/

publicstaticvoid main(String[] args){

int A[]={3,2,4,5,6,4,5,7,3,2,3,4,7,1,2,0,0,0};

int O = A.length - 3;

int B = A[0] + A[1] + A[2] + A[3] + A[4] + A[5] + A[6] + A[7] + A[8] + A[9] + A[10] + A[11] + A[12] + A[13] + A[14];

A[15] = B;// Stores the sum of the integers in A[15]

int C = B / O;

A[16] = C;// Computes and stores the average in A[16]

int D = 101;

if (A[0] < A[1]){ D = A[0];}

else{ D = A[1];}

if (A[1] < A[2]){ D = A[1];}

else{ D = A[2];}

System.out.println("There are " + O +" numbers in the Array");

System.out.println("Those numbers add up to " + B +".");

System.out.println("The average of those numbers is " + C +".");

System.out.println("The smallest value in the array is " + D +".");

}

}

The code is incomplete, but it works so far. The problem is I know there must be an easier way. SAVE ME :)

[4607 byte] By [Tritocha] at [2007-11-27 3:20:49]
# 1

> if (A[0] < A[1]) { D = A[0]; }

> else { D = A[1]; }

> if (A[1] < A[2]) { D = A[1]; }

> else { D = A[2]; }

Don't you meanif (A[0] < A[1]) { D = A[0]; }

else { D = A[1]; }

if (A[2] < D) { D = A[2]; }

if (A[3] < D) { D = A[3]; }

if (A[4] < D) { D = A[4]; }

// etc

A[17] = D;

But, please, longer (descriptive) variable

names - count, average, min etc. And it's usual (to the point of

being a rule) that variable names start with a lowercase letter.

Have you heard about loops? If not have a read of

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html

(and nearby pages) for an Easier Way.

pbrockway2a at 2007-7-12 8:23:33 > top of Java-index,Java Essentials,New To Java...
# 2

I'd try something like that:int A[]={3,2,4,5,6,4,5,7,3,2,3,4,7,1,2,0,0,0};

Arrays.sort(A);

System.out.println("Smallest is: " + A[0]);

int num = A.length;

System.out.println("Number of values is: " + num);

int sum = 0;

for (int i = 0; i < A.length; i++) {

sum+=A[i];

}

System.out.println("Sum of values is: " + sum);

double d = (double)sum/num;

System.out.println("Average of values is: " + d);

@pbrockway2: Sorry for spoon feeding.

PhHeina at 2007-7-12 8:23:33 > top of Java-index,Java Essentials,New To Java...
# 3

Hope this helps...

public class Minimum {

public static void main(String[] args) {

int[] arr={12,32,22,4,51,61,71,82,93,12};

int len = arr.length;

System.out.println("Length of array:"+len);

for (int i = 0; i < arr.length-1; i++) {

if(arr< arr[i+1]){

arr[i+1]=arr;

}else{

arr[i+1]=arr[i+1];

}

}

System.out.println("Minimum:"+arr[9]);

}

}

pundeera at 2007-7-12 8:23:33 > top of Java-index,Java Essentials,New To Java...
# 4

This code gives u the smallest number in the array:

int num[] = {10,23,2,45,64,32,14,6,9,110};

int temp = 0;

for(int i=0 ; i<num.length ; i++) {

for(int j=i ; j><num.length ; j++) {

if(num >= num[j]) {

temp = num;

num = num[j];

num[j] = temp;

}

}

}

System.out.println("The Smallest Number in the Array is : "+ num[0]);

Purushotha at 2007-7-12 8:23:33 > top of Java-index,Java Essentials,New To Java...
# 5
Check out the [url= http://forum.java.sun.com/help.jspa?sec=formatting]tips and secret java codes[/url] for making your posts formatted.
pbrockway2a at 2007-7-12 8:23:33 > top of Java-index,Java Essentials,New To Java...
# 6
Wow. Thanks a lot for the very fast responses :)I think a for loop appeals to me. Thank you all for the help. I think I'll use PhHein's code as it seems to be the smallest, yet, easier to understand.Thank you.
Tritocha at 2007-7-12 8:23:33 > top of Java-index,Java Essentials,New To Java...
# 7

OK :)

Just thought I should show you the output as to help anyone else with the same problem:

/*

* Math Problems

*

* Created on May 4, 2007, 10:54 AM

*

* PROJECT 1: - IN PROGRESS

* Create a program that allows you to create an integer array of 18 elements with the following values

* 3, 2, 4, 5, 6, 4, 5, 7, 3, 2, 3, 4, 7, 1, 2, 0, 0, 0

*

* - The program computes the sum of elements 0 to 14 and stores it in element 15// COMPLETED

* - The program computes the average and stores it in element 16 // COMPLETED

* - The program finds the smallest value from the array and stores it in element 17// COMPLETED

*

* PROJECT 2: - TO DO

* Write a program that accepts from the command line and prints them out. Then use a for loop to print

* the next 13 numbers in the sequence where each number is the sum of the previous two. FOR EXAMPLE:

*

* - input>java prob2 1 3

* - output>1 3 4 7 11 18 29 47 76 123 322 521 843 1364

*

* PROJECT 3: - TO DO

* Write a program that accepts from the command line two numbers in the range from 1 to 40. It then

* compares these numbers against a single dimension array of five integer elements ranging in value

* from 1 to 40. The program displays the message BINGO if the two inputted values are found in the array

* element. FOR EXAMPLE:

*

* - input>java prob3 3 29

* - output>Your first number was 3

* -Your second number was 29

* -Its Bingo! // This message if 3 and 29 are found in the array

* -Bokya!// This message if 3 and 29 are not found in the array

* -The array was 7 5 25 5 19 30

*

* PROJECT 3 EXTENSION: - OPTIONAL

* Generate the array of 5 unique integers using random numbers

*

*/

package mathproblems;

/**

*

* @author Mohammad Ali

*/

import java.util.Arrays;

public class Main {

/** Creates a new instance of Main */

public Main() {

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

int A[]={3,2,4,5,6,4,5,7,3,2,3,4,7,1,2,0,0,0};

Arrays.sort(A);

System.out.println("The smallest value in the array is " + A[0] + ".");

int num = A.length;

System.out.println("There are " + num + " values in the Array.");

int sum = 0;

for (int i = 0; i < A.length; i++) {

sum+=A[i];

}

System.out.println("Those numbers add up to " + sum + ".");

double d = (double)sum/num;

System.out.println("The average value of those numbers is " + d + ".");

}

What Iearned:

1) How to create for loops properly

2) How to import java.util.Arrays ( =D )

3) How to get a more accurate average using double instead of int

4) This forum is the best and has very helpful people 24/7 ( =D)

Thanks Again,

Mo.

Tritocha at 2007-7-12 8:23:33 > top of Java-index,Java Essentials,New To Java...
# 8

Just a little point:* Project 1 also asked you to store the

results of the calculation back in the array. "A[15]=sum;"

and so on. You are going to have to deal with the fact

that a java array of ints can't have a double in it! It might

be worth documenting what value A[16] really ends up

with.

* My new designation requires a quota of such things; but

more importantly I'd hate you to lose marks...

pbrockway2a at 2007-7-12 8:23:33 > top of Java-index,Java Essentials,New To Java...
# 9

> * - The program finds the smallest value from the array and stores it in element 17

> int A[]={3,2,4,5,6,4,5,7,3,2,3,4,7,1,2,0,0,0};

> Arrays.sort(A);

> System.out.println("The smallest value in the array is " + A[0] + ".");

They probably wanted you to find the minimum from (the original) elements 0 through 14, right? Since that's the question they asked with regards to the sum?

This can probably be argued either way, but I think the directions are ill-written.

bheilersa at 2007-7-12 8:23:33 > top of Java-index,Java Essentials,New To Java...