Array Example to Find a Maximum Value
Array Example to Find a Maximum Value
Steps: 1. use constant to size array 2. fill up array, completely, with scanner input 3. set (temp) max to be first value 4. compare first value to each entry in array, if any value > max, this is new max value
Note: this does NOT sort values, that is another method to find max
import java.util.Scanner;
public class ArrayMax {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_ELEMENTS = 8; int userVals[] = new int[NUM_ELEMENTS]; int i, maxVal;
// Number of elements // Array of user numbers // Loop index, Computed max
// Prompt user to populate array System.out.println("Enter " + NUM_ELEMENTS + " integer values...");
for (i = 0; i < userVals.length; ++i) { userVals[i] = scnr.nextInt(); System.out.println("Value: " + userVals[i]);
}
// set temp largest (max) number to first value in array maxVal = userVals[0];
for (i = 0; i < userVals.length; ++i) {
// if any value > first, set to new max
if (userVals[i] > maxVal) {
//System.out.println("New Max Value: " + userVals[i]);
maxVal = userVals[i];
}
}
System.out.println("Max: " + maxVal);
}
}
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- quick docs create a variable to store the largest value update the
- array example to find a maximum value
- python max builtin function examples tutorial kart
- introduction chapter to numpy
- scientific and mathematical computing using python
- python lab 3 2d arrays and plotting university of york
- max min average kansas state university
- powerpoint presentation
- data structure and algorithm in python array based sequences
- maximum slice problem codility
Related searches
- how to find a company s annual report
- how to find a company s financial reports
- how to find a product to sell
- how to find a company s annual sales
- how to find a customs broker
- how to find a financial advisor
- how to find a primary care physician
- where to find a car loan
- how to find a good financial advisor
- how to find the p value calculator
- how to find a screenshot
- how to find the p value statistics