Like Tree1Likes
  • 1 Post By Matticus

Float math

This is a discussion on Float math within the C Programming forums, part of the General Programming Boards category; Hey guys. I'm working on this program that asks the user for a price, then a discount rate then outputs ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    9

    Float math

    Hey guys. I'm working on this program that asks the user for a price, then a discount rate then outputs the final sale price on a table format. However, the math is wrong for the sale price.

    My code is as follows:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	system("cls");	
    
    	//Declarations
    	float sales[4][2];
    	int i = 0;		//First Item
    	int j = 0;		//First Category
    				//j=0 is original price
    				//j=1 is discounted rate
    				//j=2 is sale price
    
    	printf("\n\n\t\tWelcome to Haakon's dicount caluclator!");
    	printf("\n\n");
    
    	for(i=0; i<5; i++)
    	{
    		//Input
    		//sales[i][j] where i is item and j is category in a for loop
    		printf("\n\tPrice for Item #%d:\t$", i+1);
    		scanf(" %f", &sales[i][0]);
    
    		printf("\tDiscount for Item #%d:\t%%", i+1);
    		scanf(" %f", &sales[i][1]);
    
    		//Process
    		sales[i][2] = sales[i][0] * (1 - (sales[i][1] / 100));	
    	}	
    
    	//Output
    	printf("\n\n");
    	printf("\t\tPrice \t\tDiscount \t\tSale Price\n");
    	for(i=0; i<5; i++)
    	{
    		printf("Item #%d\t\t$%.2f \t\t%%%.0f \t\t$%.2f\n", i+1, sales[i][0], sales[i][1], sales[i][2]);
    	}
    
    	return 0;
    }
    I'm trying out three different scenarios:
    sales[i][0]=$10; sales[i][1]=%25; sales[i][2]=$7.5
    Instead I get $15.50
    sales[i][0]=$15.50; sales[i][1]=%12; sales[i][2]=$13.64
    Instead I get $15.50
    sales[i][0]=$42.50; sales[i][1]=%10; sales[i][2]=$38.25

    Anyone got any ideas?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,241
    You're misunderstanding some basic rules of arrays. For an "array[n]", the indices range from zero to "n-1". Therefore,

    Code:
        // array declaration
        int array[6];
    
        // array definition; note the index numbers
        array[0] = 1;
        array[1] = 2;
        array[2] = 3;
        array[3] = 4;
        array[4] = 5;
        array[5] = 6;
    So when you declare "float sales[4][2]", then you only have:

    Code:
        sales[n][0]  // original price
        sales[n][1]  // discounted rate
    "sales[n][2]" doesn't exist with your current array declaration. You also overrun the leftmost array index by ranging "i" from 0 to 4 (it's limited at 0 to 3).
    Salem likes this.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    9
    I'm dumb.. Thank you Matticus, it all makes sense now.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,241
    If you've learned anything from this mistake, then you are not dumb. Happy coding.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (float) or (float*) wrt sizeOf()
    By jakemott in forum C Programming
    Replies: 2
    Last Post: 07-19-2009, 08:13 PM
  2. trying to do simple math with float and int type
    By demuro1 in forum C Programming
    Replies: 16
    Last Post: 09-24-2008, 11:58 AM
  3. Replies: 8
    Last Post: 07-08-2005, 09:12 AM
  4. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 02:14 AM
  5. Unresolved external 'convert(float, float)'
    By Ipsec Espah in forum C++ Programming
    Replies: 4
    Last Post: 05-21-2003, 10:08 AM

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