![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
C programming problem
I have an assignment due in my Intro to Programming class. I have it all except for one part is wrong. The program is supposed to add fractions. The problem lies with 1/11. My teacher put up his EXE file and when I compare them they are all right except my 1/11 adds to be .99999988 while his is 1.00000012. I assumed it was maybe a type conversion problem or something, but it only happens with 1/11. Can anybody offer a solution? I'm running this in Visual Basic 6.0.
#include <stdio.h> #include <math.h> int main () { float sums[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; float n=0; int m=0, q=0, x=0, y=0, z=0; printf( "Representational Error - Sums of Fractions\n\n"); for( n=2; n<=15; ++n) { m = n; for( q=1; q<=m; ++q) { sums[m] += (1/n); } if (sums[m] == 1) { printf( "Adding %d copies of 1/%d = %.8f, is correct.\n", m, m, sums[m]); ++y; } else if (sums[m] > 1.00) { printf( "Adding %d copies of 1/%d = %.8f, is greater than 1.00.\n", m, m, sums[m]); ++z; } else if (sums[m] < 1.00) { printf( "Adding %d copies of 1/%d = %.8f, is less than 1.00.\n", m, m, sums[m]); ++x; } } printf( "\n%d sums are < 1, %d sums are = 1, and %d sums are > 1.", x,y,z); return 0; } |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You sure you're using Visual Basic?
|
|
|
|
|
|
#3 |
|
Newbie
|
Oh oops. I'm sorry. I'm using Microsoft Visual C++. But the file is a .c.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|