You are here:

# C/Refresher question

Question
I took a class in C in college 10 years ago, but am beginning to brush up on it. To study up on some things, I wrote a sample program (code below)

#include <stdio.h>

int main()
{
float x1 = 0;
float x2 = 0;
float y1 = 0;
float y2 = 0;
float dx = 0;
float dy = 0;
float x = 0;
float y = 0;
float product = 0;

printf("Enter the lower x bound:");
scanf("%f", &x1);
printf("Enter the upper x bound:");
scanf("%f", &x2);
printf("Enter the increment for x:");
scanf("%f", &dx);
printf("Enter the lower y bound:");
scanf("%f", &y1);
printf("Enter the upper y bound:");
scanf("%f", &y2);
printf("Enter the increment for y:");
scanf("%f", &dy);

for(x=x1;x<=x2;x+=dx) {
for(y=y1;y<=y2;y+=dy) {
product = x * y;
printf("%2.2f %2.2f %2.2f\n",x,y,product);
}
}
getch();
}

This is a sample output from the program.

Enter the lower x bound:0
Enter the upper x bound:1
Enter the increment for x:.1
Enter the lower y bound:0
Enter the upper y bound:1
Enter the increment for y:.1
0.00 0.00 0.00
0.00 0.10 0.00
0.00 0.20 0.00
0.00 0.30 0.00
0.00 0.40 0.00
0.00 0.50 0.00
0.00 0.60 0.00
0.00 0.70 0.00
0.00 0.80 0.00
0.00 0.90 0.00
0.10 0.00 0.00
0.10 0.10 0.01
0.10 0.20 0.02
0.10 0.30 0.03
0.10 0.40 0.04
0.10 0.50 0.05
0.10 0.60 0.06
0.10 0.70 0.07
0.10 0.80 0.08
0.10 0.90 0.09
0.20 0.00 0.00
0.20 0.10 0.02
0.20 0.20 0.04
0.20 0.30 0.06
0.20 0.40 0.08
0.20 0.50 0.10
0.20 0.60 0.12
0.20 0.70 0.14
0.20 0.80 0.16
0.20 0.90 0.18
0.30 0.00 0.00
0.30 0.10 0.03
0.30 0.20 0.06
0.30 0.30 0.09
0.30 0.40 0.12
0.30 0.50 0.15
0.30 0.60 0.18
0.30 0.70 0.21
0.30 0.80 0.24
0.30 0.90 0.27
0.40 0.00 0.00
0.40 0.10 0.04
0.40 0.20 0.08
0.40 0.30 0.12
0.40 0.40 0.16
0.40 0.50 0.20
0.40 0.60 0.24
0.40 0.70 0.28
0.40 0.80 0.32
0.40 0.90 0.36
0.50 0.00 0.00
0.50 0.10 0.05
0.50 0.20 0.10
0.50 0.30 0.15
0.50 0.40 0.20
0.50 0.50 0.25
0.50 0.60 0.30
0.50 0.70 0.35
0.50 0.80 0.40
0.50 0.90 0.45
0.60 0.00 0.00
0.60 0.10 0.06
0.60 0.20 0.12
0.60 0.30 0.18
0.60 0.40 0.24
0.60 0.50 0.30
0.60 0.60 0.36
0.60 0.70 0.42
0.60 0.80 0.48
0.60 0.90 0.54
0.70 0.00 0.00
0.70 0.10 0.07
0.70 0.20 0.14
0.70 0.30 0.21
0.70 0.40 0.28
0.70 0.50 0.35
0.70 0.60 0.42
0.70 0.70 0.49
0.70 0.80 0.56
0.70 0.90 0.63
0.80 0.00 0.00
0.80 0.10 0.08
0.80 0.20 0.16
0.80 0.30 0.24
0.80 0.40 0.32
0.80 0.50 0.40
0.80 0.60 0.48
0.80 0.70 0.56
0.80 0.80 0.64
0.80 0.90 0.72
0.90 0.00 0.00
0.90 0.10 0.09
0.90 0.20 0.18
0.90 0.30 0.27
0.90 0.40 0.36
0.90 0.50 0.45
0.90 0.60 0.54
0.90 0.70 0.63
0.90 0.80 0.72
0.90 0.90 0.81

My question is if I put 1 as my upper values in the input, why does the output only go to .90? Thanks.

Hi David,

Floating point mathematics is not exact because of precision issues and the way compilers/CPU architectures process results. When I print the conditional from the for loop in your sample program to 10 decimal places, it shows variance in the variable being incremented.
```(0.1000000015 <= 1.0000000000) = true
(0.2000000030 <= 1.0000000000) = true
(0.3000000119 <= 1.0000000000) = true
(0.4000000060 <= 1.0000000000) = true
(0.5000000000 <= 1.0000000000) = true
(0.6000000238 <= 1.0000000000) = true
(0.7000000477 <= 1.0000000000) = true
(0.8000000715 <= 1.0000000000) = true
(0.9000000954 <= 1.0000000000) = true
(1.0000001192 <= 1.0000000000) = false
```

C

Volunteer

#### Tony Nazzal

##### Expertise

I will answer any c language programming and syntax questions.

##### Experience

I have almost a decade of c programming experience.

Education/Credentials
I have a bachelor of science degree and several years of industry experience.