An arithmetic series is the sum of a sequence fakg, k = 1; 2; : : : ; in which e
ID: 3640203 • Letter: A
Question
An arithmetic series is the sum of a sequence fakg, k = 1; 2; : : : ;
in which each term is computed from the previous one by adding (or subtracting) a constant d, the common di?erence. Therefore, for k > 1,
ak = ak1 + d = ak2 + 2d = : : : = a1 + d(k 1).
For example, 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 is an arithmetic series
whose first term, a1, is 1, common difference, d is 1, and number of terms, k
is 10. This series is equals to 55.
Write a C++ program that prompts the user to enter the ?rst term of an
arithmetic series, its common di?erence, and the number of terms. Your
program should then compute the series, s(a1; d; k). Name your source ?le
aseries.cpp.
Sample Run 1:
What is the first term of the series? 1
What is the difference between adjacent terms? 1
How many terms does the series have? 0
Error -> The number of terms must be a positive integer.
Sample Run 2:
What is the first term of the series? 7
What is the difference between adjacent terms? 12
How many terms does the series have? 1
s(7,12,1) = 7
Sample Run 3:
What is the first term of the series? 1
What is the difference between adjacent terms? 1How many terms does the series have? 10
s(1,1,10) = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
Sample Run 4:
What is the first term of the series? 3
What is the difference between adjacent terms? -2
How many terms does the series have? 8
s(3,-2,8) = 3 + 1 - 1 - 3 - 5 - 7 - 9 - 11 = -32
Sample Run 5:
What is the first term of the series? -3.75
What is the difference between adjacent terms? 1.25
How many terms does the series have? 7
s(-3.75,1.25,7) = -3.75 - 2.5 - 1.25 + 0 + 1.25 + 2.5 + 3.75 = 0
Here is what I have so far, but I am unable to find/correct the issues:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double startNum, firstTerm, difference, numTerms, sum;
sum = 0;
cout<<"What is the first term of the series? ";
cin>>startNum;
cout<<"What is the difference between adjacent terms? ";
cin>>difference;
cout<<"How many terms does the series have? ";
cin>>numTerms;
if (numTerms <=0)
{
cout<<"Error -> The number of terms must be a positive integer."<<endl;
return 0;
}
else
{
cout<<"s("<<startNum<<","<<difference<<","<<numTerms<<") = ";
int i;
while (i <= numTerms)
{
sum = sum + startNum;
cout<<abs(startNum);
firstTerm = startNum + difference;
if (startNum <= numTerms)
{
if (firstTerm > 0)
{
cout<<" + ";
}
else
{
cout<<" - ";
}
}
}
cout<<" = "<<sum;
}
cout<<endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double startNum, firstTerm, difference, numTerms, sum;
sum = 0;
cout << "What is the first term of the series? ";
cin >> startNum;
cout << "What is the difference between adjacent terms? ";
cin >> difference;
cout << "How many terms does the series have? ";
cin >> numTerms;
if (numTerms <= 0)
{
cout << "Error -> The number of terms must be a positive integer." << endl;
return 0;
}
else
{
cout << "s(" << startNum << "," << difference << "," << numTerms << ") = ";
int i = 1; //initialize i before use it
firstTerm = startNum; //start with firstTerm = startNum
while (i <= numTerms)
{
sum += firstTerm; //use firstTerm, not startNum
cout << abs(firstTerm); //use firstTerm, not startNum
firstTerm += difference;
i++;
if (i <= numTerms) //to prevent a dummy " + " or " - " at the end of the statement
if (firstTerm >= 0) //+ 0 instead of - 0
cout << " + ";
else
cout << " - ";
}
if (numTerms > 1) // to prevent repeating " = " if the serie contains only 1 element
cout << " = " << sum;
}
cout << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.