i did what i understand it .Am i wrong ?akol ompen wrote:Hello,i know a little bit java but i dont know if java helps me on the language c.I did what i think is right as i see is simple .Anyway i dont know if it is right or i have some faults.Please check it .My english to explain isnt so good but i will try my best
Exercise : Is given this y=f(x)=1/x^2+1.The distance [a,b] and one step h,this makes division [a,b] in equals parts with some points [a,a+h, a+2h, … , b-h, b].It should be a program to find and appear(printf) the x and y.Using the while.a=0,b=1,h=0.1
what i did but idk if is right
Campbell Ritchie wrote:The four languages C (not c), C++, Java®, and C# look similar, but they are all different from one another; the closest similarity is between Java® and C#. So, no, knowing one language is little help towards learning another.
Your code is poorly formatted and indented. The days when people liked to cram as much C code into as little space as possible are long gone. Put spaces round your operators and indentation. Remember you need to be able to read your code. Separate out your formula into another function which returns the value.
You are not calculating the formula correctly. You are using / x * x where the / and * operators have the same precedence. In the formula x² has a higher precedence than /, which you are not reflecting in your formula.
You are also using integer arithmetic rather than floating‑point arithmetic in line 13.
You will have problems with line 14 because you are trying to add incompatible types of number.
Give your main function a return type (probably void if you pass no arguments).
I think that will work all right.akol ompen wrote:. . .
void main() // is this the way you mean ? not to return a value?
Nonononononono. Don't use floats. Use doubles.. . .
float x; //i change them to float
Only slightly. The x² bit is better, but you are still using integer arithmetic. Also that bit of code should be refactored into a separate function. You have changed the formula from 1 ÷ x² + 1 to 1 ÷ (x² + 1), which is different from what you posted first. Which is correct?. . .
y=1/((x*x)+1); // is this better?
But x is an integer and h is floating‑point. Don't try to mix the two kinds of number in arithmetic.
x=x+h;
. . .
this is right 1 ÷ (x² + 1).You asking me "In lines 15‑16, why are you using &x/&y rather than x/y? " .I read theory (i dont know if is right or wrong ,that what i understand ) i see it that in c in that way they doing it.In java for example it would be system.out.println("y= "+y); and for x the same system.out.println("x= "+x); .akol ompen wrote:
i did what i understand it .Am i wrong ?akol ompen wrote:Hello,i know a little bit java but i dont know if java helps me on the language c.I did what i think is right as i see is simple .Anyway i dont know if it is right or i have some faults.Please check it .My english to explain isnt so good but i will try my best
Exercise : Is given this y=f(x)=1/x^2+1.The distance [a,b] and one step h,this makes division [a,b] in equals parts with some points [a,a+h, a+2h, … , b-h, b].It should be a program to find and appear(printf) the x and y.Using the while.a=0,b=1,h=0.1
what i did but idk if is right
akol ompen wrote:You asking me "In lines 15‑16, why are you using &x/&y rather than x/y? " .I read theory (i dont know if is right or wrong ,that what i understand ) i see it that in c in that way they doing it.
I think you have misunderstood something; as Henry says, &x prints the memory location. In Java® I would prefer to writeakol ompen wrote:. . . .You asking me "In lines 15‑16, why are you using &x/&y rather than x/y? " .I read theory (i dont know if is right or wrong ,that what i understand ) i see it that in c in that way they doing it.In java for example it would be system.out.println("y= "+y); and for x the same system.out.println("x= "+x); .
Yes Try the forum called JDBC.... . .And here on this site there is a forum about sql?
thanks a lotCampbell Ritchie wrote:
I think you have misunderstood something; as Henry says, &x prints the memory location. In Java® I would prefer to writeakol ompen wrote:. . . .You asking me "In lines 15‑16, why are you using &x/&y rather than x/y? " .I read theory (i dont know if is right or wrong ,that what i understand ) i see it that in c in that way they doing it.In java for example it would be system.out.println("y= "+y); and for x the same system.out.println("x= "+x); .
System.out.printf("x = %3.1f. y = %8.6f%n", x, y);
since they are floating‑point numbers.Yes Try the forum called JDBC.... . .And here on this site there is a forum about sql?
I shall leave you to work out why you get 71 lines of output rather than 70.campbell@campbellsComputer:~/CPrograms$ gcc -o Formula formula.c
campbell@campbellsComputer:~/CPrograms$ ./Formula
x = 1.0. y = 0.500000
x = 1.1. y = 0.452489
x = 1.2. y = 0.409836
x = 1.3. y = 0.371747
x = 1.4. y = 0.337838
x = 1.5. y = 0.307692
x = 1.6. y = 0.280899
x = 1.7. y = 0.257069
x = 1.8. y = 0.235849
x = 1.9. y = 0.216920
x = 2.0. y = 0.200000
x = 2.1. y = 0.184843
x = 2.2. y = 0.171233
x = 2.3. y = 0.158983
x = 2.4. y = 0.147929
x = 2.5. y = 0.137931
x = 2.6. y = 0.128866
x = 2.7. y = 0.120627
x = 2.8. y = 0.113122
x = 2.9. y = 0.106270
x = 3.0. y = 0.100000
x = 3.1. y = 0.094251
x = 3.2. y = 0.088968
x = 3.3. y = 0.084104
x = 3.4. y = 0.079618
x = 3.5. y = 0.075472
x = 3.6. y = 0.071633
x = 3.7. y = 0.068074
x = 3.8. y = 0.064767
x = 3.9. y = 0.061690
x = 4.0. y = 0.058824
x = 4.1. y = 0.056148
x = 4.2. y = 0.053648
x = 4.3. y = 0.051308
x = 4.4. y = 0.049116
x = 4.5. y = 0.047059
x = 4.6. y = 0.045126
x = 4.7. y = 0.043309
x = 4.8. y = 0.041597
x = 4.9. y = 0.039984
x = 5.0. y = 0.038462
x = 5.1. y = 0.037023
x = 5.2. y = 0.035663
x = 5.3. y = 0.034376
x = 5.4. y = 0.033156
x = 5.5. y = 0.032000
x = 5.6. y = 0.030902
x = 5.7. y = 0.029860
x = 5.8. y = 0.028868
x = 5.9. y = 0.027925
x = 6.0. y = 0.027027
x = 6.1. y = 0.026171
x = 6.2. y = 0.025355
x = 6.3. y = 0.024576
x = 6.4. y = 0.023832
x = 6.5. y = 0.023121
x = 6.6. y = 0.022442
x = 6.7. y = 0.021791
x = 6.8. y = 0.021169
x = 6.9. y = 0.020572
x = 7.0. y = 0.020000
x = 7.1. y = 0.019451
x = 7.2. y = 0.018925
x = 7.3. y = 0.018420
x = 7.4. y = 0.017934
x = 7.5. y = 0.017467
x = 7.6. y = 0.017018
x = 7.7. y = 0.016586
x = 7.8. y = 0.016171
x = 7.9. y = 0.015770
x = 8.0. y = 0.015385
See where your hand is? Not there. It's next to this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|