Thread: Graph functions check

  1. #1

    Unhappy Graph functions check

    I posted a challenge on a different BBS that asked for a program that checks to see if a set of 4 ordered pairs create a function on a graph or not (no x value is the same as another). I have created the program and it works fine, but I need to add one more thing to it. I need it to check to see if the ordered pairs are the same (in which if they were, they wouldn't stop it from creating a function). What I have so far is-
    Code:
    #include <iostream>
    using namespace std;
    
    int IsFunction(char OrderedPairs[20]);
    
    int main()
    {
    	char OrderedPairs[20];
    	int function;
    	cout << "Enter ordered pairs: ";
    	cin.getline(OrderedPairs, 20);
    	function = IsFunction(OrderedPairs);
    	if (function == 1)
    		cout << "Is not a function.";
    	else
    		cout << "Is a function.";
    	return 0;
    }
    
    int IsFunction(char OrderedPairs[20])
    {
    	int a = OrderedPairs[1];
    	int b = OrderedPairs[6];
    	int c = OrderedPairs[11];
    	int d = OrderedPairs[16];
    	if (a == b || a == c || a == d)
    		return 1;
    	else if (b == c || b == d)
    		return 1;
    	else if (c == d)
    		return 1;
    	else
    		return 0;
    }
    I have thought of just doing like I did for the x values and assign them a letter of the alphabet and check it like that, but that seems a little unnessicary. What could I do to check to see if the ordered pairs were the same as another? Could I just assign the ordered pairs to different variables, remove the comma, and comapre them to each other?

    Oh, and if you need to know, the program will be given ordered pairs like this-
    Enter ordered pairs: (1,2)(3,4)(5,6)(7,8)
    So I will not need to remove spaces. I will probley add some error checking when I get this part done.


    The challenge is mine (I posted it for others to try) but I am just needing help with my own solution. I will not post any code y'all may supply for an example or a solution unless you give me permission, and even then you will get create for helping me out.


    Thanks.

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Couldn't you just assign the y values to the variables e,f,g and h the same way you do the x values? Then all you would need would be to write the if statements like this:
    Code:
    	if ((a == b && e!=f) || (a==c && e!=g) || (a==d && e!=h))
    		return 1;
                    etc...
    I'm sure theres a more elegant solution, but this is closest to what you have already. Keep in mind though you are assigning chars to ints. If you want to use ints, you need to subtract '0' from each number. Also, this solution will not work for double digit or negative numbers, you'll need to parse those if they are allowable.

  3. #3
    I have thought of just doing like I did for the x values and assign them a letter of the alphabet and check it like that
    Yea I have considered that. But I was wondering about other solutions. Thanks anyways though.

  4. #4
    I decided to use that method for the time being, until someone gives a better idea. Here is my code right now.
    Code:
    #include <iostream>
    using namespace std;
    
    int IsFunction(char OrderedPairs[20]);
    
    int main()
    {
    	char OrderedPairs[20];
    	int function;
    	cout << "Enter ordered pairs: ";
    	cin.getline(OrderedPairs, 20);
    	function = IsFunction(OrderedPairs);
    	if (function == 1)
    		cout << "Is not a function.";
    	else
    		cout << "Is a function.";
    	return 0;
    }
    
    int IsFunction(char OrderedPairs[20])
    {
    	int ax = OrderedPairs[1];
    	int ay = OrderedPairs[3];
    	int bx = OrderedPairs[6];
    	int by = OrderedPairs[8];
    	int cx = OrderedPairs[11];
    	int cy = OrderedPairs[13];
    	int dx = OrderedPairs[16];
    	int dy = OrderedPairs[18];
    	if ((ax == bx && ay != by) || (ax == cx && ay != cy) || (ax == dx && ay != dy))
    		return 1;
    	else if ((bx == cx && by != cy) || (bx == dx && by != dy))
    		return 1;
    	else if (cx == dx && cy != dy)
    		return 1;
    	else
    		return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. error help making no sense
    By tunerfreak in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2007, 07:55 PM
  3. Variables do not equal functions!!!
    By me@burk. in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 06:24 AM
  4. Passing data/pointers between functions #2
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 09:49 PM
  5. functions
    By code_one in forum C Programming
    Replies: 3
    Last Post: 08-18-2001, 06:41 PM