Who speaks C?, printf("I'm stuck/n"); |
|
Porsche, and the Porsche crest are registered trademarks of Dr. Ing. h.c. F. Porsche AG.
This site is not affiliated with Porsche in any way. Its only purpose is to provide an online forum for car enthusiasts. All other trademarks are property of their respective owners. |
|
Who speaks C?, printf("I'm stuck/n"); |
MattR |
Jan 22 2006, 10:48 PM
Post
#1
|
Advanced Member Group: Members Posts: 3,279 Joined: 23-January 04 From: SF Bay Area Member No.: 1,589 Region Association: Northern California |
I'm trying to finish up some homework. Its not last minute (due tomorrow night), but I'm stuck. Britt and I have been trying to debug for a little while but with no luck.
The problem: I'm trying to solve the integral 2*sqrt(1-x*x) from -1 to 1 using the trapezoidal rule. Basically I have to solve the equation: h[f(x1) + f(x2) + ... + f(x n-1)] where x1, x2, etc. is the function evaluated at the point x1, x2, etc. along the x axis. the distance between x1 and x2 is a-b/n, which is the distance of the interval. I will need to input the number of intervals and be given an approximation for the function. Here is my code; #include #include int main(void) { int n; int f=0; int J; int x=-1; printf("Please enter the number of subdivisions n:"); scanf("%d",&n); While (x<=1) { x = x+2/n; f = f+2*sqrt(1-x*x); } J=2/n*f; printf("The approximate integration by Trapezoidal Rule is: %d \n",J); return 0; } Does anybody see anything thats glaring? I'm getting an error in line 23 (after the While (x<=1) that wont stop... |
MattR |
Jan 22 2006, 11:03 PM
Post
#2
|
Advanced Member Group: Members Posts: 3,279 Joined: 23-January 04 From: SF Bay Area Member No.: 1,589 Region Association: Northern California |
error codes:
trapezoidal4.c: In function `main': trapezoidal4.c:23: error: parse error before '{' token trapezoidal4.c: At top level: trapezoidal4.c:28: error: `n' undeclared here (not in a function) trapezoidal4.c:28: error: `f' undeclared here (not in a function) trapezoidal4.c:28: warning: data definition has no type or storage class trapezoidal4.c:30: error: parse error before string constant trapezoidal4.c:30: warning: conflicting types for built-in function `printf' trapezoidal4.c:30: warning: data definition has no type or storage class |
john rogers |
Jan 22 2006, 11:04 PM
Post
#3
|
Senior Member Group: Members Posts: 1,525 Joined: 4-March 03 From: Chula Vista CA Member No.: 391 |
Well, you did not say which compiler you are using and just by looking at the code when declaring an "int" that usually means an unsigned integer which has a range of 0 to 32767, so the negative 1 won't work. It has been a while since I did any integrals in c++ but here is a reference page that might help?
http://okmij.org/ftp/packages.html |
MattR |
Jan 22 2006, 11:11 PM
Post
#4
|
Advanced Member Group: Members Posts: 3,279 Joined: 23-January 04 From: SF Bay Area Member No.: 1,589 Region Association: Northern California |
Yeah, I'm using GCC compiler... sorry, i shoulda included that.
|
bd1308 |
Jan 22 2006, 11:16 PM
Post
#5
|
Sir Post-a-lot Group: Members Posts: 8,020 Joined: 24-January 05 From: Louisville,KY Member No.: 3,501 |
ssshhhhheeeeeeeeeeett..
i forgot about that. random positive integers are assigned to rand numbers, and "int" assigned vars. b |
McMark |
Jan 22 2006, 11:17 PM
Post
#6
|
914 Freak! Group: Retired Admin Posts: 20,179 Joined: 13-March 03 From: Grand Rapids, MI Member No.: 419 Region Association: None |
|
MattR |
Jan 22 2006, 11:19 PM
Post
#7
|
||
Advanced Member Group: Members Posts: 3,279 Joined: 23-January 04 From: SF Bay Area Member No.: 1,589 Region Association: Northern California |
omg........ that did it (IMG:http://www.914world.com/bbs2/html/emoticons/ar15.gif) (IMG:http://www.914world.com/bbs2/html/emoticons/ar15.gif) (IMG:http://www.914world.com/bbs2/html/emoticons/ar15.gif) i also changed int to float |
||
MattR |
Jan 22 2006, 11:21 PM
Post
#8
|
Advanced Member Group: Members Posts: 3,279 Joined: 23-January 04 From: SF Bay Area Member No.: 1,589 Region Association: Northern California |
Okay, now I dont get any errors in GCC
but when I run the program, I enter the number of subdivisions and the result I get back is "NaN" The program looks like this: Please enter the number of subdivisions n:1000 The approximate integration by Trapezoidal Rule is: NaN Which really confuses me. The result should be J (which is 2/n * f). What is "NaN"? |
john rogers |
Jan 22 2006, 11:24 PM
Post
#9
|
Senior Member Group: Members Posts: 1,525 Joined: 4-March 03 From: Chula Vista CA Member No.: 391 |
Check your output formatting.
|
MattR |
Jan 22 2006, 11:26 PM
Post
#10
|
Advanced Member Group: Members Posts: 3,279 Joined: 23-January 04 From: SF Bay Area Member No.: 1,589 Region Association: Northern California |
I'm getting the error in the "sqrt" function.
I guess I'll just have to play with it some more... thanks guys! |
r_towle |
Jan 22 2006, 11:29 PM
Post
#11
|
Custom Member Group: Members Posts: 24,574 Joined: 9-January 03 From: Taxachusetts Member No.: 124 Region Association: North East States |
out of visual studio
While (x<=1); made me add a semi colon |
bryanc |
Jan 22 2006, 11:36 PM
Post
#12
|
Member Group: Members Posts: 321 Joined: 9-August 04 From: San Antonio, Tx Member No.: 2,495 Region Association: Southwest Region |
For this problem, all of the variables should be either double or float. for example, x will be -1, 0, 1.
Oh yeah, and change the %d in the printf to %f. |
McMark |
Jan 23 2006, 12:11 AM
Post
#13
|
914 Freak! Group: Retired Admin Posts: 20,179 Joined: 13-March 03 From: Grand Rapids, MI Member No.: 419 Region Association: None |
I bet NaN is "not a number". (IMG:http://www.914world.com/bbs2/html/emoticons/wink.gif)
|
MattR |
Jan 23 2006, 12:14 AM
Post
#14
|
||
Advanced Member Group: Members Posts: 3,279 Joined: 23-January 04 From: SF Bay Area Member No.: 1,589 Region Association: Northern California |
Yeah. The problem is with that sqrt function. I dont know how it works.. the program works fine when I take out the squareroot thing. I'll ask a TA tomorrow or something... I'm tired of messing with it tonight. Time to study circuits... damn I hate electrical engineering classes (IMG:http://www.914world.com/bbs2/html/emoticons/sad.gif) |
||
Hammy |
Jan 23 2006, 01:11 AM
Post
#15
|
mr. Wonderful Group: Members Posts: 1,826 Joined: 20-October 04 From: Columbia, California Member No.: 2,978 Region Association: Northern California |
|
Dave_Darling |
Jan 23 2006, 02:06 AM
Post
#16
|
914 Idiot Group: Members Posts: 14,982 Joined: 9-January 03 From: Silicon Valley / Kailua-Kona Member No.: 121 Region Association: Northern California |
Like they said, check your argument types and return types.... Those are the most common errors with using library functions. Did you remember to include math.h or wherever the sqrt function comes from?
--DD |
reverie |
Jan 23 2006, 05:23 AM
Post
#17
|
Senior Member Group: Members Posts: 783 Joined: 14-March 03 Member No.: 427 Region Association: None |
Welcome to the club! (IMG:http://www.914world.com/bbs2/html/emoticons/wavey.gif)
|
jasons |
Jan 23 2006, 07:55 AM
Post
#18
|
||
Jackstand Extraordinaire Group: Members Posts: 2,002 Joined: 19-August 04 From: Scottsdale, AZ Member No.: 2,573 Region Association: None |
(IMG:http://www.914world.com/bbs2/html/emoticons/agree.gif) You are using int types and division. When you divide int's that should result in fractions, you will truncate the fraction. sqrt probably will not work well with int's either. Its doing some kind of series division itself. Use floats or doubles. Example: 3/5 will result in 0 5/3 will in 1 Also, are you sure about the precedence of this operation... J=2/n*f; ? / and * have the same precedence so your they will operate left to right. Your result is this J=(2/n)*f; If you want this J=2/(n*f); You need to use ()'s |
||
bd1308 |
Jan 23 2006, 07:59 AM
Post
#19
|
Sir Post-a-lot Group: Members Posts: 8,020 Joined: 24-January 05 From: Louisville,KY Member No.: 3,501 |
doh!
that was the first thing that popped into my mind when i looked at your program this morning. W sorry buddy, I wished I could have helped more, but I was tyring to read this Edgar Allan Poe story.... (IMG:http://www.914world.com/bbs2/html/emoticons/yawn.gif) dang it...oh well... i'm glad you got most of the errors down. one thing: you needed to watch out for the conflicting numerical output types. I believe you got the sqrt() function down correctly, but something doesnt like being square-rooted. anyway...if you need more help, lemme knwo. b |
Root_Werks |
Jan 23 2006, 10:44 AM
Post
#20
|
Village Idiot Group: Members Posts: 8,317 Joined: 25-May 04 From: About 5NM from Canada Member No.: 2,105 Region Association: Pacific Northwest |
(IMG:http://www.914world.com/bbs2/html/emoticons/ohmy.gif) Wow, it has been a few years for me since I have even looked at a printf statement.
|
Lo-Fi Version | Time is now: 9th May 2024 - 11:17 AM |
All rights reserved 914World.com © since 2002 |
914World.com is the fastest growing online 914 community! We have it all, classifieds, events, forums, vendors, parts, autocross, racing, technical articles, events calendar, newsletter, restoration, gallery, archives, history and more for your Porsche 914 ... |