Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 1 decade ago

Simple C program Help computer programming C?

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.

-----------------------------------------------------------------------------------------------------

#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);

}

6 Answers

Relevance
  • cja
    Lv 7
    1 decade ago
    Favorite Answer

    If your friend says he knows C++, but not C, he doesn't know C++. C++ is a superset of C

    See below for my translation of this code into C. Notice how my sscanf statement provides input validation and allows some flexibility in the input format. Also notice how I've moved all variable declarations before the statements. Traditional C doesn't allow inline declarations as C++ does.

    #include <stdio.h>

    #include <math.h>

    #define MAX_STR_LEN 1024

    int main (int argc, char *argvp[]) {

        char line[MAX_STR_LEN];

        int A1, A2, A3, B1, B2, B3;

        double A2X, B3X, X, Y, ans;

        do {

            printf("\nEnter integers A1,A2,A3,B1,B2,B3 : ");

            fgets(line,MAX_STR_LEN,stdin);

        } while (sscanf(line, "%d ,%d ,%d ,%d ,%d ,%d",

                        &A1,&A2,&A3,&B1,&B2,&B3) != 6);

        A2X = sqrt(pow((double)A2,2)/2);

        B3X = sqrt(pow((double)B3,2)/2);

        X = A1 + A3 + B2 + A2X + B3X;

        Y = A2X + B3X;

        X = pow(X, 2);

        Y = pow(Y, 2);

        ans = sqrt(X + Y);

        printf("\nThe answer is: %g\n", ans);

        return 0;

    }

    Sample run:

    Enter integers A1,A2,A3,B1,B2,B3 : 1,2,3

    Enter integers A1,A2,A3,B1,B2,B3 : 1 , 2,3 ,4 , 5, 6

    The answer is: 15.7106

  • Bill
    Lv 4
    1 decade ago

    There is no cin or cout in C.

    Use scanf() and printf() respectively.

    Now, when using scanf, you need an extra method to allow it to take in any string.

    Also, keep in mind that there are no actual strings in C. In C, strings are just arrays of chars that end in a null or 0 char (not '0', ASCII value of 0).

    Also, there is no std:: stuff, cut that all out (As far as I know, I mean, there may be some third pary stuff, or something...) Anyway, yeah, just use include. There are good examples online, everywhere. Just look on Google, google knows all.

  • Bonnie
    Lv 4
    4 years ago

    the following code would work, #include <stdio.h> #include <conio.h> void main() { clrscr(); printf("\n hello"); getch(); } the reason why your program did give the expected output is that you didn't use "getch();" in your code. when you give this getch(); then only when you press any key, you can come out of the console window, where as if you don't give it, without showing you the required output, the control returns back to the program. so it is wrong to say that the program didn't generate the output, which means the program had given the required output but that is not visible. so try the above code and see. i hope this would have cleared your doubt. "ALL THE BEST FOR YOUR STUDIES"

  • Anonymous
    1 decade ago

    Be sure it is in correct syntax, a lot can go wrong or not work at all if part of the script isn't typed correctly as a command.

  • How do you think about the answers? You can sign in to vote the answer.
  • 1 decade ago

    It compiles and runs correctly for me under Cygwin on Windows, but %d should be %f in the printf().

    Source(s): www.cygwin.com
  • 1 decade ago

    Everything looks correct to me. convert it to C, and test it.

Still have questions? Get your answers by asking now.