[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

weight

I am a beginner in C and am trying to write the following program that will compute a person's weight on the following planets.
PLANET                  PERCENTAGE OF EARTH WEIGHT
EARTH                   100
MOON                    16
JUPITER                 264
VENUS                   85
MARS                    38
Create a printed table of weights ranging from 50 to 250 pounds in steps 0f 10 pounds. Place "Weight and Planet Headers" across the top of the page and print the table with the weights running topo to bottom.
0
barn
Asked:
barn
  • 3
  • 2
1 Solution
 
barnAuthor Commented:
Edited text of question
0
 
barnAuthor Commented:
Edited text of question
0
 
cwestinCommented:
What are you asking?  Do you just want someone else to write this for you?  Is this for a class?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RONSLOWCommented:
Sounds like a class assignment to me :-)

so you want something like:

/* these are our planets */
typedef enum {
  EARTH, MOON, JUPITER, VENUS, MARS
} Planet;

void main () {
  /* here are the names of the planets */
  char* name[] = {"Earth","Moon","Jupiter","Venus","Mars"};
  /* here are the percentage of earth weight for each planet */
  int percentage [] = { 100,16,264,85,38 };
  int earth_weight;

  /* heading line */
  printf("\tWeight and Planet\n");

  /* columns heading - one per planet */
  for (planet = EARTH; planet <= MARS; planet++) {
    printf("%8.8s",name[planet]);
  }
  printf("\n");

  /* each row is a different weight */
  for (
    earth_weight = 50;
    earth_weight <= 250;
    earth_weight += 10
  ) {
    enum Planet planet;
    /* calculate actual weight for each planet and print */
    for (planet = EARTH; planet <= MARS; planet++) {
      int weight = earth_weight*percentage[planet]/100;
      printf ("%8d",weight);
    }
    /* end of line */
    printf ("\n");
  }
}

Any questions??

0
 
barnAuthor Commented:
Thank you for the help.
I tried to run your program and got 3 error messages.
1. Declaration syntax error in function main.
2. Undefined symbol 'planet' in function main.
3. Undefined symbol 'earth_weight' in function main.

0
 
RONSLOWCommented:
OK - little syntax error :-)

Couldn't you have fixed this yourself - I just typed in a program off the top of my head - didn't know you wanted my to debug it as well !! - only problem was that I had the declaraion of Planet

Here is a version that compiles on my compiler

#include <stdio.h>

/* these are our planets */
typedef enum Planet {
      EARTH, MOON, JUPITER, VENUS, MARS
} Planet;

void main () {
      /* here are the names of the planets */
      char* name[] = {"Earth","Moon","Jupiter","Venus","Mars"};
      /* here are the percentage of earth weight for each planet */
      int percentage [] = { 100,16,264,85,38 };
      int earth_weight;
      Planet planet;
      
      /* heading line */
      printf("\tWeight and Planet\n");
      
      /* columns heading - one per planet */
      for (planet = EARTH; planet <= MARS; planet++) {
            printf("%8.8s",name[planet]);
      }
      printf("\n");
      
      /* each row is a different weight */
      for (
            earth_weight = 50;
      earth_weight <= 250;
      earth_weight += 10
            ) {
            /* calculate actual weight for each planet and print */
            for (planet = EARTH; planet <= MARS; planet++) {
                  int weight = earth_weight*percentage[planet]/100;
                  printf ("%8d",weight);
            }
            /* end of line */
            printf ("\n");
      }
}

Hope this works better for you - if not, then I cannot help because it compiles fine with no warnings etc on my compiler.  You will need to get any other bug out yourself - that is how you LEARN !!!

Roger

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now