User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,588 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,668 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 130 | Replies: 3
Reply
Join Date: May 2008
Posts: 29
Reputation: wellibedamned is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
wellibedamned wellibedamned is offline Offline
Light Poster

stuck

  #1  
May 25th, 2008
could someone please check this code and tell me where i went wrong... it always gives the result 1.00000 but as you can see in the for loop it shouldn't be like that.
#include <cstdio>
int main(void)
{
    int n;
    float s = 0;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
            {
            if(i % 2 == 0) s -= 1/(i*i);
            else s += 1/(i*i);    
            }            
    printf("%1.5f", s);
    scanf("\n");
    return 0;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 184
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: stuck

  #2  
May 25th, 2008
You need to be working with floats on the right-hand side to get floating-point division. Right now you are doing integer divisions.
s -= 1.0/(i*i);

This one gets everyone at least once a year. Hope this helps.
Reply With Quote  
Join Date: May 2006
Location: India
Posts: 247
Reputation: hammerhead is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: stuck

  #3  
May 25th, 2008
Typecasting worked for me
  if(i % 2 == 0) s -= 1/(float)(i*i);
            else s += 1/(float)(i*i);    
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote  
Join Date: May 2008
Posts: 29
Reputation: wellibedamned is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
wellibedamned wellibedamned is offline Offline
Light Poster

Re: stuck

  #4  
May 25th, 2008
hmm, thanks alot people... i never really did typecasting.. works!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

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

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:06 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC