Thread: What am I doing wrong? Help please...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    6

    What am I doing wrong? Help please...

    This is written in SSH Secure Shell

    Code:
    /* Homework Two- compute area and volume of sphere */ 
    #include <stdio.h> 
    #include <math.h> 
    #define pi = 3.141593 
    main() 
    { 
    double Area, Radius, Volume, pi; 
    int r; 
    int k; 
    for(r=1; r<=1; r++); 
    for(k=10; k<=20; k++); 
    { 
    Radius=((k)*(.1)); 
    Area=(pi*(Radius)*(Radius)); 
    Volume=((4/3)*pi*(Radius)*(Radius)*(Radius)); 
    printf("\n"); 
    printf("Sphere (%2d)\tRadius = %4.1f\tArea = %7.3f\tVolume = %7.3f",r,Radius,Are 
    a,Volume); 
    } 
    } 
    ~ 
    ~ 
    ~
    I'm getting parse errors, they are as follows...

    :Hw2.c: In function `main':
    Hw2.c:7: parse error before '=' token
    Hw2.c:14: parse error before '=' token
    Hw2.c:14: parse error before ')' token
    Hw2.c:15: parse error before '=' token
    Hw2.c:15: parse error before ')' token

    Any help would be appreciated, thanks
    -Steve

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    it appears as though your for loops aren't properly nested...

    you have:
    Code:
    for(<stuff>);
    for(<more stuff>);
    should probably be:
    Code:
    for(<stuff>) {
        for(<more stuff>) {
            <code>
        }
    }
    i don't really know, that's just what i got at first glance...
    Brett Kelly

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for(r=1; r<=1; r++); 
    for(k=10; k<=20; k++);
    Well, for starters, this one of those things that is legal, and is correct, but is wrong for what you want to do. See the ; at the end of those lines? That in effect means "make this loop do nothing other than count and end at the ;"

    So it's legal C, but it's not what you want. Remove the ; from the end of those two lines.

    [edit] "Curses, foiled again!" [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    I still am getting parse errors after removing the two para's. Only 5 this time though. Check it out....
    Code:
    w2.c:9: parse error before '=' token
    Hw2.c:14: parse error before '=' token
    Hw2.c:14: parse error before ')' token
    Hw2.c:15: parse error before '=' token
    Hw2.c:15: parse error before ')' token
    Thanks for feedback by the way

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    /* replace this
    #define pi = 3.141593 
    with this */
    #define pi 3.141593
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    Only one parse error now....

    Code:
    Hw2.c: In function `main':
    Hw2.c:9: parse error before numeric constant
    Code after Mod's

    Code:
    /* Homework Two- compute area and volume of sphere */ 
    #include <stdio.h> 
    #include <math.h> 
    #define pi 3.141593 
    main() 
    { 
    int r; 
    int k; 
    double A,R,V,pi; 
    for (r=1; r<=11; r++) 
    { 
    for (k=10; k<=20; k++) 
    { 
    R = ((k)*(.1)); 
    A = (pi*(R)*(R)); 
    V = ((4/3)*pi*(R)*(R)*(R)); 
    printf("\n"); 
    printf("Sphere (%2d)\tRadius = %4.1f\tArea = %7.3f\tVolume = %7.3f",r,R,A,V); 
    } 
    } 
    } 
    ~ 
    ~
    Thanks again for feedback...
    -Steve

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    A #define performs text replacement. So your compiler sees line 9 like this.
    Code:
    double A,R,V,3.141593;
    Choose to use pi as a #define or make it a local variable. Don't do both.
    Code:
    double A,R,V;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    Great it worked by changing it as you said Dave but now my output loops way too many times... any suggestions..

    Current Code;

    Code:
    /* Homework Two- compute area and volume of sphere */ 
    #include <stdio.h> 
    #include <math.h> 
    #define pi 3.141593 
    main() 
    { 
    int r; 
    int k; 
    double R,V,A;
    for (r=1; r<=11; r++) 
    { 
    for (k=10; k<=20; k++) 
    { 
    R = ((k)*(.1)); 
    A = (pi*(R)*(R)); 
    V = ((4/3)*pi*(R)*(R)*(R)); 
    printf("\n"); 
    printf("Sphere (%2d)\tRadius = %4.1f\tArea = %7.3f\tVolume = %7.3f",r,R,A,V); 
    } 
    } 
    } 
    ~ 
    ~

    Output is as follows.....


    Code:
    Sphere ( 1)     Radius =  1.0   Area =   3.142  Volume =   3.142 
    Sphere ( 1)     Radius =  1.1   Area =   3.801  Volume =   4.181 
    Sphere ( 1)     Radius =  1.2   Area =   4.524  Volume =   5.429 
    Sphere ( 1)     Radius =  1.3   Area =   5.309  Volume =   6.902 
    Sphere ( 1)     Radius =  1.4   Area =   6.158  Volume =   8.621 
    Sphere ( 1)     Radius =  1.5   Area =   7.069  Volume =  10.603 
    Sphere ( 1)     Radius =  1.6   Area =   8.042  Volume =  12.868 
    Sphere ( 1)     Radius =  1.7   Area =   9.079  Volume =  15.435 
    Sphere ( 1)     Radius =  1.8   Area =  10.179  Volume =  18.322 
    Sphere ( 1)     Radius =  1.9   Area =  11.341  Volume =  21.548 
    Sphere ( 1)     Radius =  2.0   Area =  12.566  Volume =  25.133 
    Sphere ( 2)     Radius =  1.0   Area =   3.142  Volume =   3.142 
    Sphere ( 2)     Radius =  1.1   Area =   3.801  Volume =   4.181 
    Sphere ( 2)     Radius =  1.2   Area =   4.524  Volume =   5.429 
    Sphere ( 2)     Radius =  1.3   Area =   5.309  Volume =   6.902 
    Sphere ( 2)     Radius =  1.4   Area =   6.158  Volume =   8.621 
    Sphere ( 2)     Radius =  1.5   Area =   7.069  Volume =  10.603 
    Sphere ( 2)     Radius =  1.6   Area =   8.042  Volume =  12.868 
    Sphere ( 2)     Radius =  1.7   Area =   9.079  Volume =  15.435 
    Sphere ( 2)     Radius =  1.8   Area =  10.179  Volume =  18.322 
    Sphere ( 2)     Radius =  1.9   Area =  11.341  Volume =  21.548 
    Sphere ( 2)     Radius =  2.0   Area =  12.566  Volume =  25.133 
    Sphere ( 3)     Radius =  1.0   Area =   3.142  Volume =   3.142 
    Sphere ( 3)     Radius =  1.1   Area =   3.801  Volume =   4.181 
    Sphere ( 3)     Radius =  1.2   Area =   4.524  Volume =   5.429 
    Sphere ( 3)     Radius =  1.3   Area =   5.309  Volume =   6.902 
    Sphere ( 3)     Radius =  1.4   Area =   6.158  Volume =   8.621 
    Sphere ( 3)     Radius =  1.5   Area =   7.069  Volume =  10.603 
    Sphere ( 3)     Radius =  1.6   Area =   8.042  Volume =  12.868 
    Sphere ( 3)     Radius =  1.7   Area =   9.079  Volume =  15.435 
    Sphere ( 3)     Radius =  1.8   Area =  10.179  Volume =  18.322 
    Sphere ( 3)     Radius =  1.9   Area =  11.341  Volume =  21.548 
    Sphere ( 3)     Radius =  2.0   Area =  12.566  Volume =  25.133 
    Sphere ( 4)     Radius =  1.0   Area =   3.142  Volume =   3.142 
    Sphere ( 4)     Radius =  1.1   Area =   3.801  Volume =   4.181 
    Sphere ( 4)     Radius =  1.2   Area =   4.524  Volume =   5.429 
    Sphere ( 4)     Radius =  1.3   Area =   5.309  Volume =   6.902 
    Sphere ( 4)     Radius =  1.4   Area =   6.158  Volume =   8.621 
    Sphere ( 4)     Radius =  1.5   Area =   7.069  Volume =  10.603 
    Sphere ( 4)     Radius =  1.6   Area =   8.042  Volume =  12.868 
    Sphere ( 4)     Radius =  1.7   Area =   9.079  Volume =  15.435 
    Sphere ( 4)     Radius =  1.8   Area =  10.179  Volume =  18.322 
    Sphere ( 4)     Radius =  1.9   Area =  11.341  Volume =  21.548 
    Sphere ( 4)     Radius =  2.0   Area =  12.566  Volume =  25.133 
    Sphere ( 5)     Radius =  1.0   Area =   3.142  Volume =   3.142 
    Sphere ( 5)     Radius =  1.1   Area =   3.801  Volume =   4.181 
    Sphere ( 5)     Radius =  1.2   Area =   4.524  Volume =   5.429 
    Sphere ( 5)     Radius =  1.3   Area =   5.309  Volume =   6.902 
    Sphere ( 5)     Radius =  1.4   Area =   6.158  Volume =   8.621 
    Sphere ( 5)     Radius =  1.5   Area =   7.069  Volume =  10.603 
    Sphere ( 5)     Radius =  1.6   Area =   8.042  Volume =  12.868 
    Sphere ( 5)     Radius =  1.7   Area =   9.079  Volume =  15.435 
    Sphere ( 5)     Radius =  1.8   Area =  10.179  Volume =  18.322 
    Sphere ( 5)     Radius =  1.9   Area =  11.341  Volume =  21.548 
    Sphere ( 5)     Radius =  2.0   Area =  12.566  Volume =  25.133 
    Sphere ( 6)     Radius =  1.0   Area =   3.142  Volume =   3.142 
    Sphere ( 6)     Radius =  1.1   Area =   3.801  Volume =   4.181 
    Sphere ( 6)     Radius =  1.2   Area =   4.524  Volume =   5.429 
    Sphere ( 6)     Radius =  1.3   Area =   5.309  Volume =   6.902 
    Sphere ( 6)     Radius =  1.4   Area =   6.158  Volume =   8.621 
    Sphere ( 6)     Radius =  1.5   Area =   7.069  Volume =  10.603 
    Sphere ( 6)     Radius =  1.6   Area =   8.042  Volume =  12.868 
    Sphere ( 6)     Radius =  1.7   Area =   9.079  Volume =  15.435 
    Sphere ( 6)     Radius =  1.8   Area =  10.179  Volume =  18.322 
    Sphere ( 6)     Radius =  1.9   Area =  11.341  Volume =  21.548 
    Sphere ( 6)     Radius =  2.0   Area =  12.566  Volume =  25.133 
    Sphere ( 7)     Radius =  1.0   Area =   3.142  Volume =   3.142 
    Sphere ( 7)     Radius =  1.1   Area =   3.801  Volume =   4.181 
    Sphere ( 7)     Radius =  1.2   Area =   4.524  Volume =   5.429 
    Sphere ( 7)     Radius =  1.3   Area =   5.309  Volume =   6.902 
    Sphere ( 7)     Radius =  1.4   Area =   6.158  Volume =   8.621 
    Sphere ( 7)     Radius =  1.5   Area =   7.069  Volume =  10.603 
    Sphere ( 7)     Radius =  1.6   Area =   8.042  Volume =  12.868 
    Sphere ( 7)     Radius =  1.7   Area =   9.079  Volume =  15.435 
    Sphere ( 7)     Radius =  1.8   Area =  10.179  Volume =  18.322 
    Sphere ( 7)     Radius =  1.9   Area =  11.341  Volume =  21.548 
    Sphere ( 7)     Radius =  2.0   Area =  12.566  Volume =  25.133 
    Sphere ( 8)     Radius =  1.0   Area =   3.142  Volume =   3.142 
    Sphere ( 8)     Radius =  1.1   Area =   3.801  Volume =   4.181 
    Sphere ( 8)     Radius =  1.2   Area =   4.524  Volume =   5.429 
    Sphere ( 8)     Radius =  1.3   Area =   5.309  Volume =   6.902 
    Sphere ( 8)     Radius =  1.4   Area =   6.158  Volume =   8.621 
    Sphere ( 8)     Radius =  1.5   Area =   7.069  Volume =  10.603 
    Sphere ( 8)     Radius =  1.6   Area =   8.042  Volume =  12.868 
    Sphere ( 8)     Radius =  1.7   Area =   9.079  Volume =  15.435 
    Sphere ( 8)     Radius =  1.8   Area =  10.179  Volume =  18.322 
    Sphere ( 8)     Radius =  1.9   Area =  11.341  Volume =  21.548 
    Sphere ( 8)     Radius =  2.0   Area =  12.566  Volume =  25.133 
    Sphere ( 9)     Radius =  1.0   Area =   3.142  Volume =   3.142 
    Sphere ( 9)     Radius =  1.1   Area =   3.801  Volume =   4.181 
    Sphere ( 9)     Radius =  1.2   Area =   4.524  Volume =   5.429 
    Sphere ( 9)     Radius =  1.3   Area =   5.309  Volume =   6.902 
    Sphere ( 9)     Radius =  1.4   Area =   6.158  Volume =   8.621 
    Sphere ( 9)     Radius =  1.5   Area =   7.069  Volume =  10.603 
    Sphere ( 9)     Radius =  1.6   Area =   8.042  Volume =  12.868 
    Sphere ( 9)     Radius =  1.7   Area =   9.079  Volume =  15.435 
    Sphere ( 9)     Radius =  1.8   Area =  10.179  Volume =  18.322 
    Sphere ( 9)     Radius =  1.9   Area =  11.341  Volume =  21.548 
    Sphere ( 9)     Radius =  2.0   Area =  12.566  Volume =  25.133 
    Sphere (10)     Radius =  1.0   Area =   3.142  Volume =   3.142 
    Sphere (10)     Radius =  1.1   Area =   3.801  Volume =   4.181 
    Sphere (10)     Radius =  1.2   Area =   4.524  Volume =   5.429 
    Sphere (10)     Radius =  1.3   Area =   5.309  Volume =   6.902 
    Sphere (10)     Radius =  1.4   Area =   6.158  Volume =   8.621 
    Sphere (10)     Radius =  1.5   Area =   7.069  Volume =  10.603 
    Sphere (10)     Radius =  1.6   Area =   8.042  Volume =  12.868 
    Sphere (10)     Radius =  1.7   Area =   9.079  Volume =  15.435 
    Sphere (10)     Radius =  1.8   Area =  10.179  Volume =  18.322 
    Sphere (10)     Radius =  1.9   Area =  11.341  Volume =  21.548 
    Sphere (10)     Radius =  2.0   Area =  12.566  Volume =  25.133 
    Sphere (11)     Radius =  1.0   Area =   3.142  Volume =   3.142 
    Sphere (11)     Radius =  1.1   Area =   3.801  Volume =   4.181 
    Sphere (11)     Radius =  1.2   Area =   4.524  Volume =   5.429 
    Sphere (11)     Radius =  1.3   Area =   5.309  Volume =   6.902 
    Sphere (11)     Radius =  1.4   Area =   6.158  Volume =   8.621 
    Sphere (11)     Radius =  1.5   Area =   7.069  Volume =  10.603 
    Sphere (11)     Radius =  1.6   Area =   8.042  Volume =  12.868 
    Sphere (11)     Radius =  1.7   Area =   9.079  Volume =  15.435 
    Sphere (11)     Radius =  1.8   Area =  10.179  Volume =  18.322 
    Sphere (11)     Radius =  1.9   Area =  11.341  Volume =  21.548
    problem is its only suppose to go through from 1.0- 2.0 for r and x.xxx for area and so on and stop and show how much it equals ex:Total Area = 324.841 Total Volume = 176.243
    but it keeps looping
    Last edited by SprinterSteve; 04-17-2003 at 04:40 PM.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    This is what I'm suppose to do if it helps any....

    Code:
    Compute the area (A) and the volume (V) of a sphere with 
    radius R=1.0, R=1.1, R=1.2, ... , R=2.0 . Compute also the 
    total area (TA) and the total volume (TV) of all eleven spheres 
    together. 
    
    Define PI by using #define and use a for-loop to repeat 
    calculations for different values of R. (Use PI=3.141593 and 
    recall the elementary formulas A=4*PI*R*R and V=4*PI*R*R*R/3). 
    
    For each sphere, print in one line 
    
    Sphere (k)     Radius = "number"     Area = "number"     Volume = "number" 
    
    At the end, in a separate line, print 
    
    Total Area = "number"    Total Volume = "number" 
    
    Use the output specifier 2d for k (k is a sequential number of the sphere), 
    
    4.1f for R, and 7.3f for A, V, TA, and TV. 
    
    .......................................................................... 
    
    Your output should look like this: 
    
    
    Sphere ( 1)      Radius =  1.0      Area =  12.566      Volume =   4.189 
    Sphere ( 2)      Radius =  1.1      Area =  15.205      Volume =   5.575 
    Sphere ( 3)      Radius =  1.2      Area =  18.096      Volume =   7.238 
    Sphere ( 4)      Radius =  1.3      Area =  21.237      Volume =   9.203 
    Sphere ( 5)      Radius =  1.4      Area =  24.630      Volume =  11.494 
    Sphere ( 6)      Radius =  1.5      Area =  28.274      Volume =  14.137 
    Sphere ( 7)      Radius =  1.6      Area =  32.170      Volume =  17.157 
    Sphere ( 8)      Radius =  1.7      Area =  36.317      Volume =  20.580 
    Sphere ( 9)      Radius =  1.8      Area =  40.715      Volume =  24.429 
    Sphere (10)      Radius =  1.9      Area =  45.365      Volume =  28.731 
    Sphere (11)      Radius =  2.0      Area =  50.265      Volume =  33.510 
    
    
    Total Area = 324.841     Total Volume = 176.243

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >but now my output loops way too many times

    You have two loops, you only need one. One that goes from R = 1.0 to R = 2.0 in steps of 0.1. Floating point equality is tricky, so I'd use something like this.
    Code:
    for ( R = 1.0; R < 2.1; R += 0.1 )
    Then don't calculate R. And use the formula given for calculating A. Increment r each time through the loop.

    Declare totals TA and TV and initialize them to 0.0. After you've calculated the current A and V, add these values to their totals. Print the totals outside of the loop.
    Last edited by Dave_Sinkula; 04-17-2003 at 09:55 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM