CONSIDER THE FOLLOWING CODE: double SumString(string x, int n) { double sum = 0;
ID: 3809805 • Letter: C
Question
CONSIDER THE FOLLOWING CODE:
double SumString(string x, int n)
{
double sum = 0;
for (int i = 0; i < n; i += 2)
sum += x.length();
return sum;
}
/////////////////////////////////////////////////////////
void Question()
{
string a = "string";
int num = a.length();
double total = SumString(a, num);
cout << "The total is: " << total << endl;
}
Select one:
a. The total is: 14.0
b. The total is: 20.0
c. The total is: 16.0
d. The total is: 18.0
Please explain how you get the answer. thank you
Explanation / Answer
Answer: d. The total is: 18.0
string a = "string";
a string value is "string"
int num value is 6
when we are this method double SumString(string x, int n) x value is "string" and n value is 6
for loop will run up to i value is less than n value.
Inside for loop we are adding the length of string every time.
when i = 0, sum value is 6
When i = 2, sum value is 12
When i = 4 , sum value is 18
When i = 6, loop will terminate ans return the sum value as 18.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.