Here is my code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x, y, z;
double numerator;
double denominator;
printf("This program will solve (x^2+y^2)/(x/y)^3\n");
printf("Enter the value for x:\n");
scanf("%lf", x);
printf("Enter the value for y:\n");
scanf("%lf", y);
numerator = sqrt(x) + sqrt(y);
denominator = pow((x/y),3);
z = (numerator/denominator);
printf("The solution is: %f\n", z);
return(0);
}
Can anyone give me a (hopefully) quick pointer to fix my infinite loop?
There's no loop in your function, so I think it's your calls to scanf() that are causing the error:
You need to pass reference to scanf(), i.e. use scanf("%lf",&x) instead of scanf("%lf",x).
BTW, according to your fonction definition, you should use pow(x,2) instead of sqrt(x) which returns the square root.
Since this is your first question
**Welcome to stack overflow**
Your code doesnt go into an infinite loop,there is a runtime error.
Your scanf Code is flawed use this:
scanf("%lf",&x);
scanf("%lf",&y);
you want scanf to modify the value contained at the address field of your value.Please read tutorials.
Also use
numerator=pow(x,2) + pow(y,2);//numerator=x^2+y^2
It's not infinite loop, your code just returns infinity. And that is because scanf() needs a pointer to variable where it should put the read number. To get an address of variable you can use & operator like this:
scanf("%lf", &x);
scanf("%lf", &y);
Related
I am new to c, please help, the answer to this is always zero.why? Instead of converting KM to Metres or centimetres(sorry for typos);
#include <stdio.h>
int main()
{
float Km;
float metres;
float inches;
float centimetres;
printf("Welcome, please enter the distance in Km.\n");
scanf("%f", &Km);
metres = Km * 1000;
centimetres = Km*100000;
inches = Km*25/1000000;
printf("Distance In Metres is:\n");
printf("%f\n", &metres);
printf("Distance in Centimeters is:\n");
printf("%f\n", ¢imetres);
printf("Distance in Inches is:\n");
printf("%f\n", &inches);
printf("bye\n");
return 0;
}
printf function writes the value of the variable. The ampersand operator & turns your value into a pointer and that's the error. Instead of printing the actual value of your variables, you are printing the address memory of the pointer.
Read documentation on printf function. More info on & and * here.
You are printing the location of the variables. The calculation is fine, but you're not actually printing the value of the variable. You're printing where it is in memory.
The & operator will give the location of the variable. You can fix your program by removing the &s in the printf statements, i.e. this:
printf("%f\n", &inches);
becomes:
printf("%f\n", inches);
Also, here is a link to a pretty in-depth printf() reference; to learn more about pointers, you can go to this page.
I ran it, and everything seems to be fine--except that it keeps giving me a margin of error of 1. Why is it doing this?
The program is supposed to prompt the user to input an estimation for the cube root of 3, and it uses Newton's method of approximation to show how many attempts it took to get to the approximation. After 500 attempts or a margin of error less than 0.000001, it's supposed to exit the loop. Why, though, doesn't the margin of error change?
Here's my code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
float a, i, e; //declare float variables
printf("Consider the function f(x) = x^3 - 3 = 0.\n");
printf("Simplifying, we get x^3 = 3.\n");
printf("Simplifying it further, we get x = 3^(1/3).\n");
printf("Enter your estimate of the root: ");
scanf("%f", &a); //prompt user to guestimate
printf("So you're saying that x = %f.\n", a);
i=0; //initiate attempt counter
e=abs((a-pow(3, (1/3)))/pow(3, (1/3))); //margin of error formula
while (e>=0.000001 && i<=500) //initiate while loop with above expressions
{
if (a!=pow(3, (1/3)))
{
printf("Attempt %f: ", i);
a = a - (pow(a, 3) - 3)/(3*pow(a, 2));
printf("%f, ", a);
printf("%f margin of error\n", e);
i=i+1;
}
else
break;
}
}
abs() deals with ints and will return an int, you need fabsf().
In the same way, pow() is for doubles, you should use powf().
Another mistake is writing 1/3 and expecting 0.333... as a result. 1 and 3 are int literals, so the operation performed is integer division. You need to use float literals, such as 1.0f/3.0f.
That's it for type compatibility. I can see another error however : you expect e to somehow remember its formula and reapply it automagically. That's not how imperative languages work : when you write e = something, "something" is calculated and stored in eĀ once and for all. You're doing it correctly for a, now just bring e=abs(...); inside the while loop to update it each time.
I'm in a programming class right now, and was asked to create a program that calculated the sum of a user's input for multiple numbers--then calculate the nth root of the sum. If the number they input was less than 0, the loop is supposed to discard the less than 0 number, then ask again.
Unfortunately, no matter what number I input--it displays "Value needs to be greater than zero!" I tried putting a fflush(stdin); statement in the loop, but that didn't seem to do anything.
Here is my code. I really appreciate any and all help.
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
int main() {
int mTotalNums, mNth; //amount of numbers in set
float mProd = 1, x, mNroot;
printf("How many numbers are in the set?\n");
scanf("%i", &mTotalNums);
mNth = mTotalNums; //set the value of mTotalNums equal to mNth becuase we'll lose the original value of mTotalNums after the loop
while (mTotalNums > 0) {
printf("Input number: ");
scanf("%lf", &x);
if (x > 0) {
mProd *= x;
} else
printf("\nValue needs to be greater than zero!\n");
}
mNroot = pow(mProd, (1 / mNth));
printf("\nThe nth root of the product of %i terms is: %.2f\n", mNth, mNroot);
return 0;
}
"%lf" is the scanf format for a double, but x is declared as float.
To scan a float, you have to use the %f format.
Note also that mTotalNums is not decremented in the loop, so that it will never
terminate.
Read the documentation of scanf(3). Since x is declared as a float, use %f as the scanf format control string. Also, take into account the result of scanf (it would be 1 if successfully read one item).
You should enable all warnings and debug info in your compiler, then learn how to use the debugger (notably to run your program step by step, display local variables, etc....).
(On Linux, if compiling with gcc -Wall -g you would get a useful warning, and the gdb debugger would be helpful...)
Try these modifications to your program (added comments with changes made)
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
int main() {
//amount of numbers in set
int mTotalNums, mNth;
// Change to double for added precision
double mProd = 1.0, x, mNroot;
printf("How many numbers are in the set?\n");
scanf("%i", &mTotalNums);
// Set the value of mTotalNums equal to mNth becuase
// we'll lose the original value of mTotalNums after the loop
mNth = mTotalNums;
// Don't forget to decrement the loop counter
while (mTotalNums-- > 0) {
printf("Input number: ");
scanf("%lf", &x);
if (x > 0) {
mProd *= x;
} else {
printf("\nValue needs to be greater than zero!\n");
}
}
// Change to 1.0 to force compiler to treat as a double
mNroot = pow(mProd, (1.0 / mNth));
printf("\nThe nth root of the product of %i terms is: %.2f\n", mNth, mNroot);
return 0;
}
You mention "calculate the nth root of the sum", but your loop is clearly tallying the cumulative product. To change it to calculate the sum, try the following additions:
// Declare a sum variable
double sum = 0;
// Sum inside your while loop
sum += x;
// Calculate the nth root of the sum instead
mNroot = pow(sum, (1.0 / mNth));
Add printf commands to see what your variables contain before you check them in your logic statements.
You also need to do something to increment/decrement your variable for your while loop... currently nothing is changing mTotalNums, so it will be an infinite loop.
while (mTotalNums > 0) {
printf("Input number: ");
scanf("%lf", &x);
printf("x=%d", x);
if (x > 0) {
mProd *= x;
} else
printf("\nValue needs to be greater than zero!\n");
mTotalNums--;
}
I have this problem with this homework I'm supposed to do.
[ It says Create a program that's able to calculate and show the sum of S]
Like S=1+1/4+1/8+1/16 ... till 1/ [2 pow n]
So I worked on it and came up with this code
#include <stdio.h>
void main()
{
int n,i;
float p,s;
printf("Enter the maximum power n :");
scanf("%d",&n);
s=0;
p=0;
for (i=0;i<n;i++)
{
p+=1/pow(2, i);
s+=p;
printf("s = %f\n",s);
}
printf("The sum of this equation is :%f",&s);
}
But when I execute it is always like S=0.
What am I doing wrong?
You are printing an address ('&s) with%f` variable. Using a wrong specifier invokes undefined behavior. You may get anything.
Also, No need of variable s. Remove the line
s+=p;
It should be like:
#include <stdio.h>
int main(void)
{
int n,i;
float p;
printf("Enter the maximum power n :");
scanf("%d",&n);
p=0;
for (i=0;i<n;i++)
{
p+=1/pow(2, i);
printf("p = %f\n",p);
}
printf("The sum of this equation is :%f",p);
}
You need to include <math.h> to get the proper prototype of pow().
You might need to link to the math library too gcc main.c -Wall -lm
#include <math.h>
....
for (i=0;i<n;i++)
{
p=1/pow(2, i);
s+=p;
printf("s = %f\n",s);
}
printf("The sum of this equation is :%f",s);
Your program has multiple problems. Enabling compiler warnings should tell you about some of them.
You should include the C header which contains the declaration of the pow function.
You add each addend twice.
In your second printf statement, you pass a float. But the %f format specifier expects a double argument. In your third printf statement, you pass a pointer to a float.
Another cosmetic problem is that your main function should return an int.
just a guess, may you replace the line
p+=1/pow(2, i);
with
p+=1.0f/(float)pow(2, i);
and
printf("The sum of this equation is :%f",&s);
with
printf("The sum of this equation is :%f",s);
Typo may be.. but you will have say %f and s (not &s)
printf("The sum of this equation is :%f",s);
On side note:
Once you include <math.h> you will get compiler warning for using correct pow(..) prototype. Below code would be relevant.
p+=1.0f/(float)pow(2.0f, i);
I am writing this program to find the square root of a number. While defining the function mysqrt(). I am using the Newton-Raphson method. in the if() I use fabs(blah data blah) < 0.001 for relative error EX. if error is less than .1% the answer is correct. When I compile the code below
I get error line 27: called object '5.0e-1' is not a function.
I am absolutely lost as to what it is referring to. Please help Thank you.
I know there is a sqrt() function but this is for an extra credit assignment I have the majority of it written but cannot correct this error.
#include <stdio.h>
#include <math.h>
double mysqrt(double a);
int main()
{
double a, result;
printf("Enter a number to find the square root of: ");
scanf("%d", &a);
result = mysqrt(a);
printf("The square root of %d is %d \n", a, result);
}
double mysqrt(double num)
{
double x, new_x;
if((fabs(((x+1) - x)/(x+1)))<0.001)
{
for(x=2; x != num;)
{
new_x=(x+1);
new_x=(0.5(x+(num/x)));
x=new_x;
}
}
else
{
return new_x;
}
}
You forgot multiplication sign here:
new_x=(0.5(x+(num/x)));
This is a quite common error and when you see "... is not a function.", it means somewhere you have ...() which is most often a forgotten multiplication.
Also your program hard to read because of lots of parentheses, why not to write:
new_x = 0.5 * (x + num/x);
In addition, this:
(fabs(((x+1) - x)/(x+1))) < 0.001
is equivalent to:
fabs(1/(x+1)) < 0.001
Do your simplifications on paper, it will make your program more readable and errors easier to spot.