Question: C++ assignment . Here's the given code I have to complete. #include #include using namespace std; string STUDENT = WHO AM I?; // Add your

C++ assignment. Here's the given code I have to complete.

#include #include using namespace std;

string STUDENT = "WHO AM I?"; // Add your Canvas login name

// Write your function here

/////////////// Optional Student Code ///////////////// int run() {

return 0; }

Here's the problem.

Step 1 Write the Stub or Skeleton Write the function sumNums(), which, when given a string, returns the sum of the numbers appearing in the string, ignoring all other characters. A number is a series of one or more digit characters in a row. Here are some examples:

sumNums("abc123xyz")123 sumNums("aa11b33")44 sumNums("7 11")18

Step 2 Loop Mechanics Now you can make test and at least one of your tests should pass. To solve the rest of them, though, we need a loop. Here's where the loop-building plan you learned comes into play. What is the loop bounds or stopping condition? When we have processed the last character in the string. What are the bounds preconditions? The number of characters to process and an index to process the string. Both must be initialized. What advances the loop? Increment the loop index. Using this plan, we can translate this to code. The for loop is designed for this. The Loop Bounds for ( ? ; i < len ; ? ) . . . Our loop will continue while the loop index, i, is less than the size of the string, len. We'll assume that the string, str, already exists (that is, it is passed as the argument to the function.)

The Bounds Precondition for (size_t i = 0, len = str.size() ; i < len ; ? ) Notice that in the for loop initialization statement we can define two variables, as long as they are both the same type. Putting the "mechanical" variables here keeps them from contaminating the rest of your program; they are local to the for loop. In Java length() returns an int, but in C++ it returns some kind of unsigned integer type. The type returned by size() named string::size_type. However, typing string::size_type, can be tedious and tends to clutter up your code. While we can use decltype() and auto to define those variables, this is unnecessary because of the "magic unsigned type" size_t. This can almost always be used in place of the named types. Advance the loop When using the for loop, advancing the loop is just a matter of updating the loop index in the loop update section like this: for (size_t i = 0, len = str.size(); i < len; i++) // process the characters here Now we're finished with the Loop Mechanics; let's tackle the goal next.

Step 3 The Loop Goal Here are the three steps needed to reach the loop's goal. The Goal Precondition Remember that a loop produces information. Before the loop starts, we want to create variables to hold that information. In our case, we need a variable sum set to zero. We'll also need a temporary variable, which I'll call num, which will hold the "current" number, every time we encounter one. Here's what our code should look like now.

int sum = 0; int num = 0; for (size_t i = 0, len = str.size(); i < len; i++) { // process the characters here }

The Loop Operation This is the hardest part, because your loop's goals can vary radically. Let's spend a few moments planning out what we want to do, and writing our plan as a pseudocode list. Grab the current character. If the character is a digit then convert the character to a decimal digit. Multiply the current value of number by 10. Add the digit to the number Else, Add the number to the sum. Set the number back to 0

Here are some notes on implementing this plan in C++. Use str.at() to grab the current character; store it in a variable Use isdigit() to see if the character is between '0' and '9' inclusive Convert the character to a decimal digit by subtracting '0'. Note that the character has an underlying ASCII code and those codes are sequential. If the character is '0' then subtracting '0' will leave the binary number 0. If the character is '1', then subtracting '0' will leave the binary number 1; and so on. Multiply the current value of num by 10, and then add the digit. For instance, if num has the value 2 and digit has the value 5, then num * 10 = 20 and adding 5 leaves num with 25, which is correct. When you encounter something that isn't a digit, then add the current value of num to sum. Then, set num back to 0 for the next iteration. Go ahead and implement this code. Then do make test. You'll find that some of the tests pass, but others don't.

Do you see any way that the failing tests are different than those that pass? It takes a sharp eye, but if you look closely, all the tests that pass end in a letter, and all the tests that fail end in a digit. That means if we are processing a number when our loop ends, we never add that number to the accumulator. Our loop has satisfied the bounds condition, but hasn't reached its goal. That's the purpose of Step 6 in our loop-building strategy, the Goal Postcondition. The Goal Postcondition After the loop is over, one of two things can be true. If the last character processed is a letter or other non-digit, then the variable num contains the value 0. If the last character was a digit, however, then num contains a non-zero value, which needs to be added to the sum. Of course, since adding zero has no effect on the sum, we can just add num to the sum one last time after the loop ends, and finally reach our goal.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!