Thread: Simple programming help

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

    Thumbs up Simple programming help

    Why are the last two equations printing the wrong numbers? Thanks in advance.


    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main() {
    
    	int x1,x2,x3,y1,y2,y3;
    	float c12,c13,c14;
    
    	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("%.2f\n", c12);
    	c12 = (pow(x3 - x1,2) + pow(y3 - y1,2));
    	c12 = sqrt(c13);
    	printf("%.2f\n", c13);
    	c12 = (pow(x3 - x2,2) + pow(y3 - y2,2));
    	c12 = sqrt(c14);
    	printf("%.2f\n", c14);
    
    
    	
    	return 0;
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What's the value of c13 and the value of c14 prior to you passing them to sqrt? That's why.

    Also, for future reference, "wrong number" doesn't tell me anything. Try providing sample input, expected output, and actual output. It'll save us all time.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    I fixed that problem and the output is still incorrect. For c12 I get the correct answer but for c13 and c14 I am getting 1627902208.00.


    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main() {
    
    	int x1,x2,x3,y1,y2,y3;
    	float c12,c13,c14;
    
    	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("%.2f\n", c12);
    	c13 = (pow(x3 - x1,2) + pow(y3 - y1,2));
    	c13 = sqrt(c13);
    	printf("%.2f\n", c13);
    	c14 = (pow(x3 - x2,2) + pow(y3 - y2,2));
    	c14 = sqrt(c14);
    	printf("%.2f\n", c14);
    
    
    	
    	return 0;
    
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > scanf("%d %d", &x3, y3);
    You forgot the & for y3
    Which compiler are you using?

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    Thanks for catching that. I am using Jgrasp with GCC.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Then set it up to compile with
    gcc -W -Wall -ansi -pedantic -O2 prog.c

    That will catch (along with lots of other stuff) the problem of getting the parameters to printf and scanf mixed up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. 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
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM