Check for min and max value in a for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boxman139
    New Member
    • Sep 2018
    • 1

    Check for min and max value in a for loop

    I am writing a program that asks for two sets of 'x' and 'y' values and checks which quadrant each is in, as well as computes the distance between the two sets. It is in a 'for' loop that loops ten times. My professor is asking me to check for the maximum distance of all the points that have been input, and print the maximum value, as well as the set of points that produced that maximum. I am having trouble figuring this part out and I could not find any answers to my specific situation on the internet. Below is my code:

    /*============== =============== =============== ==*/
    #include <stdio.h>
    #include <math.h>

    /* Function proto-type declarations */
    void where_is_xy(flo at x, float y);
    float compute_distanc e(float x1, float y1, float x2, float y2);

    /* Function definitions */
    void where_is_xy(flo at x, float y) {

    /*If statements to test what quadrant the input is*/
    if (x > 0 && y > 0)
    printf("\n(x, y): (%0.2f, %0.2f) is in the first quadrant\n", x, y);
    else if (x < 0 && y > 0)
    printf("\n(x, y): (%0.2f, %0.2f) is in the second quadrant\n", x, y);
    else if (x < 0 && y < 0)
    printf("\n(x, y): (%0.2f, %0.2f) is in the third quadrant\n", x, y);
    else if (x > 0 && y < 0)
    printf("\n(x, y): (%0.2f, %0.2f) is in the fourth quadrant\n", x, y);
    else if (x != 0 && y == 0)
    printf("\n(x, y): (%0.2f, %0.2f) is on the x-axis\n(it is an x-intercept)\n", x, y);
    else if (x == 0 && y != 0)
    printf("\n(x, y): (%0.2f, %0.2f) is on the y-axis\n(it is a y-intercept)\n", x, y);
    else
    printf("\n(x, y): (%0.2f, %0.2f) is on the origin\n", x, y);

    }

    float compute_distanc e(float x1, float y1, float x2, float y2) {

    /*Expression to determine the distance between two sets of points*/
    float d;
    d = sqrt((pow((x2 - x1), 2)) + (pow((y2 - y1), 2)));
    return (d);
    }


    int main(void)
    {
    /*Variable Declarations
    'i' is the counting variable in the for loop*/
    double x1, y1, x2, y2, d;
    int i;
    double max = 0;
    double min = 0;


    /*For loop that repeats ten times for determing the quadrant of the set input by the user*/
    for(i = 0; i < 10; i += 1) {
    {
    printf("\nPleas e Input (x, y): ");
    scanf("%lf%lf", &x1, &y1);
    where_is_xy(x1, y1); /*Calling up the function*/
    }
    {
    printf("\nPleas e input (x, y): ");
    scanf("%lf%lf", &x2, &y2); /*Stored in a different set of variables to work with compute_distanc e*/
    where_is_xy(x2, y2);
    }
    {
    d = compute_distanc e(x1, y1, x2, y2);
    printf("\nThe distance between (%0.2lf, %0.2lf) and (%0.2lf, %0.2lf) equals: %0.2lf\n", x1, y1, x2, y2, d);
    }

    }


    return (0);
    }




    =============== =============== =============== =
    Any help would be greatly appreciated. I am just completely stuck and don't know where to go from here. Thank you very much.
    Last edited by boxman139; Sep 27 '18, 11:10 PM. Reason: #include <conio.h> was removed from my actual code Missing info about the program requirements.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The first thing is to not mix integer and floating point. You can compare integers exactly but floating point, due to rounding, may not compare the way you think it should.


    There are double variables used in functions that take float arguments. Double is not float.


    There double variables compared to integers.


    I suggest you convert everything to int. The comparisons will now work.


    When you finish this, repost and I'll look at the code again.

    Comment

    Working...