Thread: Math equations help....with C code

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    33

    Math equations help....with C code

    slope_intercept_from2_pt - Takes four input parameters, the x-y coordinates of two points, and returns through output parameters the slope (m) and the y-intercept (b).


    intercept_from_pt_slope - Takes three input parameters, the x-y coordinates of one point and the slope, and returns as the function value the y-intercept.

    my prof wants me to write these two functions involving these equations and i am not sure how to write them can you help me out with getting started on them:

    Two-point form m = (y2 - y1) / (x2 - x1) (x1, y1), (x2, y2)
    Point-slope form y - y1 = m(x - x1) m, (x1, y1)
    Slope-intercept form y = mx + b m, b


    this is what i have so far.....



    FUNCTIONS.C

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define TWOPNT 1
    #define PNTSLOPE 2
    #define EXIT 3
    
    int get_problem() 
    {
    	int option= 1;
    	 do {
    		/*system ("cls");*/
    		printf("Select the Mathematical Form you would to convert the slope intercept form\n");
    		printf("%d. Two-point Form(you know the points on the line).\n", TWOPNT);
    		printf("%d. Point-slope form (you know the line's slope and one point).\n", PNTSLOPE);
    		printf("%d. Exit\n", EXIT);
    
    		scanf("%d", &option);
    
    	 } while ((option < TWOPNT) && (option > EXIT));
    	return option;
    	
    
    }
    int get2_pt(int *x1,int *y1, int *x2, int *y2)
    {
    	printf("Please enter the X and Y Coordinates of both points(x1,y1,x2,y2:\n");
    	scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    	return (*x1, *y1, *x2, *y2);
    }
    
    int get_pt_slope(double *slope, int *x, int *y)
    {
    	printf("Please input the slope and the x and y coordiantes of the point:\n");
    	scanf("%d%d%d", &slope, &x, &y);
    	return (*slope, *x, *y);
    }
    
    int slope_intercept_from2_pt(int *x1,int *y1, int *x2, int *y2)
    {
    	double slope = 0.0, b = 0.0;
    	slope = (y2 - y1)/(x2 - x1);
    	return (slope);
    }

    MAIN.C

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define TWOPNT 1
    #define PNTSLOPE 2
    #define EXIT 3
    
    int main (void) 
    {
    	int running = 1;
    	srand((unsigned int) time(NULL));
    	while(running) 
    	{
    		switch(get_problem()) {
    			case TWOPNT:
    				/*system("pause");*/
    				break; 
    			case PNTSLOPE: 
    				break; 
    			case EXIT:
    				printf("See you next time :)\n");
    				running = 0;
    				break;
    			default: 
    				printf ("That was not an option.....choose again!");
    		}
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Ah, the wonders of "hacking code without understanding, and then ask for help when it doesn't work".

    I'll give a few hints, but you need to think more carefully about what you are doing. You need to work more systematically to understand what you are trying to achieve, and how to translate it into code. Remember that a compiler is an ignoramus that will do exactly what you request it to do, and the skill of programming is making the right request in a manner that the compiler can understand.


    1) Given that slopes and intercepts may not be integral values, it would be better to use a floating point type consistently rather than int.

    2) If you want to return more than one value from a function, either return a struct or supply a pointer as an argument for each value you want to send back to the caller.

    3) The line
    Code:
    return (*x1, *y1, *x2, *y2);
    does not return a set of four values. It returns the value of *y2 only.

    4) In get_2pt() and get_pt_slope() lose the ampersands. The arguments are pointers, so they ARE addresses. The caller is required to supply the address of some actual variables.

    5) You should not use "%d" format specifier for scanf() when reading a floating point value. The compiler is not required to complain if you get this wrong.

    5) You actually need to call your various functions - get_2pt(), get_pt_slope(), etc. They don't get called by magic
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Math equations
    By C-Struggler in forum C Programming
    Replies: 6
    Last Post: 04-05-2003, 08:21 AM
  3. Math function code - couple probs, tips?
    By Captain Penguin in forum C++ Programming
    Replies: 4
    Last Post: 06-18-2002, 09:54 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM