arrays - Finding the largest value and the smallest in my code in Java -
i creating code let user type in temperature of different sort.
the user should able type in:
amount of weeks how many time person measuring temperature in 1 week. temperatures every week.
when user has typed information, program should show temperatures have been typed in.
the program should show lowest temperature every week. program should show highest temperature every week. program should show lowest, , highest temperature during weeks. program should show average temperature during every week, , during weeks.
this how far i've come:
{ system.out.println ("temperatures\n"); scanner in = new scanner (system.in); in.uselocale (locale.us); //here information being put in system.out.print ("amount of weeks: "); int amountofweeks = in.nextint (); system.out.print ("measuring temperature per week: "); int measurementsperweek = in.nextint (); //array store information double[][] t = new double[amountofweeks + 1][measurementsperweek + 1]; //mata in temperaturerna (int week = 1; week <= amountofweeks; week++) { system.out.println ("temperatures - week " + week + ":"); (int measurement = 1; measurement <= measurementsperweek; measurement++) t[week][measurement] = in.nextdouble (); } system.out.println (); //show temperatures system.out.println ("temperatures"); (int week = 1; week <= amountofweeks; week++) { (int measurement = 1; measurement <= measurementsperweek; measurement++) system.out.print (t[week][measurement] + " "); system.out.println (); } //lowest, highest, sum of temperatures, , average temp per week double[] mint = new double[amountofweeks + 1]; double[] maxt = new double[amountofweeks + 1]; double[] sumt = new double[amountofweeks + 1]; double[] averaget = new double[amountofweeks]; // write code find lowest temperature first week //´lowest, highest, sum, average weeks double mintemp = mint[1]; double maxtemp = maxt[1]; double sumtemp = sumt[1]; double averagetemp = 0; }
}
the general algorithm find maximum value in list iterate through list , store maximum value encountered far. example, if have 10 numbers stored in array of ints:
int max=intarray[0]; (int i=1; i<10;i++) if (intarray[i]>max) max=intarrray[i];
Comments
Post a Comment