Thread: Help with program

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    5

    Help with program

    This program is for converting from point slope form or two point form into slope intercept form based off of what the user wants. It also needs to prompt the user if they want to run it again. But it doesn't work passed prompting for which form they want to convert from.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    
    void get_problem(int *input);
    void get2_pt(double *x_1,double *y_1, double *x_2, double *y_2);
    void get_pt_slope(double *slope_pt_slope, double *y, double *x);
    void slope_intcpt_from2_pt(double x_1, double y_1, double x_2, double y_2, double *slope, double *intcpt);
    void intcpt_from_pt_slope(double x, double y, double slope_pt_slope, double *intcpt, double *slope);
    void display2_pt(double x_1,double y_1, double x_2, double y_2);
    void display_pt_slope(double slope, double x, double y);
    void display_slope_intcpt(double slope, double intcpt);
    int main(void)
    {
        char letter;
         int input;
        double x_1,y_1,x_2,y_2;
        double slope_pt_slope,x,y;
        double slope,intcpt;
    
        do{
        get_problem(&input);
        switch(input){
        case '1':
    
        get2_pt(&x_1,&y_1,&x_2,&y_2);
        slope_intcpt_from2_pt(x_1,y_1,x_2,y_2,&slope,&intcpt);
        display2_pt(x_1,y_1,x_2,y_2);
        display_slope_intcpt(slope,intcpt);
            break;
    
        case '2':
        get_pt_slope(&slope_pt_slope,&y,&x);
        intcpt_from_pt_slope(x,y,slope_pt_slope,&intcpt,&slope);
        display_pt_slope(slope,x,y);
        display_slope_intcpt(slope,intcpt);
            break;
        }
        printf("Do another conversion (Y or N) =>");
        scanf("%c",&letter);
        }while(letter == 'Y' || letter == 'y');
    }
    void get_problem(int *input){
        printf("                                                                       \n");
        printf("Select the form that you would like to convert to slope-intercept form:\n");
        printf("                                                                       \n");
        printf("1) Two-point form (you know two points on the line)\n");
        printf("                                                                       \n");
        printf("2) Point-slope form(you know the lines slope and first point)\n");
        scanf("%d",&*input);
    }
    
    void get2_pt(double *x_1,double *y_1, double *x_2, double *y_2){
    printf("Enter the x-y coordinates of the point separated by a space");
    scanf("%lf %lf",&*x_1,&*y_1);
    printf("Enter the x-y coordinates of the point separated by a space ");
    scanf("%lf %lf",&*x_2,&*y_2);
    }
    void get_pt_slope(double *slope_pt_slope, double *y, double *x){
        printf("Enter the slope");
        scanf("%lf",&*slope_pt_slope);
        printf("Enter the x-y coordinates of the point separated by a space ");
        scanf("%lf %lf",&*x,&*y);
    }
    void slope_intcpt_from2_pt(double x_1, double y_1, double x_2, double y_2, double *slope, double *intcpt){
    
        *slope = (y_2 - y_1) / (x_2 - x_1);
        *intcpt = ((*slope * (-x_1) + y_1));
    }
    void intcpt_from_pt_slope(double x, double y, double slope_pt_slope, double *intcpt, double *slope){
      *intcpt = (slope_pt_slope * (-x) + (-y));
      *slope = slope_pt_slope;
    }
    void display2_pt(double x_1,double y_1, double x_2, double y_2){
    printf("Two-Point Form");
    printf("(%f - %f) / (%f - %f)",y_2,y_1,x_2,x_1);
    }
    void display_pt_slope(double slope, double x, double y){
    printf("Point-Slope Form\n");
    printf("y - %f = %f(x - %f",y,slope,x);
    }
    
    void display_slope_intcpt(double slope, double intcpt){
    printf("Slope-Intercept Form\n");
    printf("y = %f x + %f ",slope,intcpt);
    }
    Attached Files Attached Files
    Last edited by Salem; 03-23-2017 at 11:52 AM. Reason: Inlined code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,743
    > scanf("%d",&*input);
    You can remove all the &* things from your scanf's, because the &* pair cancel each other out.
    Dereferencing a pointer only to take it's address once more is a nop.

    > case '1':
    It's weird that you use %d to input an integer then.
    Sure, you can type in 49 for success, since that's ASCII '1'.

    > scanf("%c",&letter);
    To make sure you get the next non-whitespace character, use
    scanf(" %c",&letter); // note: leading space
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    5

    New issue

    Quote Originally Posted by Salem View Post
    > scanf("%d",&*input);
    You can remove all the &* things from your scanf's, because the &* pair cancel each other out.
    Dereferencing a pointer only to take it's address once more is a nop.

    > case '1':
    It's weird that you use %d to input an integer then.
    Sure, you can type in 49 for success, since that's ASCII '1'.

    > scanf("%c",&letter);
    To make sure you get the next non-whitespace character, use
    scanf(" %c",&letter); // note: leading space
    I fixed it, and now it runs, but I t requires me to enter the "input" twice and I don't know why. I also used if statements rather than case statements.
    Attached Files Attached Files

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,743
    Of course it asks twice, because you call it twice.

    int input = get_problem(input);
    double x_1,y_1,x_2,y_2;
    double slope_pt_slope,x,y;
    double slope,intcpt;

    do

    int input = get_problem(input);
    You really ought to compile with more warnings enabled.
    Code:
    $ gcc -Wall -Wextra -Wshadow foo.c
    foo.c: In function ‘main’:
    foo.c:16:30: warning: passing argument 1 of ‘get_problem’ makes pointer from integer without a cast [-Wint-conversion]
          int input = get_problem(input);
                                  ^
    foo.c:5:5: note: expected ‘int *’ but argument is of type ‘int’
     int get_problem(int *input);
         ^
    foo.c:23:9: warning: declaration of ‘input’ shadows a previous local [-Wshadow]
         int input = get_problem(input);
             ^
    foo.c:16:10: note: shadowed declaration is here
          int input = get_problem(input);
              ^
    foo.c:23:29: warning: passing argument 1 of ‘get_problem’ makes pointer from integer without a cast [-Wint-conversion]
         int input = get_problem(input);
                                 ^
    foo.c:5:5: note: expected ‘int *’ but argument is of type ‘int’
     int get_problem(int *input);
         ^
    foo.c: In function ‘get_problem’:
    foo.c:49:11: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int **’ [-Wformat=]
         scanf("%d",&input);
               ^
    foo.c:50:11: warning: return makes integer from pointer without a cast [-Wint-conversion]
         return(input);
               ^
    foo.c: In function ‘intcpt_from_pt_slope’:
    foo.c:73:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    foo.c: In function ‘main’:
    foo.c:16:18: warning: ‘input’ is used uninitialized in this function [-Wuninitialized]
          int input = get_problem(input);
                      ^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  2. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  3. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM

Tags for this Thread