Values inserted to my parameter suddenly vanished after the first expression - c

I have problems for my function, since after the first expression, the values inserted to my parameters (float x, float y) suddenly becomes 0.
I called this function with main (), the output for c = x/y works as expected. But for d = x - (y * c), it gives me an output of 0, I check where the problem is and it appears to be because of x and y since they both have 0 values for some reason
I have not finished my function for greatest common divisor, since I'm still stuck at this problem
'''
int gcd (float x, float y)
{
int c = x / y;
printf ("This's your quotient: %d\n", c);
int d = x - ( y * c ); // d = 0 since y, x = 0
printf ("This's your remainder: %d\n", d);
printf ("c %d\n",c); // works normally
printf ("y %f\n",y); // y = 0
printf ("x: %f\n",x); // x = 0
}

Here is example code that fixes the issues I noticed and changing the signature to separate calculations from output:
#include <stdio.h>
void quotient_reminder(unsigned x, unsigned y, unsigned *q, unsigned *r) {
if(y == 0) return;
*q = x / y;
*r = x - (y * *q); // d = 0 since y, x = 0
}
int main() {
unsigned q, r;
quotient_reminder(25.0, 7.1, &q, &r);
printf ("This's your quotient: %u\n", q);
printf ("This's your remainder: %u\n", r);
}
and it will return:
This's your quotient: 3
This's your remainder: 4

Related

Why there is such big results of value?

I faced with a problem when solving a task from university.
The task is to do the algorithm of calculating and the program on C for determinating function's value, which is:
F = (E_1 - E_2 + E_3) / (E_1 * E_3)
where
E_1 = sin(x + 3), E_2 = tg(a), E_3 = a^x + b
whereas x, a, b - the users input.
The problem is in namely output! The numbers of there very big and inappropriate! The code below:
int main()
{
int x, a, b;
printf("Put the x value: ");
x = scanf("%i", &x); //x = 1
printf("Put the a value: ");
a = scanf("%i", &a); //a = 1
printf("Put the b value: ");
b = scanf("%i", &b); //b = 1
double E_1 = sin(x + 3);
double E_2 = tan(a);
double E_3 = pow(a, x) + b;
double F = (E_1 - E_2 + E_3) / (E_1 * E_3);
printf("Then, the F = %i", &F);
return 0;
}
And, for example, if x, a, b = 1, the output is 5502604, which is false!
There are at least a couple of problems in the posted code.
int x;
printf("Put the x value: ");
x = scanf("%i", &x); //x = 1
scanf returns the number of times a value is correctly assigned to one of its argument pointers or EOF if end-of-file (or an input error) occurs before any values are stored. So, this line and the others will always assign 1 (or 0, or EOF, which is usually -1, if an error occurs) to the variables, whatever values the user entered.
It's unclear why the variables x, a and b are declared as ints rather than doubles.
double F = /* ... */;
printf("Then, the F = %i", &F);
// ^^ ^^
Here the wrong format (%i is used for integers, a double requires %f or %lf) specifier it's used and the wrong value is passed (the address of F instead of F).

In C, I am having trouble tracing a code and not sure how the value is being computed?

I am tracing a code but I don't understand how the values are being calculated. My question is on the comments beside the code.
I am tracing code and I understand all the parts except 2. I posted those 2 part below. I will be really glad if someone could help me.
#include <stdio.h>
int function1(int *m, int n)
{
*m = *m + n;
return(*m);
}
int function2(int n, int *m)
{
n = *m + 2;
return(n);
}
int main()
{
int x = 1, y = 3;
int *xptr = &x,*yptr = &y;
x = 1; y = 3;
y = function1(xptr,x);
printf("x = %d, Y = %d\n",x,y);//x=2 but why? shouldn't it be x=1? y=2
x = 1; y = 3;
x = function1(yptr,function2(2,yptr));
printf("x = %d, y = %d\n",x,y);//x=8 y=8 but why? shouldn't y=3?
return 0;
}
So, inside function1:
int function1(int *m, int n) {
*m = *m + n;
return(*m);
} /*
... */
y = function1(xptr,x);
n holds a copy of the value of x,
m holds a copy of the value of xptr, which is the address of x.
*m refers to the contents of the address held by m
...*m = *m + n
That's 'assign to the contents of the address of x: the contents of the address of x, plus the value of x'.
The contents of the address of x (aka it's value) is 1. So we assign 1 + 1 to the contents of address holding the value of x.
In the first printf, x == 2 because when calling function1 the first time *m is a pointer to x, so you assign to the location pointed by m (that is the location of x), *m + n == x + x == 1 + 1 == 2.
In the second printf, y == 8 because in function2 you return its value incremented by 2 (so 3 + 2 == 5) but without updating y, so in function1 you sum the resulting value of function2 to the old value of y (3 + 5 == 8). function1 return also that value, so also x == 8.

Write c code to evaluate the integral between 0 and y of (x^2)(e^-x^2)dx using Simpson's rule (use a fixed number of 20,000 steps)

Second part of Q: Then solve the integral between 0 and y of (x^2)(e^(-x^2))dx=0.1 for y using bracketing and bisection.
Here's what I have done so far:
#include <stdio.h>
#include <math.h>
double f(double x, double y);
int main(void) {
int i, steps;
double a, b, y, h, m, lower, upper, x, simp, val;
/*
* Integrate (x^2)(e^(-x^2)) from 0 to y
*/
steps = 20000;
a = 0;
b = y;
h= (b-a)/steps;
/*
* now apply Simpson's rule. Note that the steps should be even.
*/
simp = -f(a, y);
x = a;
for (i =0; i < steps; i += 2) {
simp += 2.0*f(x,y)+4.0*f(x+h, y);
x += 2*h;
}
simp += f(b, y);
simp *= h/3.0;
/*
* print out the answer
*/
printf("The integral from 0 to y with respect to x by Simpson's Rule is %f\n", simp);
/*
* Now we need to bracket and bisect to find y
*/
lower = 0;
/*
* Lower bound is from turning point
*/
upper = 100;
/*
*Upper value given.
*/
while (upper - lower > 10E-10){
m = (lower + upper)/2;
val = f(m, y);
if (val >=0)
upper = m;
if (val <=0)
lower = m;
}
m = (lower + upper)/2;
printf("The value for y is: %lf\n", m);
return 0;
}
double f(double x, double y) {
return pow(x,2)*exp(pow(-x,2))-0.1;
}
Output: The integral from 0 to y with respect to x by Simpson's Rule is -0.000000
The value for y is: 0.302120
It runs but doesn't do exactly what I need it to do. I need to be able to continue working with the integral once I've used 0 and y as the limits. I can't do this. Then continue on and solve for y. It gives me a value for y but is not the same one I get if i solve using online calculators. Also, the output gave zero for the integral even when I changed the equation to be integrated to x^2. Can anyone help explain in as simple terms as possible?

Number doesn't print in expected way

This is my program on 'Sin Calculator'
#include <stdio.h>
#include <math.h>
#define PI 3.1416
void fraction(double x, int y);
void print(double sx, int x, int y, int s);
void scal();
int main(){
scal();
return 0;
}
void fraction(double x, int y){
int b = 100, a;
a = x * 100;
while ((a % 2 != 0) || (a % 5 != 0)){
if (a % 2 == 0){
a /= 2;
b /= 2;
}
else if (a % 5 == 0){
a /= 5;
b /= 5;
}
}
print(x, a, b, y);
}
void print(double sx, int x, int y, int s){
printf (" Sin(%d) = %d/%d || %.2lf\n",s,x,y,sx);
scal();
}
void scal(){
double sine, sinx;
int x, a, b;
printf ("\n Sin(X) : ");
scanf ("%d",&x);
sinx = x * (PI / 180);
sine = sin(sinx);
fraction(sine, x);
}
I don't get any errors. But when i run it, though the variable 'a' of fraction function can be divided by 5 or 2, it doesn't do it. As a result I get the whole value of 'a'. For example, the value of Sin30 degrees is 0.50, so multiplying it with 100 makes 'a' 50. 50 is dividable by 2 and 5. But in fraction function, it seems that 'a' doesn't get divided there. As a result in 'print' function, i get '50/100' instead of '1/2'. Why is this happening? And also when i enter somethin like Sin23, the program doesn't finish. It stops. What's the problem?
while ((a % 2 != 0) || (a % 5 != 0))
this test fails for the value of 50 or any integer that is multiple of both 2 an 5. Therefore the while loop will not be entered and no division will occur, so a conserves its value.
maybe your intent was the inverse of the condition:
while ((a % 2 == 0) || (a % 5 == 0))

understand c logic printf & valuables

I am trying to understand the logic behind the printf numbers in the code below. Can someone explain how the valuables change? What symbol is executed first (|| , &&, != and so on)? In which direction is it executed?
#include <stdio.h>
static float w = 3.9;
short
f (short a, short *b)
{
a++;
(*b)++;
w++;
printf ("13: %d %d %4.2f\n", a, *b, w);
return a - *b;
}
int
main ()
{
short x, y, z, arr[] = { 4, 8, 0, 6 }, *p = arr;
char m[] = "dcrfvtgb", *pc;
y = 2;
z = 3;
x = (++y != z);
printf ("1: %d %d %d\n", x, y, z);
x = y = 6;
x *= (y = 5);
printf ("2: %d %d\n", x, y);
x = 0;
y = 2;
z = 7;
printf ("3: %d %d %d %d\n", !x || y && !z, x, y, z);
x = 5;
y = 3;
printf ("4: %d %d %d\n", x, y, x < y ? x : y);
y = 0;
if (y)
x = 7;
else
x = 5;
printf ("5: %d %d\n", x, y);
y = 8;
if (z = (y++ > 8))
x = 9;
else
x = 0;
printf ("6: %d %d %d\n", x, y, z);
x = y = 5;
while (y++ < 5)
x += y;
printf ("7: %d %d\n", x, y);
for (x = y = 5; y < 7; x = y++)
printf ("8:%d %d\n", x, y);
for (x = 2, y = 5; y >= 1; x++, y /= 3);
printf ("9: %d %d\n", x, y);
printf ("10: %d %d\n", p[2], *(arr + 1));
x = y = 3;
z = f (x, &y);
printf ("11:%d %d %d %4.2f\n", x, y, z, w);
for (x = y = z = 10; z-- > 9; x *= y);
printf ("12: %d %d %d\n", x, y, z);
for (pc = m; *pc; pc++)
(*pc)--;
printf ("14: %s %c\n", m + 2, *(m + 1));
return 0;
}
#abligh says "The C standard does not specify the order in which the arguments are evaluated."
As far as I know, the Intel implementation pushes the arguments right-to-left onto the stack, which means that the arguments are evaluated right-to-left.
printf ("12: %d %d %d\n", x, z, z++);
In this example the first value of z, when printed, will be one higher than the second value of z when printed.
The printf function takes a format string as the first argument, then zero or more other arguments. Each of the things beginning with % in the format string correspond to one of the other arguments. At least the way you are using it, these are in the same order, so the first % placeholder corresponds to to the second argument (i.e. the first after the format string), the second % placeholder to the next and so forth. The letter following the % (and sometimes other characters) describe the type of argument and how it is to be printed. See the manual page for printf() for more information.
Each of the arguments is evaluated before the call to printf. The C standard does not specify the order in which the arguments are evaluated. So if you have (e.g.), !x || y && !z as an argument, each of those will be evaluated prior to the call to printf(). For the order in which the components of those are evaluated, see operator precedence.
Note that that only tells you what order operators are evaluated in (i.e. in x + z * y, this says the z * y is evaluated before the +). It doesn't tell you whether x, y or z will be evaluated first; as they may be expressions themselves, that's important. So, for instance in:
int
f(int a, int b, int c)
{
return g(a) + h(b) * i(c);
}
in the general case you have no guarantee in which order g(a), h(b), and i(c) are evaluated. You only know the order in which the + and * are evaluated.
If you want to know more about evaluation order, you need to research sequence points.

Resources