Help asap!

This is a discussion on Help asap! within the C Programming forums, part of the General Programming Boards category; I have this assignment due in 2 hours and I can't figure out why it is not giving me any ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    26

    Help asap!

    I have this assignment due in 2 hours and I can't figure out why it is not giving me any output. When I don't set the counters and accumulators to 0 it works, but when I do it doesn't!

    Here is the code:
    Code:
    #include<stdlib.h>        
    #include<stdio.h>            //standard library definitions
    #include<math.h>            //math definitions
    #define BAL_HEIGHT 12    //balcony height in feet
    #define G  32               //gravitational acceleration in ft/s2
    #define PI 3.14159
    #define POOL_CENTER 35  //distance to the center of pool
    #define MIN_THETA 5        //minimum balloon launch angle in degrees
    #define MIN_VEL 1            //minimum balloon launch velocity in ft/sec
    #define MIN_THR_HT 4.5    //minimum height of thrower in feet
    #define MAX_THETA 85        //maximum balloon launch angle in degrees
    #define MAX_VEL 30        //maximum balloon launch velocity in ft/sec
    #define MAX_THR_HT 7.0    //maximum height of thrower in feet
    #define MIN_DIAM 3        //minimum diameter of balloon in inches
    #define MAX_DIAM 9        //maximum diameter of balloon in inches
    #define CAPACITY 7        //pool capacity in gallons
    
       int main()
       {
          double balloonLaunchAngle;                    //input: angle in degrees
          double balloonLaunchVelocity;                //input: launch velocity in ft/sec
          double throwersHeight;                        //input: user's height plus balcony height in feet
          double distanceTraveled;                    //output: distance the balloon traveled in feet
          double part1, part2, part3;                //distance formula: partial result holders
          int balloonDiam;                               //ballon diameter in inches
          double balloonWater;                            //amount of water in balloon in gallons
          int poolWater;                                //amount of water in pool in gallons
          int balloonNumThrown;                    //number of balloons "thrown"
          int balloonNumHit;                        //number of balloons that "hit" the pool
          double hitPercentage;                        //percentage of balloons that "hit" the pool
       
          balloonNumHit=0;
          balloonNumThrown=0;
          poolWater=0;
       
          /*balloonDiam=rand()%(MAX_DIAM-MIN_DIAM+1)+MIN_DIAM;
          balloonLaunchAngle=(double)rand()/RAND_MAX*(MAX_THETA-MIN_THETA+1)+MIN_THETA;
          balloonLaunchVelocity=(double)rand()/RAND_MAX*(MAX_VEL-MIN_VEL+1)+MIN_VEL;
          throwersHeight=(double)rand()/RAND_MAX*(MAX_THR_HT-MIN_THR_HT+1)+MIN_THR_HT;*/
           
           srand(time(NULL));
       
          while(poolWater<CAPACITY)
          {
             balloonDiam=rand()%(MAX_DIAM-MIN_DIAM+1)+MIN_DIAM;
             balloonLaunchAngle=(double)rand()/RAND_MAX*(MAX_THETA-MIN_THETA+1)+MIN_THETA;
             balloonLaunchVelocity=(double)rand()/RAND_MAX*(MAX_VEL-MIN_VEL+1)+MIN_VEL;
             throwersHeight=(double)rand()/RAND_MAX*(MAX_THR_HT-MIN_THR_HT+1)+MIN_THR_HT;
          
          
             switch(balloonDiam)
             {
                case 3: balloonWater=0.1;
                   break;
                case 4: balloonWater=0.2;
                   break;
                case 5: balloonWater=0.3;
                   break;
                case 6: balloonWater=0.55;
                   break;
                case 7: balloonWater=0.8;
                   break;
                case 8: balloonWater=1.25;
                   break;
                case 9: balloonWater=1.7;
                   break;
             }
             
             part1=(balloonLaunchVelocity*cos(balloonLaunchAngle*PI/180.0))/G;
             part2=balloonLaunchVelocity*sin(balloonLaunchAngle*PI/180.0);
             part3=2*G*(throwersHeight+BAL_HEIGHT);
             distanceTraveled=part1*(part2+(sqrt(pow(part2,2)+part3)));
              
             if(distanceTraveled==35)
             {
                poolWater=poolWater+balloonWater;
                balloonNumHit++;
             }
             balloonNumThrown++;
          }
           
          hitPercentage=(balloonNumHit/balloonNumThrown)*100;
          printf("balloon diameter:%d\n", balloonDiam);
         // printf("balloon Launch Angle:%f\n", balloonLaunchAngle);
         // printf("balloon Launch Velocity:%f\n", balloonLaunchVelocity);
          ///printf("Throwers Hiehgt:%f\n", throwersHeight);
          //printf("Balloon num thrown: %d\n", balloonNumThrown);
         // printf("balloonNumHit: %d\n", balloonNumHit);
          //printf("Distance Balloon Traveled: %f\n", distanceTraveled);
           
          printf("%d balloons hit the pool.\n", balloonNumHit);
          printf("%d balloons were thrown.\n", balloonNumThrown);
          printf("%.2f %% balloons hit the pool", hitPercentage);
          return(0);
       }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well... you're not getting output because your printf() statements are mostly commented out...
    You're probably getting wrong answers because your variables are doubles but you're trying to print floats.
    Look up printf() in your C library documentation... it will help you fix the formatting errors.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    26
    I added those printf's in there but commented them out temporarily so I know why I'm not seeing those. I thought about that...which double variables are you referring to?

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    balloonLaunchAngle for one; you declare that as a double, but the %f in printf expects a float.

    Your balloonHitPercentage won't work properly either because you are performing integer division. You want to force floating point division, like this:
    Code:
    hitPercentage=( (double) balloonNumHit/balloonNumThrown)*100;
    Last edited by TheBigH; 09-22-2011 at 09:57 PM.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by TheBigH View Post
    balloonLaunchAngle for one; you declare that as a double, but the %f in printf expects a float.
    Actually %f and %lf are the same for printf. The difference matters with scanf.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    26
    I just looked in my programming book and it says that when using printf of a double use %f
    I'm not getting anything at all on my MS-DOS box

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... just ran this on my own system...

    1) you need to add #include <time.h> ... for your srand(time()); call.

    2) It's entering an infinite loop... because you initially set poolWater to 0 and the only time you change it's value is if distanceTravelled == 35 ... so you have to find a better way to administer that loop... perhaps on numThrown instead...

    3) The switch statement doesn't include all possibilities so it is possible for ballonWater to be used in an uninitialized state.

    4) there are a number of conversion errors reported that you should deal with.


    Best advice... turn your compiler's warning level all the way up and deal with every line it reports as an item that needs to be fixed.


    Lets hope that gets it...
    Last edited by CommonTater; 09-22-2011 at 10:17 PM.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    26
    Okay, let me do that and see if it works...

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    26
    All I did was add the #include<time.h> header but still...nothing=[

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tayexdrums View Post
    All I did was add the #include<time.h> header but still...nothing=[
    Go back up and re-read post #7 ... how many items does it list?

    Really... do them all or don't bother.

  11. #11
    Registered User
    Join Date
    Sep 2011
    Posts
    26
    No need to get hostile!lol
    But okay, I need to find a better to change the control variable poolWater instead of using distanceTraveled==35

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tayexdrums View Post
    No need to get hostile!lol
    Hey... you came to us (me) asking for "help ASAP"... and then you flat out ignore the advice you're given?

    It is very rare that any of us take posted code and compile it on our own systems...
    I've done you a special favor here and you just blew it all off.

    Not hostile... getting you to actually read what is offered!

  13. #13
    Registered User
    Join Date
    Sep 2011
    Posts
    26
    I know, i know...I thank you SO much for your assistance no doubt about it. I just think I was reading it so fast that I did what I saw first without thinking about it.

    Anyhow, I the poolWater HAS to be dependent upon whether or not the balloon hit the pool because that is the only way the water level can increase

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Then switch your while loop to administer on the number of baloons thrown... not the amount of water in the pool... interestingly you are already incrementing that variable at the bottom of the loop... just use it.

  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    26
    I got information to appear! but its still not right...however, I think I can figure out why...hopefully.lol
    thank you

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone Please Help! ASAP!!
    By CB44 in forum C++ Programming
    Replies: 8
    Last Post: 01-17-2011, 12:13 AM
  2. Someone Please Help! ASAP!!
    By CB44 in forum C Programming
    Replies: 1
    Last Post: 01-16-2011, 04:41 PM
  3. Need help!!!!!! Asap!!!!!
    By DMJJR1988 in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2010, 01:20 AM
  4. Need Help ASAP....
    By Maxwell in forum C++ Programming
    Replies: 16
    Last Post: 09-14-2005, 06:56 PM
  5. i really need help asap
    By dinesh in forum C Programming
    Replies: 1
    Last Post: 04-24-2003, 11:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21