• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exercise begginer in c

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
akol ompen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

i did what i understand it .Am i wrong ?
 
akol ompen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 cant find how can i edit my posts.Here on this site can i ask about sql?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

akol ompen wrote:. . .
void main() // is this the way you mean ? not to return a value?

I think that will work all right.

. . .
float x; //i change them to float

Nonononononono. Don't use floats. Use doubles.

. . .
y=1/((x*x)+1); // is this better?

Only slightly. The x² bit is better, but you are still using integer arithmetic. Also that bit of code shou‍ld 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?


x=x+h;
. . .

But x is an integer and h is floating‑point. Don't try to mix the two kinds of number in arithmetic.

In lines 15‑16, why are you using &x/&y rather than x/y?
 
akol ompen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

akol ompen wrote:

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

i did what i understand it .Am i wrong ?

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); .
external question : sql (i have no idea about it ) .Can i work online with a program like ibm db2?And here on this site there is a forum about sql?

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



In terms of right or wrong, it is about what you want done, as both expressions work. You call the printf() function to print out a decimal value to be passed in. And you passed in a pointer to the x/y variables. So... basically, you printed out the memory address locations of where the the x/y variables are located. Is that what you want done?

Henry
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.In java for example it would be system.out.println("y= "+y); and for x the same system.out.println("x= "+x); .

I think you have misunderstood something; as Henry says, &x prints the memory location. In Java® I would prefer to write
System.out.printf("x = %3.1f. y = %8.6f%n", x, y);
since they are floating‑point numbers.

. . .And here on this site there is a forum about sql?

Yes Try the forum called JDBC...
 
akol ompen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

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.In java for example it would be system.out.println("y= "+y); and for x the same system.out.println("x= "+x); .

I think you have misunderstood something; as Henry says, &x prints the memory location. In Java® I would prefer to write
System.out.printf("x = %3.1f. y = %8.6f%n", x, y);
since they are floating‑point numbers.

. . .And here on this site there is a forum about sql?

Yes Try the forum called JDBC...

thanks a lot
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My pleasure
Please show us the working version of your code.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now that a week has gone by, I have written my working version:-

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

I shall leave you to work out why you get 71 lines of output rather than 70.

[Edit]I discovered I can paste 1 ÷ (x² + 1) into line 33 and get it to print
“x = 7.0. 1 ÷ (x² + 1) = 0.020000” etc.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic