This is a simple problem of calculating the min number of coins needed to give the change, given a N value. The division 0.04/0.01 gives 3, why?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int MinQtdCoins(float N, float coins[], int qtdCoins)
{
int i, qtdMinCoins = 0;
int numCoins=0;
for(i=0; i<qtdCoins; i++)
{
if(N > coins[i] || fabs(N - coins[i]) < 0.0000000001) // N >= coins[i]
{
numCoins = (int)(N/coins[i]);
printf("Number of Coins: %f divided by %f = %d, ",N,coins[i],numCoins);
qtdMinCoins += numCoins;
N = N - numCoins*coins[i];
printf("remains: %f\n",N);
}
}
return qtdMinCoins;
}
int main()
{
int qtdCoins = 5;
float coins[] = {0.50, 0.25, 0.10, 0.05, 0.01};
float N=9.79;
printf("\n\n%d\n",MinQtdCoins(N, coins, qtdCoins));
return 0;
}
The division 0.04/0.01 gives 3, why?
numCoins = (int)(N/coins[i]);
Casting to int just truncates fractional part. So if 0.04/0.01 == 3.999.. (due to rounding), the result is 3.
you are doing floating point division and keeping the value in an integer...because of this value is truncated to 3
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 am trying to approximate Euler's number using the formula (1+(1/n))^n.
The compiler is telling me that there is an "expected expression before 'double'"
Here is the code:
#include <stdio.h>
#include <math.h>
int main()
{
int x, y, power;
int num = 1;
int position = 1;
while (position <= 100)
{
num = 1/num;
num = num + 1;
x = num;
power = double pow(x, x); //here
printf("%f", power);
position += 1;
num = position;
}
}
If you want a number to be a double (number with decimals), you need to define it as a double, not an integer. I have this code which should solve your problem. Also make sure to compile gcc FILEPATH -lm -o OUTPUTPATH if you are using UNIX.
#include <stdio.h>
#include <math.h>
int main()
{
double x, y, power, num = 1; //doubles allow for decimal places so declare it as double
int position = 1; //Position seems to only be an integer, so declare it as an int.
while (position <= 100)
{
num = 1/num;
num++;
x = num;
power = pow(x, x);
printf("%f", power);
position += 1;
num = position;
}
}
Another option is a for loop:
#include <stdio.h>
#include <math.h>
int main()
{
double x, y, power, num = 1;
for (int i = 1; i <= 100; i++) {
num = 1/num;
num = num + 1;
x = num;
power = pow(x, x);
printf("%f", power);
position += 1;
num = i;
}
}
If you are trying to approximate Euler's number, I don't see why not just try something like:
static const double E = 2.718281828459045;
I have simply corrected syntax errors in your program, but I don't think it will actually get you E. See this page about calculating E in C.
I'm no C master but isnt just calling double by itself a type declaration and not type casting? wouldnt it be power = (double) pow(x, x); if you are type casting? see: https://www.tutorialspoint.com/cprogramming/c_type_casting.htm
I reworked some mistakes in your code and think it should work now; however, the style, which I kept untouched, is confusing.
#include <stdio.h>
#include <math.h>
int main()
{
double power; //stores floating point numbers!
double num = 1;//stores floating point numbers!
int position = 1;
while (position <= 100)
{
num = 1/num;
num = num + 1;
power = pow(num, position); //x not needed, this is what you ment
printf("%f\n", power); //%d outputs decimal numbers, f is for floats
position += 1;
num = position;
}
}
To improve your code, I would suggest to simplify it. Something along the lines of this
#include <stdio.h>
#include <math.h>
int main()
{
double approx;
for(int iter=1; iter<=100; iter++){
approx=pow((1+1./iter),iter);
printf("%f\n", approx);
}
}
is much easier to understand.
I'm trying to use the distance between points formula to calculate the perimeter of any geometric figure, but the function is not delivering the values it should. I have no clue of what I'm doing wrong
#include <math.h>
int perimeter(int flag, dot d[]){
float result, sum1, sum2, sum3, quad1, quad2, op[flag], sum;
for(int c=0;c<flag;c++){
sum1=d[c+1].x-d[c].x;
sum2=d[c+1].y-d[c].y;
quad1=pow(sum1, 2);
quad2=pow(sum2, 2);
sum3=quad1+quad2;
result=sqrt(sum3);
op[c]=result;
}
for(int c=0;c<flag;c++){
sum+=op[c];
}return sum;
}
Test values:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct dot{
float x,y;
}dot;
int main(){
int flag=4;
dot d[flag];
d[0].x=9;
d[0].y=10;
d[1].x=21;
d[1].y=10;
d[2].x=21;
d[2].y=16;
d[3].x=9;
d[4].y=16;
float result, sum1, sum2, sum3, quad1, quad2, op[flag], sum=0.0;
for(int c=0;c<flag;c++){
sum1=d[c+1%flag].x-d[c].x;
sum2=d[c+1%flag].y-d[c].y;
quad1=pow(sum1, 2);
quad2=pow(sum2, 2);
sum3=quad1+quad2;
result=sqrt(sum3);
printf("distance %d: %d\n", c, result);
sum+=result;
}
printf("final result: %d\n", result);
}
Console log(printing steps):
gcc version 4.6.3
distance 0: 0
distance 1: 1
distance 2: 2
distance 3: 3
final result: 26533904
You should look up the hypot() functions declared in the <math.h> header. You could end up with:
float perimeter(int n_dots, dot d[])
{
float sum = 0.0;
for (int i = 0; i < n_dots; i++)
{
int n = (i+1) % n_dots;
sum += hypotf(d[n].x - d[i].x, d[n].y - d[i].y);
}
return sum;
}
If you need to report on the values calculated, you can capture the result of hypotf() before you add it to sum.
Note, too, that you should return a float value. In my book, you should change float to double throughout (and use hypot() in place of hypotf()), but that's somewhat separate as an issue. Returning float (or double) rather than int is rather important, I think.
The MCVE (Minimal, Complete, Verifiable Example) version of your code might be:
#include <math.h>
#include <stdio.h>
typedef struct dot
{
float x, y;
} dot;
float perimeter(int n_dots, dot d[]);
int main(void)
{
enum { num_dots = 4 };
dot d[num_dots] =
{
{ .x = 9, .y = 10 },
{ .x = 21, .y = 10 },
{ .x = 21, .y = 16 },
{ .x = 9, .y = 16 },
};
printf("Perimeter: %.3f\n", perimeter(num_dots, d));
return 0;
}
float perimeter(int n_dots, dot d[])
{
float sum = 0.0;
for (int i = 0; i < n_dots; i++)
{
int n = (i + 1) % n_dots;
sum += hypotf(d[n].x - d[i].x, d[n].y - d[i].y);
}
return sum;
}
Using a VLA prevents you using an initializer for it, so I made the array into a regular fixed size array.
Output:
Perimeter: 36.000
For the given data, this is correct (the four sides are of length 12, 6, 12, 6).
Initialize sum with 0.0.
Then
Replace
op[c]=result;
with
sum+=result;
You don't need the other for loop.
Secondly, make sure the value of the variable flag passed to the perimeter function is 1 less than the size of the dots array d.
Lastly, you need to add the distance between the last point and the first point to the sum. (After the for loop).
sum1 = d[flag+1].x - d[0].x;
sum2 = d[flag+1].y - d[0].y;
quad1=pow(sum1, 2);
quad2=pow(sum2, 2);
sum3=quad1+quad2;
result=sqrt(sum3);
sum+=result;
Here is my version, with better variable names,
#include <math.h>
float perimeter(int flag, dot d[]){
float sum_x, sum_y, quad_x, quad_y, sum = 0;
for(int c=0;c<flag;c++){
sum_x=d[(c+1) % flag].x-d[c].x;
sum_y=d[(c+1) % flag].y-d[c].y;
quad_x=pow(sum_x, 2);
quad_y=pow(sum_y, 2);
sum += sqrt(quad_x + quad_y);
}
return sum;
}
Quick question:
#include <stdio.h>
int main(void) {
int divisor, counter, binary, counter2;
int digit0, digit1, digit2, digit3;
float decimal;
printf("Decimal\t\tBinary\n");
for (counter = 0; counter <= 15; counter++) {
printf("%d\t\n", counter);
decimal = counter;
for (counter2 = 0; counter2 <= 3; counter2++) {
decimal % 2 == 1 ? digit0 = 1 : digit0 = 0);
}
}
return 0;
}
I keep getting the error that the "expression must be a modifiable value" on variable name "decimal" in the second for loop.
Why is this, and how can I fix it?
Thank you!
Because decimal is float,but % only for integers.If you really want to mod by using float, you can use function float fmod(float x, float y), it calculates x%y, and you should include #include <math.h> to use it.
Ive created a program to compute the value of an integral from zero to infinity, however when i go to print my answer it only prints it to 6 decimal places, could someone advise on why this is?
Thanks very much in advance :)
#include <stdio.h>
#include <math.h>
#include <float.h>
double integral_Function(double x){
double value;
value = 1/((1+ pow(x, 2))*(pow(x, 2/3)));
return value;
}
double trapezium_Rule(double lower, double upper, int n){
double h;
double total;
h = (upper - lower)/n;
total = (h/2) * (integral_Function(upper) + integral_Function(lower));
for(int i = 1; i < n; i++){
total = total + (h * integral_Function(lower + (i * h)));
}
return total;
}
int main() {
double sum = 0;
double upper = DBL_MIN * 1.001;
double lower = DBL_MIN;
while (upper <= DBL_MAX){
sum += trapezium_Rule(lower, upper, 5000) * 2;
lower = upper;
upper = upper * 1.02;
}
printf("%f", sum);
return 0;
}
This is because you use string format %f which is by default only print 6 digit after comma. To increase it, simply specify the length that you want:
printf("%.9f", sum); //note the 9 - it is the length.