Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Program I did for a class today...did I do it correctly? (http://www.programmingforums.org/showthread.php?t=15059)

WalterSlopeboi Jan 28th, 2008 2:08 PM

Program I did for a class today...did I do it correctly?
 
Instructions:

I. The program asks for the user to input the magnitudes and angles (with respect to X-axis) for two vectors, A and B. It then calculates the resultant vector (R) by summing the X- and Y-components of the two vectors, and prints the magnitudes, and angles (with respect to X-axis) of the vectors A, B, and R on the screen in the format given below.

II. Helpful Math Hints
Rad_A = Deg_A * PI/180

Rx = A cos (rad_A) + B cos(rad_B)
Ry = A sin (rad_A) + B sin(rad_B)

R = √ (Rx^2 + Ry^2), and rad_R = [atan(Ry/Rx)*180/PI]

III. Helpful Programming Hints:

1. Preprocessor define PI = 3.14159
2. Define variables A, B, R, deg_A, rad_A, deg_B, Rad_B, deg_R, rad_R, Rx, Ry as "double/or float"
3. Ask user to enter A in newtons, read the value and assign to A; ask to enter deg_A in degrees, read the value (scan in lf format/or float number as appropriate) and assign to deg_A, and so on...for all the input variables.
4. Computer the rad_A, rad_B, and computer the components (Rx and Ry) of the resultant (R):
5. Computer the resultant vector's magnitude: R = √ (Rx^2 + Ry^2)
6. Compute the resultant vector's directions in degrees:



:

#include <stdio.h>
#include <math.h>
#define PI 3.14159

int main()
{
double A,B,R,deg_A,rad_A,deg_B,rad_B,deg_R,rad_R,Rx,Ry;

printf("*My name* 2441203 SA33\n");
printf("ESC151 Spring 08\n");
printf("Recitation 1 Jan 28, 2008\n\n\n");


printf("Enter magnitude of A in newtons \n");
scanf("%lf", &A);
printf("Enter direction of A in degrees \n");
scanf("%lf", &deg_A);
printf("Enter magnitude of B in newtons \n");
scanf("%lf", &B);
printf("Enter direction of B in degrees \n");
scanf("%lf", &deg_B);

rad_A=deg_A*PI/180;
rad_B=deg_B*PI/180;
Rx=A*cos(rad_A)+B*cos(rad_B);
Ry=A*sin(rad_A)+B*sin(rad_B);
R=sqrt(Rx*Rx+Ry*Ry);
deg_R=atan(Ry/Rx)*180/PI;

printf("Force(N)                                Direction(degrees) \n");
printf("A %lf                                        %lf\n",A,deg_A);
printf("B %lf                                        %lf\n",B,deg_B);
printf("R %lf                                        %lf\n:,R,deg_R);

        return 0;

}


Sane Jan 28th, 2008 2:23 PM

Re: Program I did for a class today...did I do it correctly?
 
Why don't you tell me?

It looks promising, but you have a typo on your last printf statement. Literals are terminated with a quotation mark. Not a colon.

See what happens when you input:

:

1
270
1
180


You get an answer in the first quadrant. The answer should be in the third quadrant (225) with magnitude sqrt(2). Do you need to account for this?


All times are GMT -5. The time now is 3:55 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC