Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Please read the entire question carefully. This is one whole question an sho

ID: 3788914 • Letter: C

Question

C++

Please read the entire question carefully. This is one whole question an should be answered completely.

Question:

Write a program that computes the x?th term and summation of the following sequences, where x is a user input. You should write three functions one for each of the following sequences. Each function should show the xth term on console and return value of the summation.


1.

-1; 1; 3; 5; 7;.......
Compute the summation of all the elements between 1st and the xth term. For example, if the user inputs 5 the function should add -1 + 1 + 3 + 5 + 7, show on console the value of the 5th element which is 7, and return 15.

2.

Compute the summation of all the elements between 1st? and the x?th term. For example, if the user inputs 5 the function should add 1?2+22+32?+42+5?2, show on console the value of the 5th element which is 5?2, and return 55.

3.

Explanation / Answer

C++ Program

#include<iostream>
#include<math.h>
using namespace std;
int sequences1(int); // function prototype for first sequence
int sequences2(int); // function prototype for second sequence
double sequences3(int); // function prototype for third sequence
int main()
{
   int xth,result1,result2; // variable decleration
   double result3; // variable decleration
   cout<< "Enter the value of x"<< endl;// command to enter the value of x
   cin >> xth;// get the value of x
   result1 = sequences1(xth);// calling functionsequences1
   result2 = sequences2(xth);// calling functionsequences2
   result3 = sequences3(xth);// calling functionsequences3
   cout << "sum of the first sequence = "<< result1 << endl; // output to the screen
   cout << "sum of the second sequence = "<< result2 << endl;// output to the screen
   cout << "sum of the third sequence = "<< result3 << endl;// output to the screen
   return 0;
}// end of main function

int sequences1(int a) // the function to compute sum of first sequence
{
   int i,e = -1,s = -1;// variable decleration
   for(i = 1;i<a;i++) // loop for x times
   {
       e = e+2; // calculating each element
       s = s+e; // finding summation
   }
   cout << "The "<< a <<"th element is " << e << endl;// output to the screen
   return s;
}
int sequences2(int a)
{
   int i,e = 1,s = 1;// variable decleration
   for(i = 1;i<a;i++)// loop for x times
   {
       e = (i+1)*(i+1); // calculating each element
       s = s+e;// finding summation
   }
   cout << "The "<< a <<"th element is " << e << endl;// output to the screen
   return s;
}
double sequences3(int a)
{
   double e = 1.0,s = 1.0,rt = sqrt(2);// variable decleration
   int i;
   for(i = 1;i<a;i++)// loop for x times
   {
       e = -1*e*rt; // calculating each element
       s = s+e;// finding summation
   }
   cout << "The "<< a <<"th element is " << e << endl;// output to the screen
   return s;
}

OUTPUT

$./a.out
Enter the value of x
5
The 5th element is 7
The 5th element is 25
The 5th element is 4
sum of the first sequence = 15
sum of the second sequence = 55
sum of the third sequence = 2.75736

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote