Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 27th, 2005, 4:24 PM   #1
mmmm_strawberries
Newbie
 
Join Date: Feb 2005
Posts: 10
Rep Power: 0 mmmm_strawberries is on a distinguished road
Send a message via AIM to mmmm_strawberries
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;

}
mmmm_strawberries is offline   Reply With Quote
Old Feb 27th, 2005, 4:31 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You sure you're using Visual Basic?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 27th, 2005, 4:32 PM   #3
mmmm_strawberries
Newbie
 
Join Date: Feb 2005
Posts: 10
Rep Power: 0 mmmm_strawberries is on a distinguished road
Send a message via AIM to mmmm_strawberries
Oh oops. I'm sorry. I'm using Microsoft Visual C++. But the file is a .c.
mmmm_strawberries is offline   Reply With Quote
Old Mar 1st, 2005, 3:48 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Try using the double type instead of float.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:55 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC