I made the following program to calculate sum,mean and standard deviation of 5 numbers.The Sum is correct but mean is coming out to be zero ALWAYS and hence the SD is also wrong.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int sum(int, int, int, int, int);
float SD(int, int , int, int,int,int);
float mean(int);
int main()
{
int a,b,c,d,e,m;
printf("Enter 5 integers succesively by pressing enter after entering a number.\n");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
m=mean(sum(a,b,c,d,e));
printf("\nThe sum is %d , mean is %f and standard deviation is %f",sum(a,b,c,d,e),m,SD(a,b,c,d,e,m));
return 0;
}
int sum(int v, int w, int x, int y, int z)
{
return(v+w+x+y+z);
}
float SD(int v, int w, int x, int y, int z,int mm)
{
float k;
k=sqrt(((mm-v)*(mm-v) + (mm-w)*(mm-w) + (mm-x)*(mm-x) + (mm-y)*(mm-y) + (mm-z)*(mm-z))/5);
return k;
}
float mean(int v)
{
return (v/5);
}
when you use division make use float data type. in mean() and SD() function you do division by 5 and both operands are int so change one of those to float. otherwise result will be truncated to be a int.
You can change /5 to /5.0 or you can use a float type cast.
The mean you are getting as 0 since you defined m as integer int and using %f to print it.
int a,b,c,d,e,m;
printf("\nThe sum is %d , mean is %f and standard deviation is %f",sum(a,b,c,d,e),m,SD(a,b,c,d,e,m));
m is mean so make it a float type variable.
int a,b,c,d,e;
float m;
Change v/5 to v/5. .
Since v and 5 are both int, then v/5 is an integer division. You want to do a floating-point division instead, so you have to make one of the operands be floating, and 5. is a constant of type double.
Same thing applies in your SD function.
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 need to print any value that's valid to be the size of the fourth side of a quadrilateral whose other 3 sides are given.
Problem link: https://codeforces.com/contest/1422/problem/A
So below is my program for it but somehow the online judge won't accept it, giving me this for the test case#2
"wrong answer : Maximum value no less than sum other values on 96"
Now the problem is I couldn't see the input 96 on test case#2 and thus don't understand what's the problem with my code. What should I do here?
#include<stdio.h>
long long int max(long long int x, long long int y);
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
long long int a,b,c, d, num;
scanf("%llu %llu %llu", &a, &b, &c);
num = max(a,max(b,c));
printf("%llu\n", num+1); //valid fourth side size - max(a,b,c) + 1
}
return 0;
}
long long int max(long long int x, long long int y)
{
return (x > y) ? x : y;
}
I could solve it with some other valid values for the fourth side like a+b+c-1 but I think I need to know why is the above code failing.
This question already has answers here:
Why is this simple piece of code not working?
(5 answers)
Closed 5 years ago.
#include <stdio.h>
float div ( int a,int b, int c, float x );
int main()
{
int a,b,c,x;
a=250;
b=85;
c=25;
x=div (a,b,c,x);
printf("%d ", x);
}
// your code goes here
float div (int a,int b, int c, float x)
{
x=((a-b)/c);
return(x);
}
Because x is declared as int instead of float.
Your code:
int x;
...
printf("%d ", x);
What you need instead:
float x;
...
printf("%f ", x);
And your div function is wrong too:
float div (int a,int b, int c, float x)
{
x=((a-b)/c); // << this will perform an integer division
return(x); // and therefore x will be truncated, even if it is a float
}
You need this:
float div (int a,int b, int c, float x)
{
x = ( ((float)(a-b) ) / c);
return x;
}
The (float) before (a-b) is called a cast and will ensure that (a-b) will be treaded as a float number during the division by c.
BTW the function can be simplified, you don't need the x parameter:
float div (int a,int b, int c)
{
float x = ( ((float)(a-b) ) / c);
return x;
}
or even simpler:
float div (int a,int b, int c)
{
return ((float)(a-b) ) / c;
}
Look closely at the statement
x=((a-b)/c);
Note the operands, they are all integers, so the integer division is performed and then, the result is converted to floating type upon assignment. You don't want that.
In case you want a floating point division, you need to make sure one of the operands are of type float/ double.
You can use a cast, like
x=(((float)a-b)/c);
to force floating point arithmetic.
That said, in the main(), you MUST use proper conversion specifier for float, %f.
prints out an integer:
printf("%d ", x);
prints out an floating point number:
printf("%f", x);
also x must be from type float
how can I do an increment when a range is given for conversion? Following is the code:
#include <stdio.h>
int disrange(float x, float y);
int main()
{
printf("The conversion from miles to km are:");
printf("\n");
printf("Miles kilometers");
printf("\n");
// Set the range in Miles
disrange(1,5,incr); ---> extra params increment
}
int disrange(float x, float y)
{
int a;
for(a=x; a<=y; a++){
// convert each value into km
float b = a*1.61;
printf("%d %f\n",a,b);
}
}
When the increment is 2, the print out should be conversion for 1, 3, 5. Thanks!!
int disrange(float x, float y,float inc)
{
int a;
for(a=x; a<=y; a+=inc){
// convert each value into km
float b = a*1.61;
printf("%d %f\n",a,b);
}
}
If you want it in table format, try:
int disrange(float x, float y,float inc)
{
int a;
for(a=x; a<=y; a+=inc){
// convert each value into km
float b = a*1.61;
printf("%6d\t%07.5f\n",a,b);
}
}
This will make the first field a minimum of 6 characters long, with the second 7 characters long with five after the decimal place. See here for more information on printf
I have this function that is not returning a function value. I've added some random testers to try and debug but no luck. Thanks!
#include <stdio.h>
#include <math.h>
#include <time.h>
#define N 100
float error(int a, int b);
int main(){
printf("START\n");
srand(time(NULL));
int a, b, j, m;
float plot[N+1];
printf("Lower bound for x: ");
scanf("%d", &a);
printf("Upper bound for x: ");
scanf("%d", &b);
printf("okay\n");
for(j = 0; j < N; j++)
plot[j] = 0;
printf("okay1\n");
m = error(a,b);
printf("%f\n",m);
return 0;
}
float error(int a, int b){
float product = a*b;
printf("%f\n",product);
return product;
}
so the m = error(a,b) always gives 0 no matter what!
Please help. I apologise for not cleaning this up...
This is because you declared m as int. Declare it as float and cast a*b to float because a and b are also ints. Another way is change the return type of your function to int;
int error(int a, int b){
int product = a*b;
printf("%f\n",product);
return product;
}
and print m and product with %d specifier. Also do not forgot to change you function prototype in latter case.
int m;
m = error(a,b);
printf("%f\n",m);
You are trying to display m as float while it is int.
This shoud be:
m = error(a,b);
printf("%d\n",m);
My advice: stick to ints for now.
I think we have misunderstanding of how types work in C. It's not like in PHP that a variable can hold an int, then a float and some time later some string. In C variables are like steel buckets. They can change content, but never their shape. If you assign a float to an int, the floating point value will be converted into a integer value. Once you declare m as int, it will remain int to the end of it's life, capable of holding only int values.