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
Related
#include <stdio.h>
#include<math.h>
int series(float,float);
int main()
{
float x,n,series_value;
printf("Enter the value of x: ");
scanf("%f",&x);
printf("\nEnter the value of n: ");
scanf("%f",&n);
series_value=series(x,n);
printf("\nValue of series sin (%.2f) is: %f\n",x,series_value);
return 0;
}
int series(float x,float n)
{
int i,sum=0,sign=-1;
int j,fact=1,p=1;
for (i=1; i<=(2*n)-1; i+=2)
{
for (j=1; j<=i; j++)
{
p=p*x;
fact=fact*j;
}
sign=-1*sign;
sum=sum + sign*p/fact;
}
return (sum);
}
Output:
Enter the value of x: 5
Enter the value of n: 10
(lldb)
and this message
Thread 1: EXC_ARITHMETIC (code=EXC_I386_DIV, subcode=0x0)
![Thread 1 Queue : com.apple.main-thread (serial)
]1
Why is this message coming? and what is wrong in the program as answer is not coming right
There is a few problems with your code. As #PaulHankin said, when fact overflows and becoms zero, you will have a division by zero, and "weird things" happen.
Your factorial and power calculation is also wrong. You are recalculating it in each iteration of the outer loop without reseting fact and p first:
fact = 1; // You need to reset fact and p to its start value here
p = 1;
for (j=1; j<=i; j++)
{
p=p*x;
fact=fact*j;
}
Your third problem is that for your function calculate the correct value for sin, which is not an integer value, you need to use float, or even better double, when calculating sum. So sum must be declared float, and the division p/fact must use float division. By also declaring p and fact as float, you will solve both the overflow issue, and use the correct division. Naturally your function must also return a float
float series(float x,float n)
{
int i,sign=-1;
int j,
float sum = 0;
float fact = 1;
float p = 1;
for (i=1; i<=(2*n)-1; i+=2)
{
fact = 1;
p = 1;
for (j=1; j<=i; j++)
{
p=p*x;
fact=fact*j;
}
sign=-1*sign;
sum=sum + sign*p/fact;
}
return (sum);
}
This code still has a minor problem. By having an inner loop, it is slower than necessary. Since this probably is homework, I am not getting rid of that loop for you, just giving you a hint: You don't have to recalculate fact from scratch on each iteration of the outer loop, just try to find out how fact changes from one iteration to the next. The same goes for p.
//Series of Sinx
#include<stdio.h>
#include<math.h>
#define ACCURACY 0.0001
int factorial(int n);
int main()
{
float x,sum,term;
int i,power;
printf("Enter value of X: ");
scanf("%f",&x);
i=1;
power=3;
sum=x;
term=x;
while(term>=ACCURACY)
{
term = pow(x,power) / factorial(power);
if(i%2==1)
{
sum -= term;
}
else
{
sum += term;
}
power+=2;
i++;
}
printf("sin(%f) = %.6f\n",x,sum);
return 0;
}
int factorial(int n){
int i=n,fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
return fact;
}
plenty bugs. To do not caclulate the fact values all the time they are in the lookup table
#include <stdio.h>
#include <math.h>
double series(double,int);
long long fact[] = { 1, 2, 6, 24,
120, 720, 5040, 40320,
362880, 3628800, 39916800, 479001600,
6227020800, 87178291200, };
double mypow(double x, unsigned p)
{
double result = x;
while(p && --p)
result *= x;
return result;
}
int main()
{
for(double x = 0; x <= M_PI + M_PI / 60; x += M_PI / 30)
printf("Value of series sin (%.2f) is: %f\n",x,series(x, 5));
fflush(stdout);
}
double series(double x,int n)
{
double sum = x;
int i,sign=1;
for (i=3; i<=(2*n)-1; i+=2)
{
sign=-1*sign;
sum += sign*(mypow(x, i)/fact[i -1]);
}
return (sum);
}
https://godbolt.org/z/U6dULN
maybe its due to floating-point exception as u have declared that the function should return int type value
int series(float,float);//hear
so u can try editing the return type of this function as float
Note:-also u need to change at function definition and the datatype of
int i,sum=0,sign=-1;
int j,fact=1,p=1;
to float as it is returning the value (sum) which should also be float
I'm new to C and I'm having some problems in this code where I'm getting these errors.
sum2.c: In function 'main':
sum2.c:22:6: warning: 'z' is used uninitialized in this function [-Wuninitialized]
int z = twice(x, z);
On my code I needed to add a fuction twice, which, given a number, calculates its double, using only the elementary operations and the sum function. And I don't know if the way I put the function is correct.
//USER
//3532
#include <stdio.h>
int sum(int x, int y){
return y == 0 ? x : sum(x+1, y-1);
}
int twice(int x, int z){
z = x * x;
return 0;
}
int main(void){
int x;
int y;
scanf("%d%d", &x, &y);
int z = twice(x, z);
printf("%d\n", z);
return 0;
}
The instructions say that twice is "given a number", but you've defined it as taking two numbers. It only needs one parameter.
And you're supposed to use your sum() function. There's no need to multiply x*x (that's the square of the number, not twice the number), nor is there any point in assigning a variable in the return statement.
You only need to read one number as input to test this.
#include <stdio.h>
int sum(int x, int y)
{
return y == 0 ? x : sum(x+1, y-1);
}
int twice(int x)
{
return sum(x, x);
}
int main(void)
{
int x;
int w;
scanf("%d", &x);
int w = twice(x);
printf("%d\n", w);
return 0;
}
According to your description the function twice should accept only one argument.
So this function definition
int twice(int x, int z)
{
z = x * x;
return 0;
}
does not make sense. Moreover the function always returns 0.
Also take into account that the type int is a signed integer type. The user can input a negative number. In this case your function sum will yield a wrong result.
The sum of two integers of the type int can be too big to fit in an object of the type int. That is there can be for example an overflow.
The functions can be defined the following way as it is shown in a demonstrative program.
#include <stdio.h>
long long int sum( int x, int y )
{
return y == 0 ? ( long long int )x
: sum( y < 0 ? x - 1 : x + 1, y < 0 ? y + 1 : y - 1 );
}
long long int twice( int x)
{
return sum( x, x );
}
int main(void)
{
int x;
printf( "Enter a number: " );
scanf("%d", &x);
long long int result = twice( x );
printf("The sum is %lld\n", result );
return 0;
}
Its output might look like
Enter a number: -5
The sum is -10
Or
Enter a number: 5
The sum is 10
I am in a beginner c class and when I run my code it drops some decimal places. According to my handwritten equation of the same type it should equal 99.5 can someone tell me what I'm doing wrong?
#include <stdio.h>
//create the number variable
double number = 0;
//function prototypes
double get_input();
double get_next(double one, double two);
void print_result(void);
int main(){
//get all the input needed
double x_two = get_input();
double x_one = get_input();
//calculate third
double x_three = get_next(x_one,x_two);
//calculate fourth
double x_four = get_next(x_three,x_two);
//calulate fith and set to number
number = get_next(x_four,x_three);
//print the result
print_result();
}
double get_input(void){
double number = 0;
//prompt the user for the information needed
printf("Please enter a value > ");
//take the user info and pass it back
scanf("%lf",&number);
return number;
}
double get_next(double minus_one, double minus_two){
double number = (minus_two/2)+(3*minus_one);
return number;
}
void print_result(void){
printf("The result is: %lf",number);
}
The equation is given as such
Xn =
Xn−2/2 + (3 * xn-1)
the numbers I plug in for n-2 is 2 and n-3 is 3
Your get_input() needs to return a value. return number;
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.
the formula is pretty complicated. the numerator is num and the denominator is den, in the formula there is a root on the denominator so i have putted den in sqrrt() but sqrrt only accepts doubles
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define LEN 11
// for the following set of x and y find r by the formula ..
float sum(float arr[]);
void main(void)
{ int i;
float x[]={43.22,39.87,41.85,43.23,40.06,53.29,53.29,54.14,49.12,40.71,55.15};
float y[]={102.43,100.93,97.43,97.81,98.32,98.32,100.07,97.08,91.59,94.85,94.6};
float num,den[LEN],r[LEN],xy[LEN],x2[LEN],y2[LEN];
for(i=0;i<LEN;i++)
{
x2[i]=x[i]*x[i];
y2[i]=y[i]*y[i];
xy[i]=x[i]*y[i];
}
num=sum(xy)-sum(x)*sum(y);
for(i=0;i<LEN;i++)
{
den[i]=((LEN*sum(x2)-(sum(x))*(sum(x)))*(LEN*sum(y2))-(sum(y2))*(sum(y2)));
r[i]=num /sqrt(den); /*<----------the problem is here-----> */
}
printf("%f",r);
getch();
}
float sum(float arr[])
{
int i;
float total=0;
for(i=0;i<=LEN;i++)
{
total+=arr[i];
}
return total;
}
Out of sheer boredom I have fixed your code. It is still ugly and extremely inefficient but compiles and should work. I'll leave you or someone else to make it decent.
#include <stdio.h>
#include <math.h>
#define LEN 11
// for the following set of x and y find r by the formula ..
float sum(float arr[]);
int main(void)
{ int i;
float x[]={43.22,39.87,41.85,43.23,40.06,53.29,53.29,54.14,49.12,40.71,55.15};
float y[]={102.43,100.93,97.43,97.81,98.32,98.32,100.07,97.08,91.59,94.85,94.6};
float num,den,r[LEN],xy[LEN],x2[LEN],y2[LEN];
for(i=0;i<LEN;i++)
{
x2[i]=x[i]*x[i];
y2[i]=y[i]*y[i];
xy[i]=x[i]*y[i];
}
num=LEN*sum(xy)-sum(x)*sum(y);
den = (LEN*sum(x2)) - sum(x)*sum(x);
float alpha = sum(y)/LEN - (num/den)*sum(x)/LEN;
printf("beta = %f, alpha = %f\n", num/den, alpha);
for(i=0;i<LEN;i++)
{
float term = y[i] - alpha - (num/den)*x[i];
r[i] = (term*term);
printf("%f",r[i]);
}
}
float sum(float arr[])
{
int i;
float total=0;
for(i=0;i<=LEN;i++)
{
total+=arr[i];
}
return total;
}
To be consistent with the rest of the code, you should presumably be writing:
r[i] = num / sqrt(den[i]);
However, the calculation is not one I recognize. The body of the second loop is going to produce the same result for each value in den and therefore also in r, which is probably not what the question asked for.
You need to give the index den[i] at the denominator....instead in your code you have just passed the base address!
r[i]=num /sqrt(den[i]);
If this is what you want to achieve, which is quite unclear.