Register to reply 
Help with c++ program for series 
Share this thread: 
#1
Mar2506, 03:40 PM

P: 28

i wanna form program 2 sum this
pie=4[11/3 +1/5 1/7+..... 1/n] { int n,z,i; float x,y,pie; cin>>n; i=0; while (i<=n) { y=1/((2*i)+1); i=i+2; } for(z=1;z<=n;z=z+2) { x=1/((2*z)+1); } pie=4*(yx); cout<<pie; } cant understand wat to do...this is wat i did but its giving 0 


#2
Mar2506, 03:53 PM

P: n/a




#3
Mar2506, 03:59 PM

P: 28

so will i initialize x nd y first?



#4
Mar2506, 08:25 PM

P: 34

Help with c++ program for series
Yeah, initialize them to zero and make finchie's changes, and it should do what you describe.



#5
Mar2606, 03:48 AM

P: 28

#include <iostream.h>
void main() { int n,z,i; float x,y,pie; cin>>n; i=0; y=0; while (i<=n) { y=y+1/((2*i)+1); i=i+2; } x=0; for(z=1;z<=n;z=z+2) { x=x+1/((2*z)+1); } pie=4*(yx); cout<<pie; } it still wont work 


#6
Mar2606, 08:39 AM

Emeritus
P: 1,919

1) You can write the program using 1 for loop. You don't need to separate the task of getting the negative part and the positive parts of the series.
2) You have typecasting issues. While doing your calculations you need to promote your ints to floats. This is pretty much as tight as you can get it:



#7
Mar2606, 08:46 AM

Emeritus
Sci Advisor
PF Gold
P: 1,817

You are having problems with 'int' to 'float' conversions.
These lines are the problem: y=y+1/((2*i)+1); x=x+1/((2*z)+1); y is a float, but i and z are integers. Because they are integers, the whole section 1/((2*i)+1) is treated like an integer, and equal to 0 for any value of i larger than 0. There are a few ways to get around this. Either create a dummy float 'foo' and then do foo = i; y=y+1/((2*foo)+1); or more cleanly look up what the static_cast command does and change the lines to y=y+(1/((2*static_cast<float>(i))+1)); EDIT: looks like dduardo beat me to it. 


#8
Mar2606, 08:49 AM

P: 28

thanks a lot...i do get it now nd i ll try to form less loops now
thanx a lot for the tip EDIT: plz can u tell wat this statement means j%2 ? pi=1.0/i : pi+=1.0/i ; nd y are we doing it? 


#9
Mar2606, 09:02 AM

Emeritus
P: 1,919

j%2 ? pi=1.0/i : pi+=1.0/i ;
is just a shorthand for:
Also this: 1.0/i is identical to this: 1/(double)i This basically promotes the integer to double 


#10
Aug1008, 08:39 PM

P: 5




#11
Aug1008, 09:40 PM

Sci Advisor
HW Helper
P: 8,955

The for statement is split into three parts with ";" The first part is only executed once before the start of the loop, this creates variables i and j and sets them to 0. The second part "i<n+1" is tested each time through the loop, the loop exits when this is false, so in this case when i is greater or equal to n The third part is executed each time through the loop, it increments i and j for each iteration 


#12
Aug1308, 02:35 AM

P: 5

# include <iostream>
using namespace std; int main(){ int num; cout <<"enter the desired num"; cin >>num ; cout <<"*"<<endl; for (int j=1;j<=num;j++) { for (int i =1;i <=j;i++) if (i%2==0) cout <<" "; else cout <<i<<"*"<<" "; cout<<endl; } return 0; } this is the program and giving the result : * 1* 1*3* 1*3* 5* But the desired result is : * 1* 1*3* 1*3* 1*3*7* 1*3*7*11* PLease help in getting desired result. 


#13
Aug1308, 03:04 AM

HW Helper
P: 2,616

You mean to say you want to produce a series of integers which increments by 2,4,4 then after that it how does it increment? 2,4,4,6,6,6,8,8,8,8...?



#14
Aug1308, 07:13 AM

P: 5

and result should be,,,,,,,,,,,1,3,7,11,15and so on,,,,,,,,,,,,, 


#15
Aug1308, 07:14 AM

P: 5

plz reply me soon.................



#16
Aug1408, 12:40 AM

HW Helper
P: 2,616

Well then that isn't too hard. You need an encompassing for loop for loop to print out all the sequences and within it, you need a cout statement to print out the initial +2 increment and then another for loop to print out the rest, incrementing the variable by +4 each time.



#17
Sep308, 11:15 PM

P: 5

Hi tomorrow i have a test and i need your help........
please helo me in calling a two dimensional array I want two store two values that is height and weight of 10 person in the system by using array array and using switch.As this is a long ques i need help only in this,Please reply me with correct program as soon as possible. # include <iostream> using namespace std; void Array ( int height[],int weight[],int size){ for (int i =0;i<10;i++) { cout <<"enter the height"; cin >> height [i]; cout <<"enter the weight"; cin>>weight[i]; } } double abc(int choice) { switch (choice) { case 1: Array(int height[],int weight[],10); } } int main(){ int choice; cin>>choice; cout <<abc(choice); cout <<endl; return 0; 


Register to reply 
Related Discussions  
Running a Fortran 77 program in a C++ enviorment/program  Programming & Computer Science  2  
Forces problem( help needed)  Introductory Physics Homework  3  
 Please help  easy series  Calculus & Beyond Homework  6  
Quantum Mechanics Homework Help(Urgent Urgent Urgent)!  Advanced Physics Homework  3  
Astro simulation program problem  program bugs?  Astronomy & Astrophysics  4 