Help with c++ program for series

In summary, you need to create a two dimensional array to store the height and weight of 10 people using the switch statement. You can do this by first creating a function that takes in two arrays as parameters and then using a nested for loop to input the values for each person. The switch statement can then be used to call this function.
  • #1
Isma
27
0
urgent help with c++ program for series

i want to form program 2 sum this
pie=4[1-1/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*(y-x);
cout<<pie;
}



cant understand wat to do...this is wat i did but its giving 0
 
Technology news on Phys.org
  • #2
Isma said:
y=1/((2*i)+1);
x=1/((2*z)+1);
I don't know much about programming, but shouldn't the two lines above be:
Code:
 y = y + 1/((2*i)+1);
x = x + 1/((2*z)+1);
otherwise, you won't get the sum of the terms, and so y = x, and so you get pie to be 0.
 
Last edited by a moderator:
  • #3
so will i initialize x nd y first?
 
  • #4
Yeah, initialize them to zero and make finchie's changes, and it should do what you describe.
 
  • #5
#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*(y-x);
cout<<pie;
}
it still won't work
 
  • #6
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:

Code:
#include <iostream>

using namespace std ;

int main( void ) {
  int n; double pi=0 ;
  cin >> n ;
  for( int i=1, j=0 ; i<n+1; i+=2, j+=1 ) {
    j%2 ? pi-=1.0/i : pi+=1.0/i ;
  }
  cout << 4*pi << endl ;
  return 0 ;
}

If you don't understand something in the above code, don't hesitate to ask.
 
  • #7
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
thanks a lot...i do get it now nd i ll try to form less loops now
thanx a lot for the tipEDIT: please can u tell wat this statement means
j%2 ? pi-=1.0/i : pi+=1.0/i ;
nd y are we doing it?
 
Last edited:
  • #9
j%2 ? pi-=1.0/i : pi+=1.0/i ;

is just a shorthand for:

Code:
if(j%2){
  pi-=1.0/i ;
} else {
  pi+=1.0/i ;
}

The % operator is called the modulus operator. It gives you the remainder. In this case j%2 gives you the remainder of j divided by 2. If the reminder is 0, then you know the number is even, else it is odd.

Also this:

1.0/i

is identical to this:

1/(double)i

This basically promotes the integer to double
 
Last edited:
  • #10


dduardo said:
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:

Code:
#include <iostream>

using namespace std ;

int main( void ) {
  int n; double pi=0 ;
  cin >> n ;
  for( int i=1, j=0 ; i<n+1; i+=2, j+=1 ) {
    j%2 ? pi-=1.0/i : pi+=1.0/i ;
  }
  cout << 4*pi << endl ;
  return 0 ;
}

If you don't understand something in the above code, don't hesitate to ask.
Hi i am new user of c++ language and i didnt get how this loop is working.
 
  • #11


charu said:
Hi i am new user of c++ language and i didnt get how this loop is working.

for( int i=1, j=0 ; i<n+1; i+=2, j+=1 ) {
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


# 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


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...?

1*3*
1*3*
There is a repeated line here, is that intentional?
 
Last edited:
  • #14


Defennder said:
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...?

There is a repeated line here, is that intentional?

no i want series should be produce in 2,4,4,4,4,4,4,4, increment...
and result should be,,,,,,,,,,,1,3,7,11,15and so on,,,,,,,,,,,,,
 
  • #15


please reply me soon....
 
  • #16


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


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 ;
cout <<"enter the weight";
cin>>weight;

}
}
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;
 

1. What is a series in C++?

A series in C++ refers to a sequence of numbers or characters that follow a specific pattern or rule. It can also be defined as a set of values that are generated through a mathematical or logical operation.

2. How do I write a C++ program for a series?

To write a C++ program for a series, you will need to first understand the pattern or rule that the series follows. Then, you can use variables, loops, and conditional statements to generate the series and output the desired result.

3. Can you provide an example of a series program in C++?

Sure, here is a simple C++ program for printing the Fibonacci series (0, 1, 1, 2, 3, 5, 8, 13, 21, ...):

#include <iostream>using namespace std;int main() {    int n, num1 = 0, num2 = 1, nextNum;    cout << "Enter the number of terms: ";    cin >> n;    cout << "Fibonacci Series: ";    for (int i = 1; i <= n; ++i) {        cout << num1 << ", ";        nextNum = num1 + num2;        num1 = num2;        num2 = nextNum;    }    return 0;}

4. How can I improve the efficiency or speed of a series program in C++?

One way to improve the efficiency of a series program in C++ is to use a more efficient algorithm or data structure. For example, in the Fibonacci series program above, the use of a recursive function can significantly slow down the execution. Hence, it can be optimized by using an iterative approach instead.

5. Are there any built-in functions or libraries in C++ for working with series?

Yes, there are several built-in functions and libraries in C++ that can be used for working with series, such as the <cmath> library for mathematical operations, the std::vector container for storing series of values, and the std::accumulate function for calculating the sum of a series.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
660
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
4
Views
498
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
705
Back
Top