0 Members and 2 Guests are viewing this topic.
This is even more off-topic, but here's my version.Code: [Select]grades = []n = lowest = float("inf") # 'inf' stands for infinity - this actually works.while n > 0: n = float(input("Grade: ")) if n > 0: if n < lowest: lowest = n grades.append(n)for i in range(grades.count(lowest)): grades.remove(lowest) print("Average = {0}\nLowest = {1}".format(sum(grades)/len(grades), lowest))Scout's version was 13 lines long (without empty lines), mine is 11 lines long (without empty lines) .Mine also removes any repeated low scores. If that isn't desired behavior, then you could just remove the 'for' statement and execute 'grades.remove(lowest)' only once.
grades = []n = lowest = float("inf") # 'inf' stands for infinity - this actually works.while n > 0: n = float(input("Grade: ")) if n > 0: if n < lowest: lowest = n grades.append(n)for i in range(grades.count(lowest)): grades.remove(lowest) print("Average = {0}\nLowest = {1}".format(sum(grades)/len(grades), lowest))
inputs = []while True: n = input() if n<0: break inputs.append(n) print "Average: %d\nLowest: %d" % (sum(inputs)/len(inputs),min(inputs))
Quote from: Michael_Lee on March 31, 2011, 06:38:03 pmThis is even more off-topic, but here's my version.Code: [Select]grades = []n = lowest = float("inf") # 'inf' stands for infinity - this actually works.while n > 0: n = float(input("Grade: ")) if n > 0: if n < lowest: lowest = n grades.append(n)for i in range(grades.count(lowest)): grades.remove(lowest) print("Average = {0}\nLowest = {1}".format(sum(grades)/len(grades), lowest))Scout's version was 13 lines long (without empty lines), mine is 11 lines long (without empty lines) .Mine also removes any repeated low scores. If that isn't desired behavior, then you could just remove the 'for' statement and execute 'grades.remove(lowest)' only once.For the fun of it, 7 lines!Code: [Select]inputs = []while True: n = input() if n<0: break inputs.append(n) print "Average: %d\nLowest: %d" % (sum(inputs)/len(inputs),min(inputs))* Scout wins.
inputs = []while True: n = input() if n<0: break inputs.append(n) print "Average: %d\nLowest: %d" % ((sum(inputs) - min(inputs) * inputs.count(min(inputs))) / len(inputs), min(inputs))
int sum;int numScores;int lowest = 100;while (true){ try { int score = Integer.parseInt(JOptionPane.showInputDialog("Enter test score (negative to exit): ")); } catch (NumberFormatException exc) { break; } if (score < 0) break; numScores++; if (score < lowest) lowest = score; sum += score;}System.out.println("Average: " + ((sum - lowest) / (numScores - 1)), "Lowest: " + lowest);
well deep thought's code breaks after it reaches a negative number, so if you did 10,90,6,-5,12,5 it would calculate 10, 90, and 6 to get 10+90+6=106/3=35.333....
Quote from: graphmastur on April 03, 2011, 09:27:54 amwell deep thought's code breaks after it reaches a negative number, so if you did 10,90,6,-5,12,5 it would calculate 10, 90, and 6 to get 10+90+6=106/3=35.333....Well, it wouldn't even ask you for 12 and 5.
Quote from: graphmastur on April 03, 2011, 09:27:54 amwell deep thought's code breaks after it reaches a negative number, so if you did 10,90,6,-5,12,5 it would calculate 10, 90, and 6 to get 10+90+6=106/3=35.333....if you did 10,90,6,-5 shouldn't 6 be dropped, and you end up with 100/2 = 50?
I am assigned to make 'A program that will read integer test scores from the keyboard until a negative value is typed in. The program will drop the lowest score from the total and print the average of the remaining scores.'
Quote from: Snake X on March 30, 2011, 07:02:25 pmI am assigned to make 'A program that will read integer test scores from the keyboard until a negative value is typed in. The program will drop the lowest score from the total and print the average of the remaining scores.'I agree with nemo.
public class Main { public static void main(String[] args) { int lowScore=Integer.MAX_VALUE; int sum=0; int numValues=0; int currGrade; while(true) { System.out.print("Enter Grade: "); currGrade=TextIO.getInt(); if (currGrade<0) break; if(currGrade<lowScore) { if(lowScore!=Integer.MAX_VALUE) sum+=lowScore; lowScore=currGrade; numValues++; } else {sum+=currGrade;numValues++;} } System.out.println("Average: "+(double)sum/(numValues-1)); }}
import javax.swing.JOptionPane;public class while2 { public static void main(String[] args) { int temp = 0; int low = 0; int acc = 0; double total = 0; int score = 0; int lowest = 0; String scoretemp = JOptionPane.showInputDialog("Enter your test score (enter a negative number to exit):"); score = Integer.parseInt(scoretemp); //5 low = score; //low is 5 total += score; //total is 5 acc += 1; //1 while (true) { scoretemp = JOptionPane.showInputDialog("Enter your test score (enter a negative number to exit):"); score = Integer.parseInt(scoretemp); //score is now a negative number if (score < 0) { //nope break; } else{ if (score < low) { //if 1 < 2 total += low; // total is total + low (5+4+3+2+1) = 15 low = lowest; //lowest is now 2 } else{ total += score; } } acc += 1; //6 low = score; //low is now 1 } System.out.println(lowest); //lowest is 2 System.out.println(total); //total is 15 total = total - lowest; //total is now 13 System.out.println(total); acc = acc - 1; //5 System.out.println(acc); total =(double) total / acc; //13 / 5 = 2.6 System.out.println(total);// System.out.println("your average of the test scores minus the lowest score is: " + total);}}