# 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. ## 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
sales[i][0]=\$15.50; sales[i][1]=%12; sales[i][2]=\$13.64
sales[i][0]=\$42.50; sales[i][1]=%10; sales[i][2]=\$38.25

Anyone got any ideas?

2. 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).

3. I'm dumb.. Thank you Matticus, it all makes sense now.

4. If you've learned anything from this mistake, then you are not dumb. Happy coding.