import java.util.Scanner;
/**
* Contains example methods for lecture, about loops.
*/
public class ArrayDemos {
private static Scanner s = new Scanner(System.in);
private static final int NUM_DATA = 365;
/** Return the largest value in a double[].
* @param data The array to find the max value in.
* Pre-condition: Must be non-empty (data.length > 0.)
* @return the largest value in data.
*/
public static double findMax( double[] data ) {
assert(data.length > 0);
double maxSoFar = data[0];
for (int i=1; i < data.length; ++i ) {
if (maxSoFar < data[i]) {
maxSoFar = data[i];
}
// Or, just: maxSoFar = Math.max( maxSoFar, data[i]);
// Too bad there isn't a "max=" operator !-)
// ... see fold
}
return maxSoFar;
}
/** Return the index of a largest value in a double[]. ("argmax").
* @param data The array to find the max value in.
* Pre-condition: Must be non-empty (data.length > 0.)
* @return the index of a largest value in data.
* If the largest value occurs several times, we return the index of
* the ???? occurrence.
*/
public static int indexOfMax( double[] data ) {
return -1;
}
public static double maxConsecutivePair( double[] data ) {
return -1;
}
public static int countOccurrences( String wrd, char target ) {
return -1;
}
public static int countOccurrences( String[] wrd, char target ) {
return -1;
}
public static String arrayToString( double[] wrd ) {
return "blah";
}
public static int[] letterFrequency( String wrd ) {
int[] letterFreqSoFar = new int[26];
// initialize all 25 locations to 0.
for (int i=0; i < letterFreqSoFar[i]; ++i ) {
letterFreqSoFar[i] = 0;
}
// Now, look at each letter, and add it to the appropriate tally:
for (int i=0; i < wrd.length(); ++i) {
int letterInd = (int)(wrd.toLowerCase().charAt(i) - 'a');
++letterFreqSoFar[ letterInd ];
}
return letterFreqSoFar;
}
public static double[] doublesFromKbd( int sz ) {
double[] theAnswer = new double[sz];
int indx = 0;
while (indx < theAnswer.length) {
System.out.println("Please enter datum #" + indx );
theAnswer[indx] = s.nextDouble();
indx = indx + 1;
}
return theAnswer;
}
public static double sum( double[] data ) {
double totalSoFar = 0.0;
for ( int i = 0; i < data.length; ++i ) {
totalSoFar += data[i];
}
return totalSoFar;
}
public static void rainfallExample() {
double[] rainfall = doublesFromKbd( NUM_DATA );
double totalRainSoFar = sum( rainfall );
System.out.println("average rainfall is " + totalRainSoFar/rainfall.length );
}
//
public static int indexOf( String s, char target ) {
int i = 0;
boolean seenSoFar = false;
while (i < s.length() && !seenSoFar) {
if (s.charAt(i) == target) {
seenSoFar = true; // cause the loop to stop
}
i += 1;
}
return i; // the index IS our accumulated answer in this case!
}
public static int indexOf_v2( String s, char target ) {
int i = 0;
boolean seenSoFar = false;
while (i < s.length() && !seenSoFar) {
// update accum to: "previously seen, or we're seeing it now"
seenSoFar = seenSoFar || (s.charAt(i) == target);
i += 1;
}
return i; // the index IS our accumulated answer in this case!
}
public static int indexOf_v3( String s, char target ) {
int i = 0;
while ( target != s.charAt(i) && i < s.length() ) {
i += 1;
}
return i;
}
public char[] wordFreqs() {
// How to represent histogram's info?
while (s.hasNext()) {
// read one word, count its letters, adding them to our histogram
}
// return resulting histogram
return new char[0]; // Stub -- an array with 0 locations (!)
}
}