Thread: if-statment not working

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    23

    Thumbs up if-statment not working

    For some reason I can not get the if statements at the end to work. Atleast one of the if statements should work during each run of the program. Thanks in advance.


    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main() {
    
    	int x1,x2,x3,y1,y2,y3,a1,a2,a3,a4,a5,a6;
    	double c12 = 0,c13 = 0,c23 = 0;
    
    	printf("What are the first x and y coordinates?\n");
    	scanf("%d %d", &x1, &y1);
    	printf("What are the second x and y coordinates?\n");
    	scanf("%d %d", &x2, &y2);
    	printf("What are the third x and y coordinates?\n");
    	scanf("%d %d", &x3, &y3);
    	
    	c12 = (pow(x2 - x1,2) + pow(y2 - y1,2));
    	c12 = sqrt(c12);
    	printf("%.2lf\n", c12);
    	c13 = (pow(x3 - x1,2) + pow(y3 - y1,2));
    	c13 = sqrt(c13);
    	printf("%.2lf\n", c13);
    	c23 = (pow(x3 - x2,2) + pow(y3 - y2,2));
    	c23 = sqrt(c23);
    	printf("%.2lf\n", c23);
    
    	if (c12 + c13 == c23){
    		a1 = (x2 + x3)/2; 
    		a2 = (y2 + y3)/2;
    		printf("(%d,%d) is the point in the middle", a1, a2);
    		}
    	if (c12 + c23 == c13){
    		a3 = (x1 + x3)/2;
    		a4 = (y1 + y3)/2;
    		printf("(%d,%d) is the point in the middle", a3, a4);
    		}
    	if (c23 + c13 == c12){
    		a5 = (x1 + x2)/2;
    		a6 = (y1 + y2)/2;
    		printf("(%d,%d) is the point in the middle", a5, a6);
    		}

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    thought about just using an if, else if, else rather then three if statements?
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    tried that after I posted. Still same result

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    The if- statements are still not working. This is what I have:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main() {
    
    	int x1,x2,x3,y1,y2,y3,a1,a2,a3,a4,a5,a6;
    	double c12 = 0,c13 = 0,c23 = 0;
    
    	printf("What are the first x and y coordinates?\n");
    	scanf("%d %d", &x1, &y1);
    	printf("What are the second x and y coordinates?\n");
    	scanf("%d %d", &x2, &y2);
    	printf("What are the third x and y coordinates?\n");
    	scanf("%d %d", &x3, &y3);
    	
    	c12 = (pow(x2 - x1,2) + pow(y2 - y1,2));
    	c12 = sqrt(c12);
    	printf("%.2lf\n", c12);
    	c13 = (pow(x3 - x1,2) + pow(y3 - y1,2));
    	c13 = sqrt(c13);
    	printf("%.2lf\n", c13);
    	c23 = (pow(x3 - x2,2) + pow(y3 - y2,2));
    	c23 = sqrt(c23);
    	printf("%.2lf\n", c23);
    
    	if ((c12 + c13) == c23){
    		a1 = (x2 + x3)/2; 
    		a2 = (y2 + y3)/2;
    		printf("(%d,%d) is the point in the middle", a1, a2);
    		}
    	else if ((c12 + c23) == c13){
    		a3 = (x1 + x3)/2;
    		a4 = (y1 + y3)/2;
    		printf("(%d,%d) is the point in the middle", a3, a4);
    		}
    	else if ((c23 + c13) == c12){
    		a5 = (x1 + x2)/2;
    		a6 = (y1 + y2)/2;
    		printf("(%d,%d) is the point in the middle", a5, a6);
    		}
    		
    		
    	
    	return 0;
    
    }

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    I figured out where my problem is but I need help correcting it. For some reason my distance formula is not working out. It should work out that the two smallest distances should equal the bigger distance. Dosent that make sense?

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Not really. Where in a math book does it say on any given triangle, the length of one size is equal to the sum of the lengths of the two other sides? I'd like to help you with your math, but I can't really see what you're trying to accomplish here.
    Last edited by SlyMaelstrom; 02-20-2006 at 08:13 PM.
    Sent from my iPadŽ

  7. #7
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Also, you shouldn't compare two floating point numbers with ==. Please see http://www.c-faq.com/fp/fpequal.html

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    probably u are talking about a right angle triangel , the pythagoras theorem states that sume of squares of two sides is equal to the square of the other side.
    so u r not actually squaring there.


    and if u r talking about any general triangle,the sum of two sides is always greater than the other side,it can only be equal when the three points lie on a straight line and not in a triangle.
    Last edited by qqqqxxxx; 02-21-2006 at 12:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  2. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  3. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  4. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM