By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,983 Members | 2,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,983 IT Pros & Developers. It's quick & easy.

Check for min and max value in a for loop

P: 1
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(float x, float y);
float compute_distance(float x1, float y1, float x2, float y2);

/* Function definitions */
void where_is_xy(float 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_distance(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("\nPlease Input (x, y): ");
scanf("%lf%lf", &x1, &y1);
where_is_xy(x1, y1); /*Calling up the function*/
}
{
printf("\nPlease input (x, y): ");
scanf("%lf%lf", &x2, &y2); /*Stored in a different set of variables to work with compute_distance*/
where_is_xy(x2, y2);
}
{
d = compute_distance(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.
Sep 27 '18 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Sep 28 '18 #2

Post your reply

Sign in to post your reply or Sign up for a free account.