10 Replies - 940 Views - Last Post: 10 March 2013 - 07:28 PM Rate Topic: -----

#1 rochie..1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-March 13

how can i improve my code

Posted 10 March 2013 - 03:51 AM

#include <stdio.h>
#include <stdlib.h>

#define FALSE 0
#define TRUE 1


main()
{
      float weight;
      float height;
      char age;
      double BMI;
      int year, mass;
      
      printf("Please enter the year you were born : ");
      scanf("%d", & age);        

      printf("Please enter your weight in kg :");
      scanf("%f", &weight);
      
      printf("Please enter your height in meters :");
      scanf("%f",&height);
      age=2013-age ;
      BMI=mass/height;
      
      if (age <= 18) 
      {
            if (BMI  >= 19.99)
            {
                   printf("your an underweight child\n");
            }
      
            else if ((BMI <=20.00)||(BMI<=28.00))
            {
                   printf("healthy weght child keep working out\n");
            }
            else if ((BMI <=29.00 )||(BMI<=31.00))
            {
                   printf("You overweight child workout please\n");
            }
            else if (BMI >= 32.00)
            {
                   printf("you are OBESE!!child \n");
            }

      else if (age <= 18)
      {
            if (BMI <=18.50)          
            {
                   printf("your underweight\n");
                   
                   if(BMI <=16.00)
                          {
                          printf("Your severe thinness");
                          }
                   if((BMI<=16.00)||(BMI<=16.99))
                          {
                           printf("your moderate thinness");
                          }
                   if((BMI<=17.00)||(BMI<=18.49))
                          {
                          printf("mild thinness");
                          }
            }
            else if ((BMI <=18.50)||(BMI<=24.99))
            {
                   printf("your normal range \n");
            }
            else if (BMI >= 25.00)
            {
                   printf("your over weight\n");
                   if((BMI<=25.00)||(BMI<=29.99))
                   {
                   printf("your pre-obese");
            }
           
            else if (BMI >=30.00)
            {
                   printf("You are obese\n");
                   
                  
                   if((BMI<=30.00)||(BMI<=34.99))
                   {
                   printf("obese class i");
                   }
                  
                   if((BMI<=35.00)||(39.99))
                   {
                   printf("obese class ii");
                   }
                  
                   if(BMI >= 40.00)
                   {
                   printf("your obese class iii");
                   }
        }
        
      system("PAUSE");
                      
}


*** EDIT ***
Please use code tags when posting code
:code:

This post has been edited by GunnerInc: 10 March 2013 - 09:52 AM
Reason for edit:: Added elusive code tags


Is This A Good Question/Topic? 0
  • +

Replies To: how can i improve my code

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,361
  • Joined: 02-June 10

Re: how can i improve my code

Posted 10 March 2013 - 06:23 AM

Please fix your code tags. Its not rocket science and its explained all over including the box where you pasted your code.

Posted Image

Also... Isn't this C++ code? You posted in VB.NET
Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,361
  • Joined: 02-June 10

Re: how can i improve my code

Posted 10 March 2013 - 06:34 AM

This isn't really a "what could I do better" question.
This is a 'find my mistakes for me so I don't have to do my own debugging.' thread. There are numerous issues with this that normally running/testing/debugging would find. I truly think you just don't feel like debugging and cleaning up your own homework and want us to do it.

Just a few examples are...

printf("your an underweight child\n");
Wrong word... You're is the contraction of you are. Your is possession like "This is your toaster". So you need to change all of your 'your' to you're

printf("healthy weght child keep working out\n");
Weight not weght


Assume a BMI of 10... All of these if checks are going to be true and thus all of these statements are going to print
if(BMI <=16.00)
{
   printf("Your severe thinness");
}
if((BMI<=16.00)||(BMI<=16.99))
{
   printf("your moderate thinness");
}
if((BMI<=17.00)||(BMI<=18.49))
{
   printf("mild thinness");
}


We do help people think through better design ideas. But this is just laziness and trying to get others to do your work for you.
Was This Post Helpful? 3
  • +
  • -

#4 rochie..1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-March 13

Re: how can i improve my code

Posted 10 March 2013 - 06:35 AM

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define FALSE 0
#define TRUE 1


main()
{
      float weight;
      float height;
      char age;
      double BMI;
      int year, mass;
      
      printf("Please enter the year you were born : ");
      scanf("%d", & age);        

      printf("Please enter your weight in kg :");
      scanf("%f", &weight);
      
      printf("Please enter your height in meters :");
      scanf("%f",&height);
      
      
      age=2013-age ;
      BMI=mass/height;
      
      if (age <= 18) 
      {
            if (BMI  >= 19.99)
            {
                   printf("your an underweight child\n");
            }
      
            else if ((BMI <=20.00)||(BMI<=28.00))
            {
                   printf("healthy weght child keep working out\n");
            }
            else if ((BMI <=29.00 )||(BMI<=31.00))
            {
                   printf("You overweight child workout please\n");
            }
            else if (BMI >= 32.00)
            {
                   printf("you are OBESE!!child \n");
            }

      else if (age <= 18)
      {
            if (BMI <=18.50)          
            {
                   printf("your underweight\n");
                   
                   if(BMI <=16.00)
                          {
                          printf("Your severe thinness");
                          }
                   if((BMI<=16.00)||(BMI<=16.99))
                          {
                           printf("your moderate thinness");
                          }
                   if((BMI<=17.00)||(BMI<=18.49))
                          {
                          printf("mild thinness");
                          }
            }
            else if ((BMI <=18.50)||(BMI<=24.99))
            {
                   printf("your normal range \n");
            }
            else if (BMI >= 25.00)
            {
                   printf("your over weight\n");
                   if((BMI<=25.00)||(BMI<=29.99))
                   {
                   printf("your pre-obese");
            }
           
            else if (BMI >=30.00)
            {
                   printf("You are obese\n");
                   
                  
                   if((BMI<=30.00)||(BMI<=34.99))
                   {
                   printf("obese class i");
                   }
                  
                   if((BMI<=35.00)||(39.99))
                   {
                   printf("obese class ii");
                   }
                  
                   if(BMI >= 40.00)
                   {
                   printf("your obese class iii");
                   }
        }
        
      system("PAUSE");
                      
}



Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,361
  • Joined: 02-June 10

Re: how can i improve my code

Posted 10 March 2013 - 07:53 AM

Much easier to read. Thank you. But you didn't seem to fix anything.

I also think you're not familiar with the difference between a greater than and less than symbol. Or the differences between and (&&) and or (||) Look at line 60. Are you trying to create a range between two values? Because both of your comparisons are LESS THAN. A range would be "greater than or equal to x AND less than or equal to y"

Here's a tip that helps a lot of people: Read your code out loud as a sentence and see if it makes sense and describes what you want.


060 if((BMI<=16.00)||(BMI<=16.99))
"If BMI is less-than-or-equal-to 16, or, BMI is less-than-or-equal-to 16.99"
Hmmm... Not quite right. Not a range. What about this sentence..
"If BMI is Greater-than-or-equal-to 16, and BMI less-than-or-equal-to 16.99"
Hmmm... That sounds like a range between two numbers. Now what would that look like in code?
Was This Post Helpful? 1
  • +
  • -

#6 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: how can i improve my code

Posted 10 March 2013 - 09:51 AM

There is no way this is VB.NET code!! Please watch what forums you post to! Also, please use code tags when posting code.
:code:
Was This Post Helpful? 1
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5957
  • View blog
  • Posts: 23,226
  • Joined: 23-August 08

Re: how can i improve my code

Posted 10 March 2013 - 10:00 AM

Let's throw this into Sublime Text 2 and reindent it shall we?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define FALSE 0
#define TRUE 1


main()
{
  float weight;
  float height;
  char age;
  double BMI;
  int year, mass;
  
  printf("Please enter the year you were born : ");
  scanf("%d", & age);        

  printf("Please enter your weight in kg :");
  scanf("%f", &weight);
  
  printf("Please enter your height in meters :");
  scanf("%f",&height);
  
  
  age=2013-age ;
  BMI=mass/height;
  
  if (age <= 18) 
  {
    if (BMI  >= 19.99)
    {
      printf("your an underweight child\n");
    }
    
    else if ((BMI <=20.00)||(BMI<=28.00))
    {
      printf("healthy weght child keep working out\n");
    }
    else if ((BMI <=29.00 )||(BMI<=31.00))
    {
      printf("You overweight child workout please\n");
    }
    else if (BMI >= 32.00)
    {
      printf("you are OBESE!!child \n");
    }

    else if (age <= 18)
    {
      if (BMI <=18.50)          
      {
        printf("your underweight\n");
        
        if(BMI <=16.00)
        {
          printf("Your severe thinness");
        }
        if((BMI<=16.00)||(BMI<=16.99))
        {
          printf("your moderate thinness");
        }
        if((BMI<=17.00)||(BMI<=18.49))
        {
          printf("mild thinness");
        }
      }
      else if ((BMI <=18.50)||(BMI<=24.99))
      {
        printf("your normal range \n");
      }
      else if (BMI >= 25.00)
      {
        printf("your over weight\n");
        if((BMI<=25.00)||(BMI<=29.99))
        {
          printf("your pre-obese");
        }
        
        else if (BMI >=30.00)
        {
          printf("You are obese\n");
          
          
          if((BMI<=30.00)||(BMI<=34.99))
          {
            printf("obese class i");
          }
          
          if((BMI<=35.00)||(39.99))
          {
            printf("obese class ii");
          }
          
          if(BMI >= 40.00)
          {
            printf("your obese class iii");
          }
        }
        
        system("PAUSE");
        
      }





Can you see the issues, now that it's properly indented? Did you even bother to compile this? As it is now, it will not compile.
Was This Post Helpful? 3
  • +
  • -

#8 rochie..1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-March 13

Re: how can i improve my code

Posted 10 March 2013 - 11:37 AM

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define FALSE 0
#define TRUE 1


main()
{
      float weight;
      char age ;
      int height;
      double BMI;
      
      printf("Please enter your age: ");
      scanf("%c", &age);
      
      printf("Please enter your height in meters:");
      scanf("%d", &height);
      
      printf("please enter your weight in kg:");
      scanf("%f",&weight);
      
      BMI=weight/height;
      
      if (age  <=18) 
      {
            if (BMI <19.99)
            {
                   printf("You are underweight\n");
            }
      
            else if ((BMI >= 20.00)&&(BMI<=28.00))
            {
                   printf("You are healty weight child\n");
            }
            else if ((BMI >=29.00)&&(BMI<=31.00))
            {
                   printf("YOu're overweight child\n");
            }
            else if (BMI >= 32.00)
            {
                   printf("You are a very obese child\n");
            }
      }
      else if (age>18)
      {
            if (BMI >= 18.50)          
            {
                   printf("You are underweight adult\n");
                          
                   if(BMI<16.00)
                   {
                        printf("you're severe thiness\n");
                   }
                   if((BMI>=16.00)&&(BMI<=16.99))
                   {
                        printf("moderate thinness\n");
                   }
                   if((BMI>=17.00)&&(BMI<=18.49))
                   {
                        printf("moderate thinness\n");
                   }
            }
            else if ((BMI <= 18.50)&&(BMI<=24.99))
            {
                   printf("You are in healthy condition\n");
            }
            else if (BMI >=25.00 )
            {
                   printf("You are overweight\n");
                   if((BMI >=25.00)&&(BMI<=29.99))
                   {
                        printf("you are pre obese\n");
                   }
            }
            else if (BMI >= 30.00)
            {
                   printf("You are obese\n");
                   if((BMI >=30.00)&&(BMI<=34.99))
                   {
                        printf("obese class i\n");
                   }
                   if((BMI >=35.00)&&(BMI<=39.99))
                   {
                        printf("obese class ii\n");
                   }
                   if(BMI >=32.00)
                   {
                        printf("obese class iii\n");
                   }
            }
            
      }
      system("PAUSE");
}



i made a new code but its runs but
 19	      printf("Please enter your height in meters:");
 

and
22	      printf("please enter your weight in kg:");

print on a same line ... how can i solve this
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5957
  • View blog
  • Posts: 23,226
  • Joined: 23-August 08

Re: how can i improve my code

Posted 10 March 2013 - 12:07 PM

I think you really want age to be an int, don't you?
Was This Post Helpful? 1
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: how can i improve my code

Posted 10 March 2013 - 01:17 PM

Use functions!

Your logic is a little whack. Let's just boil it down:
if (age <=18) {
	if (BMI <19.99) { 
		/* ok, fine */
	} else if ((BMI >= 20.00) && (BMI<=28.00)) {
		/* ooops, BMI 19.991...19.999999 just got skipped
	} else if ((BMI >=29.00)&&(BMI<=31.00)) {
		/* just skipped 28.xxx */
	} else if (BMI >= 32.00)
		/* just skipped 31.xxx */



The > 18 is actually worse.

Instead:
if (age <=18) {
	if (BMI <20) { 
	} else if (BMI<29) {
	} else if (BMI<32) {
	} else {
	}
} else {



Now you don't miss any.

Height should also be float: there's a big different between 1 meter and 2 meters.

This post has been edited by baavgai: 10 March 2013 - 01:17 PM

Was This Post Helpful? 1
  • +
  • -

#11 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: how can i improve my code

Posted 10 March 2013 - 07:28 PM

It may not seem necessary now, but main() is ALWAYS supposed to return an int -- ALWAYS!

int main(void) {




   return 0;
}

or

int main(argc, *argv[]) {







   return 0;
}



One or the other are OK. The operating system (yes, even DOS!) expects a running program to return an int at the end of it's run. If there should be a problem during the run (maybe a disk drive got full, etc., the program could return a 1, 2, etc., which you could interpret to tell you what failed during the run.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1