c code doesnt work on turbo c++ with the float numbers - c

#include <stdio.h>
#include <math.h>
int main(void)
{
double r,d,x,y,pi;
clrscr();
printf("Input the value of r and degree: ");
//scanf("%f",&r);
//scanf("%f",&d);
r = 12;
d = 195;
pi = 3.14;
x = r * cos(d * pi/180);
y = r * sin(d * pi/180);
printf("The polar form is: (%f,%f)", x, y);
getch();
}
In the 1st case with defined values of r and d the output comes correct but in 2nd case when I give the input the output doesn't match with the original answer. The code worked on codeblocks but isn't working on turbo c++.
what am I doing wrong in turbo c++?

Mis-matched format specifier. Use "%lf" with a double *
double r,d,x,y,pi;
...
//scanf("%f",&r);
//scanf("%f",&d);
scanf("%lf",&r);
scanf("%lf",&d);
Better codes checks for success before using r.
if (scanf("%lf",&r) != 1) Handle_Error();
Output is Cartesian coordinates. #Jens. I'd also recommend a '\n'.
// printf("The polar form is: (%f,%f)", x, y);
printf("The Cartesian form is: (%f,%f)\n", x, y);
It appears Turbo-C requires "l" when printing double, so use
printf("The Cartesian form is: (%lf,%lf)\n", x, y);
Little reason to use a low precision pi. Suggest
pi = acos(-1.0);

In:
//scanf("%f",r);
//scanf("%f",d);
You need to pass the addresses of the variables, &r and &d.

Related

Example of a colloquium exam

Check the image
This is my 1st post so have that in mind while reading my question.
I have an exam of a colloquium but my code does not provide me the correct result.
So if anyone could help me that would be great. :)
These are the informations that are provided in the exam:
A function y=f(x)=ax^2+bx+c
We have to find the surface that is below the chart but keep in mind that dx(Delta X)=B-A and the height goes like this: A,A+dx,A+2dx, .... , B-dx.
As dx value gets lower the surface will be more accurate.
You have to write the program so that the surface with precision 0.001
This is my code so could someone who is good in C check it please.
Thank you.
#include <stdio.h>
#include <math.h>
int main()
{
int a,b,c;
double A,B,dx,p,D,q,x,y,nv,nv1,nv2,sv;
do{
printf("Insert a & b: "),scanf("%lf %lf",&A,&B);
} while(A<1 || B<1);
nv=dx=B-A;
do{
printf("enter odds: "),scanf("%d %d %d",&a,&b,&c);
p=(-b)/2;
D=sqrt(pow(b,2)-4*a*c);
q= -D/4*a;
} while( a<0 || p<0 || q<0);
do{
sv=nv;
dx/=2;
nv=0;
for(x=A;x<p;x+=dx)
for(dx=B-A;dx<q;dx/=2)
nv1+=x*dx;
for(y=p;y<=B;y+=dx)
for(dx=q;dx<B;dx/=2)
nv2+=y*dx;
nv=nv1+nv2;
}while(fabs(nv-sv)>0.001);
printf("The surface is %lf",nv);
return 0;
}
You want to find the approximation of a definite integral of a quadratic function. There are several issues with your code:
What is the restriction of A ≥ 1 and B ≥ 1 for? A parabola is defined over the whole abscissa. If anything, you should enforce that the input is numeric and that two values were given.
You don't need to find the vertex of the parabola. Your task is to create small rectangles based on the left x value of each interval as the image shows. Therefore, you don't need p and q. And you shouldn't enforce that the vertex is in the first quadrant on the input without indication.
Why are the coefficients of the parabola integers? Make them doubles to be consistent.
Because you don't need to know the vertex, you don't need to split your loop in two. In your code, you don't even check that p is between A and B, which is a requirement of cour code.
What is the inner loop for? You are supposed to just calculate the area of the current rectangle here. What's worse: you re-use the variable dx as iteration variable, which means you lose it as an indicator of how large your current interval is.
The repeated incrementing of dx may lead to an accumulated floating-point error when the number of intervals is large. A common technique to avoid this is to use an integer variable for loop control and the determine the actual floating-point variable by multiplication.
The absolute value as a convergence criterion may lead to problems with small and big numbers. The iteration ends too early for small values and it may never reach the criterion for big numbers, where a difference of 0.001 cannot be resolved.
Here's a version of your code that puts all that into practice:
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c;
double A, B;
printf("Lower and upper limit A, B: ");
scanf("%lf %lf", &A, &B);
printf("enter coefficients a, b, c: ");
scanf("%lf %lf %lf", &a, &b, &c);
double nv = 0;
double sv;
int n = 1;
do {
int i;
double dx;
sv = nv;
n *= 2;
dx = (B - A) / n;
nv = 0;
for (i = 0; i < n; i++) {
double x = A + i * (B - A) / n;
double y = a*x*x + b*x + c;
nv += dx * y;
}
} while(fabs(nv - sv) > 0.0005 * fabs(nv + sv));
printf("Surface: %lf\n", nv);
return 0;
}
The code is well-behaved for empty intervals (where A = B) or reversed intervals (where A > B). The inpt is still quick and dirty. It should really heck that the entered values are valid numbers. There's no need to restrict the input arbitrarily, though.

Having Trouble with Creating A Program to Measure the Distance Between Two Points in C

I am trying to write a program in C that calculates the shortest distance between two points here on earth (So I have to use latitude and Longitude). I am using the haversine formula to help me calculate this distance. When I wrote the code I got a bunch of errors (The main errors being C2065 undeclared identifier errors.
#include <stdio.h>
#include <math.h>
//Pi number in math
#define pi 3.14159265358979323846
//This is the radius of the earth in KM
#define R 3959
int main ()
{
double lat1;
double lon1;
double lat2;
double lon2;
//The User's initial Input for Point 1
printf("Please Enter Your Latitude anghghd Longitude foroik Point 1\n");
scanf ("%d%d", &lat1, &lon1);
printf("Please Enter Your Latitude and Longitude for Point 2\n");
scanf("%d%d", &lat2, &lon2);
double phi1;
if (lat1>0)
{
phi1= 90-lat1;
}
else
{
phi1=90+lat1;
}
double phi2;
if (lat2>0)
{
phi2=90-lat2;
}
else
{
phi2=90+lat2;
}
double theta1;
if (lon1>0)
{
theta1=lon1;
}
else
{
theta1=-lon1;
}
double theta2;
if (lon2>0)
{
theta2=lon2;
}
else
{
theta2=-lon2;
}
double c;
//This is our Haversine formula to calculate the two distances between two points
c = sin(phi1)*sin(phi2)*cos(theta1-theta2)+cos(phi1)*cos(phi2);
int d;
//The shortest great circle distance between two points
d = R*acos(c);
printf("The distance between the two points is %d \n", d);
return (0);
}
The type of errors I am getting have been C2065 undeclared Identifier which is making me believe that the if statements I am using to describe phi and theta do not work. Let me know what you think!
You are running into errors at your scanf calls. "d" corresponds to an integer not a double, use f instead.
If you're writing C in Visual Studio, which it sounds like you are, you'll likely have to declare all your variables at the beginning of the function (or block), because it doesn't support a modern version of C that lets you do otherwise.
In other words, you'll have to do:
int main(void)
{
double lat1;
double lon1;
double lat2;
double lon2;
double phi1;
double phi2;
double theta1;
double theta2;
and so on, and only then go on with your printf() calls and stuff.

what data type should i use for printing a number like 64.432785439?

what data type i need to use and what %?
i tried lots of them and i printed only 0 or 0.00000
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[]) {
int ab;
int ac;
int bc;
printf("introdu valoarea laturii AB:\n");
scanf("%d", &ab);
printf("introdu valoarea laturii BC:\n");
scanf("%d", &bc);
printf("introdu valoarea laturii AC:\n");
scanf("%d", &ac);
int per= (bc+ac+ab)/2;
unsigned int x= sqrt(per*(per - ab)*(per-bc)*(per-ac));
printf("aria triunghiului este: %x", x);//printarea ariei triunghiului
}
You can use a double like this:
double per= (bc+ac+ab)/2.0;
double x= sqrt(per*(per - ab)*(per-bc)*(per-ac));
printf("aria triunghiului este: %lf", x);
EDIT:-
If you are not passing the int value as the side of your triangle then you need to change the datatype of your input variables as well
double ab = 0.0;
double ac = 0.0;
double bc = 0.0;
Working Demo
printf("aria triunghiului este: aria %.9f", x);
This will print a float type with nine decimal places, but you'll need a float type and x in your case is an unsigned int.
Change your last lines to:
float per= (bc+ac+ab)/2.0f;
double x= sqrt(per*(per - ab)*(per-bc)*(per-ac)));
printf("aria triunghiului este: aria %.9lf", x);
You probably need to have all your variables declared as double.
double ab=0.0, ac=0.0, bc=0.0, x=0.0;
I think that initializing variables is a good habit. You could initialize them with NAN from <math.h> ...
Read them with e.g.
if (scanf("%f", &ab)<=0)
{ perror("failed ab input"); exit(EXIT_FAILURE); };
Print them using %g or %.6f etc.
You absolutely need to read the floating point guide i.e. What Every Programmer Should Know About Floating-Point Arithmetic
You should also read about every standard function you are using, in particular about scanf(3) and printf(3) etc.
You should compile with all warnings and debug info (e.g. gcc -Wall -g) and learn how to use the debugger (e.g. gdb). Learn right now about it!
Regardless of your example,
for printing a fixed point value in c you should use %f, like:
double someDoubleVar=64.123132123;
printf("%f",someDoubleVar);
you can control the number of digits after the point by doing:
double someDoubleVar=64.123132123;
printf("%.3f",someDoubleVar);
(copy it and you will see the output will be 64.123).
In your program:
double per= (bc+ac+ab)/2.0;
double x= sqrt(per*(per - ab)*(per-bc)*(per-ac));
printf("aria triunghiului este: %lf", x);
will probably do the job.
you can consider useing only double variables if so comment.

Standard Deviation Program

This was the instructions: Write a program that starts out asking the user for the mean u and
standard deviation s for the normal distribution
(see the wiki article http://en.wikipedia.org/wiki/Normal_distribution
The program then asks for an N, and then asks for N values x. For each x it
writes out f(x) to the screen.
Note that the program asks the user for u, s, and N just once. After that it
asks for N values for x, one by one. After each value x it writes out the
corresponding value of the function.
Of course, use double precision for this and use the square root and
exponential function from the standard math library.
This is my code so far but I can't get the N to work.
#include <stdio.h>
#define PI 3.141592653589793238462643383
#define E 2.7182818284590452353602874713526624977572470937
#include <math.h>
#include <stdlib.h>
int main()
{
double u,s,N,x,math1, math2, math3,n,;
printf("Enter Mean: \n");
scanf("%d", &u);
printf("Enter Deviation: \n");
scanf("%d", &s);
n=1/2;
math1 =1/(u*sqrt(2*PI));
math2= (x-u)/s * (x-u)/s;
math3= E * exp(n);
x = math1 * exp(math3)*exp(math2);
printf("%d \n", x);
system("Pause");
}
n=1/2;
This will be equal to 0, since 1 is an integer, 2 is an integer and 1 divided by 2 is 0 in integer math.
Try 1.0/2.0
Make sure every other division has a double on one or both sides, otherwise it will be done as integer math

Program that calculates area between quadratic and x-axis in C

I'm new to this whole programming things so bear with me.
I want to make a program that could calculat the area between quadratic and x-axis.
Right now my code is only designed for functions were a is postive and c is negative.
#include <stdio.h>
int main()
{
float a;
float b;
float c;
float x; /* this is the zero that is to the right*/
float y; /* this is the zero that is to the left*/
float z;
{
printf("Consider the function ax^2 + bx + c\n");
printf("Enter the a value: \n");
scanf("%f",&a);
printf("Enter the b value: \n");
scanf("%f",&b);
printf("Enter the c value: \n");
scanf("%f", &c);
x = (( -b + sqrt(b*b - 4*a*c)) / (2*a));
y = (( -b - sqrt(b*b - 4*a*c)) / (2*a));
do {
z=(((y+0.01)-(y))*((a*(y*y))+(b*y)+(c)));
y+0.01;} while (x>y);
if (y>=x) {
printf("The area is %f", z);
}
The problem is that the program just never stops running. What im trying to do is to make small squares and measure their area (remmember LRAM and RRAM). So what im doing is (zero + a little bit) times y value (a*(y*y))+(b*y)+(c)))`
Any tips?
In
do {
z=(((y+0.01)-(y))*((a*(y*y))+(b*y)+(c)));
y+0.01;
} while (x>y);
you should change y+0.01 to y += 0.01, if you use y+0.01, y never change during the loop.
Increment both z and y in the do while loop.
z = 0; //initialize z to remove any garbage values.
do {
// here z is NOT getting assigned, but incremented each time this loop runs.
// NOTE: A += C; is short for A = A + C;
z += (((y+0.01)-(y))*((a*(y*y))+(b*y)+(c)));
y += 0.01;
}
while (x>y);
I understand it doesn't help immediately, but for stuff like this, Numerical Recipes is the ultimate book. The old C version (which of course, still works quite nicely in C++) is available for free. They also have a C++ version you can buy.
It has code for every algorithm in the book, and explains step by step what you are doing and why.
Link to homepage
Link to C version

Resources