I wrote this code in C:
double x1 = 7.52, x2 = 7, x3 = 8;
int m = 0;
double sum = x1 + x2*m + x3*m*m;
printf("%lf", m, sum);
but sum is always 0 no matter what the value of m that i changed..
why doesnt it make normal calculation ?
thanks
Because your are printing m which is int with the "%lf" specifier which is for double.
And you are also passing more arguments to printf() than format specifiers, which means that you are not enabling compiler warnings, you should, specially if you are a beginner.
Change this
printf("%lf", m, sum);
to
printf("m = %d\nsum = %f\n", m, sum);
and see what I mean.
Actually you are printing the value of m -
printf("%lf", m, sum);
You have to print -
printf("%lf",sum);
Related
I have the following code in c
#include <stdio.h>
int main(void)
{
int a, b, c, sum;
double d, p;
sum = a + b + c;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
d = sum / 3;
printf("3个整数的平均值是:d=%.2f", d);
p = a / sum * 100;
printf(",第一个数占和的比例是:p=%4.2f%%", p);
}
It is about entering three integers from the keyboard, average them, and calculate the ratio of the first number to the sum of the three numbers. The output result retains 2 decimal places .
I cannot find where is wrong.
The two main issues are:
You calculate sum with uninitialized values for a, b and c. Move that calculation to after a successful scanf() to ensure those variables are set.
You probably want to do the calculations of d and p with double, rather than integer, precision. I make use of automatic type conversion via fractional constants. The other two options are to change the type of sum from an int to a double, or explicitly use a type cast (see answer by #FeihuLiu).
Minor issues:
Original code was formatted poorly (since fixed by one of our friends :-).
Optional for main() but it's a good idea to return an integer as your declaration said you would.
(not fixed) If you don't use p or d for anything else, consider just eliminating them in favor of doing the calculation call to printf()
It's generally a good idea to reduce the scope of variables so I moved those definitions to just before they are used.
#include <stdio.h>
int main(void) {
printf("请输入三个整数:");
int a, b, c;
if (scanf("%d %d %d", &a, &b, &c) != 3) {
// TBD: translate error message
printf("scanf failed\n");
return 1;
}
int sum = a + b + c;
double d = sum / 3.0;
printf("3个整数的平均值是:d=%.2f", );
double p = 100.0 * a / sum;
printf(",第一个数占和的比例是:p=%4.2f%%", p);
return 0;
}
Besides the sum problem, integer division will result in integer. You can use the following code:
#include <stdio.h>
int main(void) {
int a, b, c, sum;
double d, p;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
sum = a + b + c;
d = (double) sum / 3;
printf("3个整数的平均值是:d=%.2f", d);
p = (double) a / sum * 100;
printf(",第一个数占和的比例是:p=%4.2f%%", p);
}
I am currently reading an online version of Stephen Kochan's "Programming in C (3rd Edition)." One of the activities involves evaluating an equation,
Write a program that evaluates the following expression and displays the results
(remember to use exponential format to display the result):
(3.31 x 10-8 x 2.01 x 10-7) / (7.16 x 10-6 + 2.01 x 10-8)
When I attempt to do this, the output is always 0.0000. Here is my code.
#include <stdio.h>
int main (void) {
float result;
result = (3.31 * pow(10,-8) * 2.01 * pow(10,-7)) / (7.16 * pow(10,-6) + 2.01 * pow(10, -8));
printf ("%f", result);
return 0;
}
If I am doing anything wrong, please point it out. If you have any tips, please say so.
You must #include <math.h>
Also, change to this:
printf ("%e\n", result);
You should probably also have
double result;
because pow() returns a double.
Use %e to get an exponent result value as instructed.
int main (void)
{
float i;
float j;
float result;
result = i / j;
i = 3.31 * 10 -8 * 2.01 * 10 -7;
j = 7.16 * 10 -6 + 2.01 * 10 -8;
printf("%e", i, j, result);
return 0;
}
I am trying to make a program that gives a specific sum until some point that I define, here it is:
float sum(int n,float m);
main(void) {
float a,m=1.0;
int n;
scanf_s("%ld", &n);
a = sum(n, m);
printf("%f", a);
}
float sum(int n, float m) {
if ((n/m) < 0.0005) {
return 0;
}
else {
return n/m + sum(n, m + 2);
}
}
(Notice that the point I defined is 0.0005) When I give a value bigger or equal to 5, program gives me this error:
...has stopped working
Also, when I increase the defined point to like 0.5, the number of values I can give increases too. Why do you think is this happening and how can I fix it?
The %ld format specifier to scanf_s expects a long int * argument. What you're passing in is a int *. These types are incompatible. Using the wrong format specifier invokes undefined behavior, which in this case manifests as a crash.
The proper format specifier for an int * is %d:
scanf_s("%d", &n);
EDIT:
The crash you're seeing is probably a stack overflow. The sum function will recursively call itself 1000 * n times. I see a similar error under MSVC but at a different limit. You can get around this by going with an iterative solution:
float sum(int n, float m){
float result = 0;
while ((n/m) >= 0.0005){
result += n/m;
m+=2;
}
return result;
}
There is a serie for the exp function whitch looks like this:
exp(x) = (x^0)/0! + (x^1)/1! + (x^2)/2! + (x^3)/3! + ···. And I'm trying to compute it for different values of x, checking my results with a calculator and I found that for big values, 20 for example, my results stop increasing and get stuck in a value that is almost the real one. I get 485165184.00 and the real value is 485165195.4.
I must do this code in a for cycle or a recursive function, since it is a homework assignment.
My code looks as following
#include <stdio.h>
#define N 13
#define xi 3
double fun7(int n, int m){
int i;
double res=1, aux=0;
for(i=1, aux=1; i<(n+1); i++){
res += aux;
aux *= m;
aux /= i;
}
return res-1;
}
int main() {
int a, b, pot, x[xi];
float R[N][xi];
x[0] = 5;
x[1] = 10;
x[2] = 20;
for(b=0; b<xi; b++){
for (a=0, pot=1; a<N; a++){
R[a][b] = fun7(pot, x[b]);
pot *= 2;
}
}
for(b=0; b<xi; b++){
for (a=0, pot=1; a<N; a++){
printf("%d\t%f\n", pot, R[a][b]);
pot *= 2;
}
printf("\n");
}
return 0;
}
The float data type can normally represent numbers with a tad more than 7 decimal digits of precision.
485165184 has 9 decimal digits. The last two digits are just meaningless noise as far as float goes. You really should be showing 4.851652e8, which is the correct value for exp(20) with the given level of precision.
If you want to increase precision, try using double or long double data types.
Consider the following:
double fact(int n)
{
int i;
double res = 1;
for (i = 1; i <= n; i++)
res *= i;
return res;
}
double f = 1;
for (int i = 0; i < 16; i++)
{
printf("%lf \n", fact(2*i + 1));
f *= (f + 1)*(f + 2);
printf("%lf \n", f);
}
Why does fact(2*i+1) results a correct value while f results a weird value of 1.#INF00?
Because it overflows.
The value of f after 16 iterations is bigger than if your code looked like this and your initial f was 2:
f *= f*f;
Which is the same as
f = f*f*f
So you take a cube 16 times - this is HUGE!
2^3 = 8
8^3 = 512
512^3 = 134217728
...
On the topic of undefined behaviour, the l length modifier in %lf is only defined for conversion specifiers using integer types. If you meant to use %Lf, then your argument should be a long double. Perhaps you meant to use %f, which corresponds to a double argument (floats end up promoted to double when passing them to variadic functions such as printf).
As Peter Ivanov explained, your calculations cause an overflow, which IIRC is also undefined behaviour.
As you've probably guessed, you might find a solution to your problem by using the long double type (and the corresponding %Lf format specifier) throughout your code...