Simple C program Help

This is a discussion on Simple C program Help within the C Programming forums, part of the General Programming Boards category; A friend has been helping me but he only knows C++ so he used C++ commands, I just want to ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    37

    Simple C program Help

    A friend has been helping me but he only knows C++ so he used C++ commands, I just want to know if the code is correct so i can then change the commands to C.
    I test it and it ask me to enter the values for A1, A2,....then when i click enter so it can give the distance between the points, it doesn't do it.


    Suppose two men (say, A and B) walk away from the same location as follows.
    Man A walks
    A1 miles to east, then
    A2 miles to north-east, then
    A3 miles to east again and stops.
    Man B walks
    B1 miles south, then
    B2 miles west, then
    B3 miles south-west, and stops
    We want to know the straight line distance between the two points where these men stopped. Solve the problem in terms of A1, A2, A3, B1, B2, B3 (user will enter values for these variables) and then write a program to compute the straight line distance between Man A and Man B.



    Code:
    #include <iostream>
    #include <math.h>
    using std:: cout;
    using std:: cin;
    using std:: endl;
    
    int main ()
    {
    int A1, A2, A3, B1, B2, B3;
    cout<<"Enter A1: ";
    cin>>A1;
    cout<<"Enter A2: ";
    cin>>A2;
    cout<<"Enter A3: ";
    cin>>A3;
    cout<<"Enter B1: ";
    cin>>B1;
    cout<<"Enter B2: ";
    cin>>B2;
    cout<<"Enter B3: ";
    cin>>B3;
    
    double A2X = sqrt(pow((double)A2,2)/2);
    double B3X = sqrt(pow((double)B3,2)/2);
    
    double X, Y;
    X = A1 + A3 + B2 + A2X + B3X;
    Y = A2X + B3X;
    
    X = pow(X, 2);
    Y = pow(Y, 2);
    double ans = sqrt(X + Y);
    
    cout<<printf("\nThe answer is: %d", ans);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Get some graph paper, and draw out the routes.
    Then work out the maths.
    Then write the program.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Word Problem!!

    If you don't get the right answer when you run your friends program on C++ compiler, then I'm quite confident, it's not right!

    The input should be no problem, is using the Pythagorean Theorem the problem or what? I'd leave your friend out of this, it's a fun problem. Work it out on paper, and then you'll have just the C part of it, to deal with.

    Surely you can think of more descriptive variable names than A1, A2, A3, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 05:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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