Like Tree1Likes

C Program - Accept rectangular points and calculate distance between

This is a discussion on C Program - Accept rectangular points and calculate distance between within the C Programming forums, part of the General Programming Boards category; I am writing a c program that accepts two coordinate points from a rectangle, using a function distance, calculates the ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    98

    C Program - Accept rectangular points and calculate distance between

    I am writing a c program that accepts two coordinate points from a rectangle, using a function distance, calculates the distance between using the formula distance = sqrt (x2-x1)^2+(y2-y1)^2
    I know my beginning code is off base but I am just beginning in code and struggling to find correct codes.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #pragma warning (disable:4996)
    int main()
    {
      float x1, x2, y1, y2, midpoint, xtotal, ytotal;
      printf("Enter the first x coordinate: ");
      scanf("%f", &x1);
      printf("Enter the first y coordinate: ");
      scanf("%f", &y1);
      printf("Enter the second x coordinate: ");
      scanf("%f", &x2);
      printf("Enter the second y coordinate: ");
      scanf("%f", &y2);
      xtotal = (x2 - x1) * (x2 - x1);
      ytotal = (y2 - y1) * (y2 - y1);
      midpoint = sqrt(xtotal) + (ytotal);
      printf("The distance of the coordinate is %f", midpoint);
       return 0;
      system("pause");
    }
    Last edited by Salem; 03-06-2012 at 07:43 PM. Reason: demunging the horrible choice of tags - paste as TEXT in future

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what is it you are actually having trouble with?


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

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    > midpoint = sqrt(xtotal) + (ytotal);
    How much of this is actually within the sqrt() call?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    I am supposed to right a seperate function, that is distance. When I did that, it would not recognize the characters that I have under float currently. So, how to write the distance function correctly.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    It should be both xtotal and ytotal, which is written wrong on second look, my main issue is with correctly writing the solution with the function distance included.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    2,2 and 4,4
    (4-2)*(4-2) = 4
    4 + 4 = 8
    distance = square root of 8
    distance = 2.8 and some change

    Is that right? If you know it's right on paper then, you can test that your program is doing it right with the same input.


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

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    My solution exits before the answer is displayed

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    That's very misleading, name your variables better.

    You aren't passing enough to sqrt().

    You're returning before you can system("pause"); (which you shouldn't be doing in the first place)

    Code:
    #pragma warning (disable:4996)
    What does this ^^ do? It sounds like you're trying to force your compiler to stop showing you warnings, which is a dumb idea in it of itself, because warnings necessarily indicate that you're doing something wrong. I just googled it, and it seems like it has something to do with deprecated functions.

    I'm making a wild guess and assuming that you kept getting warned about the non-standard use of system(pause), and instead of fixing it, told the compiler to shut up instead.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,974
    Quote Originally Posted by memcpy
    What does this ^^ do? It sounds like you're trying to force your compiler to stop showing you warnings, which is a dumb idea in it of itself, because warnings necessarily indicate that you're doing something wrong. I just googled it, and it seems like it has something to do with deprecated functions.

    I'm making a wild guess and assuming that you kept getting warned about the non-standard use of system(pause), and instead of fixing it, told the compiler to shut up instead.
    In this case, the warning suppression is legitimate. Microsoft compilers mark certain standard library functions as "deprecated" (even though they are not) because the standard library implementation was extended to provide versions of these functions that are intended to be safer to use carelessly. However, the better way of suppressing these warnings is to define _SCL_SECURE_NO_WARNINGS through the compiler options.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User DevoAjit's Avatar
    Join Date
    Jun 2011
    Location
    Ludhiana, Punjab, India, India
    Posts
    32
    Why we use #pragma?
    I can search over net, but i need a simple answer and a refrense. Thank u.

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    The pragma warning is scanf, which our instructor told us to do. What do I need to do differently so the solution doesn't exit before displaying the answer.

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    Ok my solutions displays the answer now, but I am receiving zero as the answer. What is wrong with the equation I have entered?

  13. #13
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Show us your updated code please.
    Code:
    while(!asleep) {
       sheep++;
    }

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<math.h>
    #pragmawarning (disable:4996)
    int main()
    { 
    float x1, x2, y1 ,y2, distance,xtotal, ytotal;
    printf("Enter the first x coordinate: ");
    scanf("%f", &x1);
    printf("Enter the first y coordinate: ");
    scanf("%f", &y1);
    printf("Enter the second x coordinate: ");
    scanf("%f", &x2);
    printf("Enter the second y coordinate: ");
    scanf("%f", &y2);
    xtotal = (x2-x1)*(x2-x1);
    ytotal = (y2-y1)*(y2-y1);
    distance = sqrt((xtotal)+(ytotal));
    printf("The distance of the coordinate is %d", distance);
    system ("pause");
    return 0;
    }
    


  15. #15
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    distance is a float, but your printf is trying to print an integer.
    LearnOnTheFly likes this.
    Code:
    while(!asleep) {
       sheep++;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A program that can only accept a numerical value?
    By Bano360 in forum C Programming
    Replies: 4
    Last Post: 11-15-2010, 06:55 PM
  2. help! calculate arbitrary number of floating points
    By cakestler in forum C Programming
    Replies: 5
    Last Post: 02-26-2009, 01:47 PM
  3. Rectangular Approximation Program Help
    By Noah in forum C Programming
    Replies: 4
    Last Post: 03-15-2006, 01:23 PM
  4. Segfaulting Distance Program
    By radiohead in forum C Programming
    Replies: 2
    Last Post: 01-09-2006, 07:48 PM
  5. Distance Formula in my program..... I need help fast!!!
    By Mackology101 in forum C Programming
    Replies: 3
    Last Post: 09-23-2004, 10:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21