Results 1 to 4 of 4
  1. #1
    HardToHandle is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default 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 x-coordinate have to be between 1 and 5, and an x-coordinate 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 x-coordinate please: ");
      domain [i]= in.nextInt();
    
      System.out.println ("Enter the y-coordinate 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.");
      }
       
     
      
      
      
       
     
     }
     
    
    
    }
    Thanks in advance! :)
    Last edited by HardToHandle; 01-18-2012 at 02:04 AM. Reason: Improved code

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Checking if a math function is valid

    You declare domain and range to be double arrays, but they are, in fact, 1-dimensional arrays. It would make more sense to declare them as:

    Java Code:
    int[] domain = new int[5];
    int[] range = new int[5];
    As for the duplicates check, after you have checked that a domain value is within the right limits you could then use another for loop to determine whether it is equal to any earlier domain value.

  3. #3
    HardToHandle is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default 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!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. painting math function graph
    By batia in forum Advanced Java
    Replies: 5
    Last Post: 09-17-2011, 09:58 PM
  2. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 10:00 PM
  3. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 07:23 AM
  4. Replies: 3
    Last Post: 12-04-2009, 04:08 PM
  5. math.random function help
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 04:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •