-1

Doing a project for AP Computer science, and I still haven't managed to figure out how to do this part:

The program is supposed to ask the user for 5 test score inputs and then the code will "curve" the smallest value entered by taking the square root of the score and adding the integer value of the square root to the original score. I have figured out how to do the math and everything else, but I just have no clue how to find the smallest score entered. The teacher specifically said no arrays or collections.

My int variables are score1, score2, score3, score4, and score5. Say the user entered 75 for score1, 32 for score2, 42 for score3, 99 for score4, and 100 for score 5. How would I make it do the curve function (that I've already made) for score2 specifically, since it's the minimum value. I've already set up the scanners and everything, I just don't know how to get the minimum (and maximum) values.

Thank you!

7
  • 5
    Each time the user enters a value, check it against the last known lowest value Commented Oct 16, 2015 at 0:19
  • 3
    "The teacher specifically said no arrays or collections." sigh Yet another class that should be titled "Stupid Java Tricks." Try not to let it rot your brain too much. Commented Oct 16, 2015 at 0:20
  • 5
    Show us what you've tried so far Gabe. Commented Oct 16, 2015 at 0:22
  • 2
    @KevinKrumwiede I have to disagree. Ok, 5 ints is a contrived example, but "find the minimum score from 50 million student records" is probably not something you want to do with an array. There are a couple of benefits here: 1) students have to think about the problem (ok, posting the whole thing on SO without any thought defeats the purpose). 2) students learn a new technique - possible a whole new area ("if all I have is a hammer [aka array] then everything looks like a nail") Commented Oct 16, 2015 at 0:28
  • 1
    @John3136 With millions of records, you'd definitely want to buffer them in an array or collection. You certainly wouldn't write something like the accepted answer, which, unfortunately, is probably exactly what the teacher is looking for. Commented Oct 16, 2015 at 5:59

6 Answers 6

1

I've seen this before. (hehe)

Key here is Math class. Math.Min or Math.Max takes only two argument, but can be expanded to work for more than that, indefinitely.

Here's an example with 3 variables. int var1, var2, var3;

int max = Math.max(var1, Math.max(var2, var3)); int min = Math.min(var2, Math.max(var2, var3)); int middle = var1 + var2 + var3 - max - min 

I can do this with even more than 3, but it gets a little messy. Here's a helpful visual on imgur. Direct Album

I'll shorten Math.Max to max() and Math.min() to min, from here.

 // a, b, c, d = int max1 = max(a, b) max2 = max(c, d) min1 = min(a, b) min2 = min(c, d) max = max(max1, max2) mid_high = max(min1, min2) mid_low = min(max1, max2) min = min(min1, min2) 

This works with 3 arguments too.

max1 = max(a, b) max2 = max(b, c) // or a, c : doesn't matter, as long as you have all three argument min1 = min(a, b) min2 = min(b, c) // or a, c : doesn't matter, as long as you have all three argument mid = min(max1, max2) mid = max(min1, min2) 

I hope you learned a new technique today

Sign up to request clarification or add additional context in comments.

4 Comments

Please add some further explanation to your answer. Why did you choose to use Python for this? Why do you explain something about using three arguments while only using two?
I do not understand. Python allows max(more, than, two, arguments). I used 3 arguments, a b and c.
None of the calls for max or min uses more than two arguments - Math.max(var1, Math.max(var2, var3)); are two nested calls using two arguments each
Yes, I know, it's just for convenience, but I didn't need it. It doesn't matter the language, I'll edit it out.
0
int n1, n2, n3, n4, max = 0; // get values of from input or assign it to n1, n2, n3 and n4 if (n1 > max) max = n1; if (n2 > max) max = n2; if (n3 > max) max = n3; if (n4 > max) max = n4; System.out.println(max); 

Comments

0
 /* Problem Statement:- Enter many positive numbers & find Maximum & Minimum number entered & also total numbers entered. */ import java.util.*; public class MathOperations { public static void main(String args[]) { //Variables double flag=0,count=0,max=0,min=0; //Creating Scanner class Object Scanner scan = new Scanner(System.in); while (true) { //Taking input from user System.out.println("Enter a double number (negative to quit) : "); double number = scan.nextDouble(); if (number > 0) { count++; max = Math.max(max,number); if (flag == 0) { min = number; flag++; } else { min = Math.min(min,number); } } else { break; } } System.out.println("Total Numbers entered: "+count); System.out.println("Maximum: "+max); System.out.println("Minimum: "+min); } } 

Comments

-1

it seems to me what the problem means is that you keep track the min and max value on each input without using array,

 Scanner sc = new Scanner(System.in); int min = -1, max = -1; int input; for(int i=0; i<5; i++) { input = sc.nextInt(); if(input < min) min = input; if(input > max) max = input; } // here you have min and max number from the inputs and can perform the curve function 

Comments

-1

You can use following code snippet:

int min = Integer.MAX_VALUE; //As per given requirement 'min' can be set to any value >=100 int max = Integer.MIN_VALUE; //As per given requirement 'max' can be set to any value <=0 try(Scanner input = new Scanner(System.in)) { int inValue; for(int i=0; i<5; i++) { inValue = input.nextInt(); if(inValue < min) min = inValue; if(inValue > max) max = inValue; } } System.out.println("Minimum value is : " + min); System.out.println("Maximum value is : " + max); 

See it working here.

Comments

-2

You could try this:

int s1, s2, s3, s4, s5; s1 = 75; s2 = 32; s3 = 42; s4 = 99; s5 = 100; int min = s1; min = (min < s2) ? min : s2; min = (min < s3) ? min : s3; min = (min < s4) ? min : s4; min = (min < s5) ? min : s5; System.out.println(min); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.