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
*/
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};
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 :)