Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 09-24-2006, 07:01 PM   #1 (permalink)
 
Newb Techie

Join Date: Sep 2006

Posts: 1

jmak88

Send a message via AIM to jmak88
Default C program--Help Please!!

I am relatively new to the programming world and was wondering if y'all could give me a hand with this lab. Pretty much it requires a user to press "+" or "-" until the two values (volumes of a cube and sphere) are equal, with the "+" or "-" changing the spheres value. The next loop would be the exact same thing except for the surface area of the cube to be changing. I'm unsure how to get the scan f working and if my loops are correct. Take a look and thanks!!
Code:
#include <stdio.h>
#include <math.h>

/* The prototype. */
char * prompt_and_get( char * prompt, char * response);

int main(){

  const double PI = atan2( 0.0, -1.0);
  double side, diameter, inp;
  double sphere_volume, sphere_surface, cube_volume, cube_surface, ratio_of_vol\
ume_to_surface_area_sphere, ratio_of_volume_to_surface_area_cube;
  double change;
  char * prompt_ptr, response[132], * response_ptr;

  double difference;

  printf("Enter a diameter and side separated by a space and followed by ENTER\\
n");
  scanf("%lf%lf", &diameter, &side);
  getchar();  /* get past the scanf pains */

  printf("You entered %10.2lf for the diameter and %10.2lf for the side\n",
         diameter, side);

  prompt_ptr = "Enter *, +, -, or simply ENTER: ";

  cube_volume = side * side * side;

  change = diameter;

  while(1){
    break;
    sphere_volume = PI * diameter * diameter * diameter / 6.0;
    printf("Sphere volume, cube volume %12.5lf %12.5lf\n",
           sphere_volume, cube_volume);

    difference = fabs(sphere_volume - cube_volume);
    if( (difference / sphere_volume) < 0.001  ) break;

    response_ptr = prompt_and_get( prompt_ptr, response );
 if( response[0] == '+' ){
      printf("A plus sign was entered\n");
      change = fabs( change ) * 0.5; }

    else if( response[0] == '-' ){
      printf("A minus sign was entered\n");
      change = -fabs( change) * 0.5; }

    else if( response[0]== '*' ){
      printf("Keeping change. %f\n", change); }

 else if( response[0] > ' ' ){
      printf("Why did you enter %s ?\n", response);
      continue; } /* Not quitting yet.*/

    else break;

    diameter = diameter + change;

    printf("Diameter is now %12.5lf\n", diameter);
    printf("Enter 0 to exit");
 scanf(&inp);
    if (inp == 0) break;
  }

    printf("Enter a side and a diameter by a space and followed by ENTER\n");
    scanf("%lf%lf", &side, &diameter);
    getchar();

    printf("You entered %10.2lf for the side and %10.2lf for the diameter\n",
           side, diameter);

 prompt_ptr = "Enter *, +, -, or simply ENTER: ";

    sphere_surface = PI * diameter * diameter;

    change = side;
    while(2){ break;
      cube_surface = 6.0 * side * side;
      printf("cube surface, sphere surface %12.5lf %12.5lf\n",
             cube_surface, sphere_surface);

      difference = fabs(cube_surface - sphere_surface);
      if( (difference / cube_surface) < 0.001  ) break;

      response_ptr = prompt_and_get( prompt_ptr, response );
      if( response[0] == '+' ){
        printf("A plus sign was entered\n");
        change = fabs( change ) * 0.5; }

      else if( response[0] == '-' ){
        printf("A minus sign was entered\n");
        change = -fabs( change) * 0.5; }

else if( response[0]== '*' ){/* Do not change change. */
        printf("Keeping change. %f\n", change); }

      else if( response[0] > ' ' ){/* strange input, expected
                                      + sign, minus sign, blank, or ENTER.*/
        printf("Why did you enter %s ?\n", response);
        continue; } /* Not quitting yet.*/

      else break;

      side = side + change;

      printf("side is now %12.5lf\n", side);
      printf("Enter 0 to exit");
      scanf(&inp);
      if (inp == 0) break;
    }

    ratio_of_volume_to_surface_area_cube = cube_volume / cube_surface;
    ratio_of_volume_to_surface_area_sphere = sphere_volume / sphere_surface;

 printf("%f ratio of volume to surface area in a cube\n %f ratio of volume t\
o surface area in a sphere\n", ratio_of_volume_to_surface_area_cube, ratio_of_v\
olume_to_surface_area_sphere);

    return 0; }

jmak88 is offline  
Old 09-24-2006, 07:35 PM   #2 (permalink)
 
True Techie

Join Date: May 2006

Posts: 150

fjf314 is on a distinguished road

Send a message via AIM to fjf314
Default

Just so you know, using the code tags is your friend in this case. It'd be a lot easier to read through what you've got if the whitespace wasn't all messed up.

<CODE>

***paste your code here***

</CODE>

Just replace the <> with []
__________________
There are 10 kinds of people in the world, those who understand binary and those who don\'t.
fjf314 is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On