LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2005, 06:03 AM   #1
J_K9
Member
 
Registered: Nov 2004
Distribution: Slackware 11, Ubuntu 6.06 LTS
Posts: 700

Rep: Reputation: 30
Simple C program - error with float


Hi,

I'm currently going through this C tutorial, and here's the task I've been set as an exercise:
Quote:
2. Write a program that counts from 1 to 12 and prints the count and its inversion to 5 decimal
places for each count. This will require a floating point number.
1 1.00000
2 .50000
3 .33333
4 .25000
etc.
I thought it'd be fairly easy at first, but for some unknown reason my code doesn't work! Here it is:
Code:
#include <stdio.h>

int main()
{
  int count;
  float invert;
  for(count = 1; count < 13; count++) {
    invert = (1 / count);
    printf("%d\t\t%8.5f\n", count, invert); }
  return 0;
}
The output in terminal is this:
Code:
1		 1.00000
2		 0.00000
3		 0.00000
4		 0.00000
5		 0.00000
6		 0.00000
7		 0.00000
8		 0.00000
9		 0.00000
10		 0.00000
11		 0.00000
12		 0.00000
I'm not sure why this is happening, because it is a float, and I've set the output to have 5 decimal places. Any help would be appreciated.

M.
 
Old 10-24-2005, 06:22 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
its quite simple when you know whats going on.
this line
invert = (1 / count);
if you replace the variables and constant with there types
float = int/int; do you see the error now?

Last edited by dmail; 10-24-2005 at 06:25 AM.
 
Old 10-24-2005, 11:36 AM   #3
J_K9
Member
 
Registered: Nov 2004
Distribution: Slackware 11, Ubuntu 6.06 LTS
Posts: 700

Original Poster
Rep: Reputation: 30
Of course! Thank you so much!

For those who might have had similar problems, just replace the 'invert = (1 / count);' with 'invert = (1.00 / count);'
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM
Simple C pow() program, strange error funkymunky Programming 2 06-25-2004 04:20 AM
A simple program on C++ for the linux with an error. Israfel2000 Programming 4 06-16-2004 06:25 PM
Error for a very simple C program pkashyap Linux - Newbie 2 03-11-2004 10:24 AM
Help with the following simple C program ..... purpleburple Programming 5 09-13-2002 10:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration