Results 1 to 4 of 4
 01182012, 12:56 AM #1Member
 Join Date
 Jan 2012
 Posts
 7
 Rep Power
 0
Checking if a math function is valid
I am given the function {1,2,3,4,5}. I have to receive user input on how many ordered pairs he wants, then verify if the function is valid (values for the xcoordinate have to be between 1 and 5, and an xcoordinate CAN'T be repeated). I know how to loop for and check if the value of X is between 1 and 5, however, I am having trouble checking the string for repeating elements. I wrote the conditional expression for x less than 1 and bigger than 5, but I am stumped on how to write an expression that checks for repeating elements. Can somebody help me with that please? This is what I have so far:
Java Code:import java.util.Scanner; public class Functions { public static void main (String args [] ) { Scanner in = new Scanner (System.in); int []domain = new int [5]; int [] range = new int [5]; int orderedPairs = 0; boolean function = true; System.out.println ("Enter the number of ordered pairs please: "); orderedPairs = in.nextInt(); while (orderedPairs < 0  orderedPairs > 5) { System.out.println ("This input is invalid. Enter a number between 0 and 5 and try again: "); orderedPairs = in.nextInt (); } for (int i = 0; i < orderedPairs; i++) { System.out.println ("Enter the xcoordinate please: "); domain [i]= in.nextInt(); System.out.println ("Enter the ycoordinate please: "); range [i] = in.nextInt(); } for (int i = 0; i < orderedPairs; i++) { System.out.println ("f(" + domain [i] + "): " + range [i]); } for (int i = 0; i < orderedPairs;i++) { if (domain [i] > 5  domain [i] < 1) { function = false; } for (int n = i + 1; n < orderedPairs; n++) { if (domain[i] == domain [n] && range [n] != range [i]) { function = false; } } } if (function == false) { System.out.println ("This is NOT a valid function."); } else if (function == true) { System.out.println ("This is a valid function."); } } }
Last edited by HardToHandle; 01182012 at 02:04 AM. Reason: Improved code
 01182012, 01:33 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,574
 Rep Power
 12
Re: Checking if a math function is valid
You declare domain and range to be double arrays, but they are, in fact, 1dimensional arrays. It would make more sense to declare them as:
Java Code:int[] domain = new int[5]; int[] range = new int[5];
 01182012, 02:03 AM #3Member
 Join Date
 Jan 2012
 Posts
 7
 Rep Power
 0
Re: Checking if a math function is valid
You are absolutely right about having a 1D array, I fixed that. Thanks for the help, I understood what you meant and fixed my code now. Cheers!
 01182012, 02:40 AM #4Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,574
 Rep Power
 12
Similar Threads

painting math function graph
By batia in forum Advanced JavaReplies: 5Last Post: 09172011, 09:58 PM 
Calling function in Javascript from other function
By jdigger in forum New To JavaReplies: 1Last Post: 02272011, 10:00 PM 
Create Math.sin without math.sin
By vudoo in forum New To JavaReplies: 11Last Post: 12072010, 07:23 AM 
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12042009, 04:08 PM 
math.random function help
By katie in forum New To JavaReplies: 2Last Post: 08062007, 04:31 AM
Bookmarks