patterncppMinor
Arithmetic Progression
Viewed 0 times
progressionarithmeticstackoverflow
Problem
I had to write a program that would complete an arithmetic progression based on the users input. The user would provide the third term in the series, the third from the last term, and the sum of all the terms in the series. The program would display the total number of terms are in the series and then the series itself. I already submitted it but I am hoping to get some general feedback.
Example: 3, 8, 55
Program output:
10
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Example: 3, 8, 55
Program output:
10
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
#include
#include
int main() {
int thirdTerm;
int thirdLastterm;
int sum;
int total;
int constantNumber = 1;
int a = 0;
std::vector vSeries;
while (std::cout > thirdTerm)) {
std::cin.clear(); //clear bad input flag
std::cin.ignore(std::numeric_limits::max(), '\n'); //discard input
std::cout > thirdLastterm)) {
std::cin.clear(); //clear bad input flag
std::cin.ignore(std::numeric_limits::max(), '\n'); //discard input
std::cout > sum)) {
std::cin.clear(); //clear bad input flag
std::cin.ignore(std::numeric_limits::max(), '\n'); //discard input
std::cout 100000) {
std::cout << "There doesn't appear to be a solution based on the input - exiting program.";
return 0;
}
} while (vSeries[vSeries.size() - 3] != thirdLastterm || total != sum);
std::cout << "\nNumber of terms in the series: " << vSeries.size() << std::endl;
for (int i = 0; i < vSeries.size(); i++) {
if (i == 0)
std::cout << vSeries[i];
else
std::cout << "," << vSeries[i];
}
return 0;
}Solution
A couple of things I noticed:
Here:
Aside from the prompt string this is repeated three times. This would be better if it was in a separate function that accepted the prompt string and the address of the variable to store the input value:
Here:
You're iterating through the vector getting a new total. It would be better to just add each element to a running total:
Here:
while (std::cout > thirdTerm)) {
std::cin.clear(); //clear bad input flag
std::cin.ignore(std::numeric_limits::max(), '\n'); //discard input
std::cout << "Invalid input; please re-enter.\n";
}Aside from the prompt string this is repeated three times. This would be better if it was in a separate function that accepted the prompt string and the address of the variable to store the input value:
void GetInput(string prompt, int& input)
{
while (std::cout > input))
{
std::cin.clear(); //clear bad input flag
std::cin.ignore(std::numeric_limits::max(), '\n'); //discard input
std::cout << "Invalid input; please re-enter.\n";
}
}
GetInput("Please enter the third term in the series: ",thirdTerm);
GetInput("Enter the third from the last term in the series: ",thirdLastterm);
GetInput("Please enter the sum of the series: ",sum);Here:
do {
total = 0;
vSeries.push_back(thirdTerm + (a * constantNumber));
a++;
for (int i = 0; i < vSeries.size(); i++) {
total += vSeries[i];
}
} while (total < sum);You're iterating through the vector getting a new total. It would be better to just add each element to a running total:
total = 0;
do {
int temp = thirdTerm + (a * constantNumber);
vSeries.push_back(temp);
total += temp;
a++;
} while (total < sum);Code Snippets
while (std::cout << "Please enter the third term in the series: " && !(std::cin >> thirdTerm)) {
std::cin.clear(); //clear bad input flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //discard input
std::cout << "Invalid input; please re-enter.\n";
}void GetInput(string prompt, int& input)
{
while (std::cout << prompt && !(std::cin >> input))
{
std::cin.clear(); //clear bad input flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //discard input
std::cout << "Invalid input; please re-enter.\n";
}
}
GetInput("Please enter the third term in the series: ",thirdTerm);
GetInput("Enter the third from the last term in the series: ",thirdLastterm);
GetInput("Please enter the sum of the series: ",sum);do {
total = 0;
vSeries.push_back(thirdTerm + (a * constantNumber));
a++;
for (int i = 0; i < vSeries.size(); i++) {
total += vSeries[i];
}
} while (total < sum);total = 0;
do {
int temp = thirdTerm + (a * constantNumber);
vSeries.push_back(temp);
total += temp;
a++;
} while (total < sum);Context
StackExchange Code Review Q#155807, answer score: 2
Revisions (0)
No revisions yet.