Thread: Value not calculated

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    Value not calculated

    Hi, I'm trying to write a program that works out BMI from inputted height and weight, it all works fine but the calculation for bmi doesn't do anything and I don't understand why.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	float arrayName[5][3];
    	int i;
    
    
    	for (i=1; i<6; i++)
    	{
    	printf("\nEnter the Weight of person %d in (kg): ", i);
    	scanf("%f", &arrayName[i-1][0]);
    	printf("\nEnter the Height of person %d in (m): ", i);
    	scanf("%f", &arrayName[i-1][1]);
    
    	arrayName[i-1][2]=arrayName[i-1][1]/arrayName[i-1][0]*arrayName[i-1][0];
    
    
    	}
    
    
    	printf("\n\n\tPerson #\t:\tWeight (kg)\t:\tHeight (m)\t:\tBMI\t");
    	printf("\n\t\t1\t:\t%0.2f\t\t:\t%0.2f\t\t:\t%0.2f\t", arrayName[0][0], arrayName[0][1], arrayName[0][2]);
    	printf("\n\t\t2\t:\t%0.2f\t\t:\t%0.2f\t\t:\t%0.2f\t", arrayName[1][0], arrayName[1][1], arrayName[1][2]);
    	printf("\n\t\t3\t:\t%0.2f\t\t:\t%0.2f\t\t:\t%0.2f\t", arrayName[2][0], arrayName[2][1], arrayName[2][2]);
    	printf("\n\t\t4\t:\t%0.2f\t\t:\t%0.2f\t\t:\t%0.2f\t", arrayName[3][0], arrayName[3][1], arrayName[3][2]);
    	printf("\n\t\t5\t:\t%0.2f\t\t:\t%0.2f\t\t:\t%0.2f\t\n", arrayName[4][0], arrayName[4][1], arrayName[4][2]);
    
    	return 0;
    
    }
    Last edited by Salem; 02-12-2011 at 01:20 AM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please read the forum guidelines and use code tags next time.

    Could it be your BMI formula? Think about order of operations. You basically have: BMI = height / weight * weight.
    It divides height by weight, then multiplies that by weight, cancelling out the divide and effectively giving you: BMI = height

    What is the formula for BMI? We're programmers, not nutrition and health experts, so help us out a bit.

    Also, why aren't you using a loop for printing output?

    EDIT: Traditionally, an array processing loop looks like
    Code:
    int i, array[SIZE];
    for (i = 0; i < SIZE; i++) {
        // do stuff in here
    }
    It's easier to avoid going off the end of the array that way, and you can just adjust the index when you print it. It's safer and easier for people to read too.
    Last edited by anduril462; 02-11-2011 at 04:35 PM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    it's height/weight^2 apparently so that's probably the problem and I'll try using a loop for the output, I only started programming yesterday so it's all pretty new at the moment.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, you're off to a good start. A set of ( ) should handle your order of operations problem just fine.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    sorry, weight/height^2

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Put parenthesis in appropriate places. Your compiler divides before it multiplies.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need your help guys :'(
    By Saarah in forum C Programming
    Replies: 24
    Last Post: 09-08-2006, 01:18 AM
  2. The highest Fibonacci number ever calculated
    By dwks in forum C Programming
    Replies: 19
    Last Post: 07-27-2005, 05:57 PM
  3. Having a problem!
    By Zildjian in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2004, 09:40 AM
  4. How are s&t calculated on a NURBS surface?
    By BrianK in forum Game Programming
    Replies: 1
    Last Post: 12-04-2002, 11:48 PM
  5. how to make the worm move
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 11-10-2001, 03:11 PM