473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Program to calculate 1/3^1 + .... + 1/3^n

4
Hello, I'm trying to make a C++ program which calculates the following series:

1/3^1 + 1/3^2 + .... + 1/3^n

So far I've tried this
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void main()
  5. {
  6.  
  7. int z=1, n, n2;
  8. float num=1/3, sum=0;
  9.  
  10.  
  11. cout<<"n= ";
  12. cin>>n;
  13.  
  14. while(n > 0)
  15. {
  16.     z=1;
  17.  
  18.     n2 = n;
  19.     while(n2 > 0)
  20.     {
  21.         z = num * z;
  22.         n2--;
  23.     }
  24.     sum = z + sum;
  25.     n--;
  26. }
  27. cout<<endl<<sum;
  28. }
  29.  
but it always returns zero. Any help would be appreciated.
May 19 '14 #1

✓ answered by Banfa

Just to be clear the problem in

Expand|Select|Wrap|Line Numbers
  1. float num=1/3, sum=0;
  2.  
Is that 1 and 3 are both integer constants so the calculation is done in integer arithmetic which drops all decimal places therefore 1/3 = 0.

This is easy to fix by forcing 1 or both of the constants to be floating point constants by adding the decimal, the sum will then be calculated using floating point arithmetic and you will get the expected result

Expand|Select|Wrap|Line Numbers
  1. float num=1/3., sum=0;
  2.  
(Note I added a . the decimal point). Of course by default floating point constants have type double and you are using float. Normally it is best to keep your types the same to prevent conversion although strictly the compiler does all floating point calculations as double and the converts down to float anyway.

Normally we would also say don't bother using float use double unless you have a particularly low memory platform. but if you did want to specify constants with type float you would do it like this

Expand|Select|Wrap|Line Numbers
  1. float num=1.F/3.F, sum=0;
  2.  

2 1408
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. float num=1/3 etc....
  2.  
So, 1/3 is 0 because there are no threes in 1.

Don't mix integer arithmetic with floating point.
May 19 '14 #2
Banfa
9,065 Expert Mod 8TB
Just to be clear the problem in

Expand|Select|Wrap|Line Numbers
  1. float num=1/3, sum=0;
  2.  
Is that 1 and 3 are both integer constants so the calculation is done in integer arithmetic which drops all decimal places therefore 1/3 = 0.

This is easy to fix by forcing 1 or both of the constants to be floating point constants by adding the decimal, the sum will then be calculated using floating point arithmetic and you will get the expected result

Expand|Select|Wrap|Line Numbers
  1. float num=1/3., sum=0;
  2.  
(Note I added a . the decimal point). Of course by default floating point constants have type double and you are using float. Normally it is best to keep your types the same to prevent conversion although strictly the compiler does all floating point calculations as double and the converts down to float anyway.

Normally we would also say don't bother using float use double unless you have a particularly low memory platform. but if you did want to specify constants with type float you would do it like this

Expand|Select|Wrap|Line Numbers
  1. float num=1.F/3.F, sum=0;
  2.  
May 19 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Joecx | last post by:
Does anyone know how to give a date (not today's date) and have the program calculate the previous monday's date from it? Any help would be appreciated. Thanks!!!! Joe
5
by: jbailey006 | last post by:
I am trying to add the following information to a payroll program I am working on for assignment. Modify the Payroll Program so that it uses a class to store and retrieve the employee's name, the...
4
by: damatrix1 | last post by:
28, 32,37,24,33 im not sure how to make the program calculate it
3
by: Starbuck01 | last post by:
I have to write a program for my AP Computer Science Class. Here is the instructions. The Police Department is asking for help in catching those who speed. You will write a batch style program...
2
by: hamady2001 | last post by:
Hello everyone this is my first post and I just started my Python class. I am stuck on an assignment...please help I have to redo the below program (from a prior assignment) to include 2 functions...
14
by: Three Headed Monkey | last post by:
What does bellow C program calculate? I tried my best but couldn't figured it out. #include <stdio.h> main() { long a=1000,b=0,c=2800,d,e=0,f,g; for(;b-c;) f=a/5;...
3
by: strangetorpedo | last post by:
The problem lies under where if (choice == '2') is...I need the program to keep prompting for num and den values as long as the user enters values less than or equal to 0. Once they are values...
1
by: ballpen1019 | last post by:
can someone help me to check any problems in this program...i cant run my program.. /* calculate depreciation using one of three different methods */ #include <stdlib.h> #include <stdio.h>...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.