I have the following code in C to make some arithmatic calculation
#include <stdio.h>
#include <stdlib.h>
int main()
{
float x,y;
float z;
printf("Enter x y z \n");
scanf("%f %f %f ", &x, &y , &z);
z = ((4.2 (x+y)))/ (z - (0.25*z))/ (y+z)/ ((x+y) * (x+y));
printf("\n z = %f", z);
return 0;
}
when i build the program , i get the following error message in the following code line
z = ((4.2 (x+y)))/ (z - (0.25*z))/ (y+z)/ ((x+y) * (x+y));
called object is not a function or dunction pointer
That's a typo, you're missing an operator:
z = ((4.2 * (x + y))) / (z - (0.25*z)) / (y + z) / ((x + y) * (x + y));
^
whatever the operator is
C has no support for mathematics-implicit multiplication operator (more or less as you would write in an equation in school). E.g.
// y = 2x
int y;
y = 2 * x;
Related
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
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?
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.
I was coding up a short program to compute right endpoints for a given graph (not shown here), so to avoid tedious computation, I decided to write up a short program to do the work for me. Yet my C program just prints out nan. I am very rusty on C, but I am not sure why I am getting NaN.
#include <stdio.h>
int main(void) {
int x;
float y, z;
for (x = 0; x <= 8; x++) {
y += 10.0 - (12.0 + (float)x) / 4.0;
printf("%f\n", y);
}
z = 0.5 * y;
printf("%f\n", z);
return 0;
}
y = 10.0 - (12.0 + (float)x) / 4.0;
Followed by
y = y+1;
This makes sense else you have y uninitialized which leads to undefined behavior because the value of y is undeterminate.
During declaration you can initialize y and use += operator.
Like
float y = 0;
So I have to make this formula "y = y / (3/17) - z + x / (a % 2) + PI" in C
I am having a problem with (a%2) as it is returning odd values. ie 1%2 = 0.000001
int assignment7()
{
#define PI 3.14
int a=0,amod2;
double Ny=0,y=0,z=0,x=0;
printf("Enter values for x,y,z and a: ");
scanf("%d%lf%lf%lf",&a,&y,&z,&x);
//printf("%d,%lf,%lf,%lf\n",a,y,z,x);
//amod2=1%2;
//printf("%lf",amod2);
Ny=y/(double)(3/17) - z+x / amod2 + PI;
printf("%lf\n",Ny);
When you say:
printf("%lf",amod2);
the compiler expects amod2 to be a "long float" (aka a double), but you defined it as:
int amod2;
Also your prompt says "x,y,z and a" but you read in the order "a,y,z,x":
printf("Enter values for x,y,z and a: ");
scanf("%d%lf%lf%lf",&a,&y,&z,&x);
that's awkward at best.
EDIT: cleaned up a bit and made some assumptions about order of operations:
#include <stdio.h>
#define PI 3.14
#define DIVSOR (3.0/17.0)
int assignment7 ( void );
int assignment7 ( void ) {
double x = 0.0;
double y = 0.0;
double z = 0.0;
int a = 0;
int amod2;
double Ny;
printf("Enter values for x,y,z and a: ");
scanf("%lf%lf%lf%d",&x,&y,&z,&a);
amod2 = a % 2;
Ny = (y / DIVSOR) - z + (x / amod2) + PI;
printf("%lf\n", Ny);
return 0;
}
int main ( void ) { return assignment7(); }
You don't say what inputs you are giving it, (a test case with inputs and the expected results would be super helpful), but I can point out that x / (a % 2) is going to be infinity when a is 2 or 4 or 6 or ...