Question

In: Computer Science

This is for C++ Assuming the value of a is already defined elsewhere, convert the following...

This is for C++ Assuming the value of a is already defined elsewhere, convert the following code snippet from using a WHILE loop to using a DO-WHILE loop. Please paste in the modified code in the text area below.

int k = 1; while( k <= 8 ) { a = a * k; k++; }

Solutions

Expert Solution

The equivalent do-while() loop is given below:

do
{
if(k>8)
break;
  
a = a * k;
k++;
}
while(true);

The screenshot of the above source code is given below:

Types of loops:

  1. Entry controlled loops
  2. Exit controlled loops

1. Entry controlled loops:

The loop which checks for the exit condition before each iteration is known as entry controlled loops.

For example:

while(condition)

{

//statements

}

The while loop will be executed until the condition is true and will exit when the condition will be false.

This loop should be used when we don't know the number of iteration in advance but a condition is defined and the required statement must not execute for a single time if the condition is false.

2. Exit controlled loops:

The loop which checks for the exit condition after each iteration is known as exit controlled loops.

For example:

do

{

//statements

} while(condition)

The do-while loop will be executed until the condition is true and will exit when the condition will be false.

This loop should be used when we don't know the number of iteration in advance but a condition is defined and the required statement must execute at least once even if the condition is false.


Related Solutions

Assume the following functions have already been defined, write a main() using c++ that uses them...
Assume the following functions have already been defined, write a main() using c++ that uses them to fill a vector with random integers, remove the smallest and largest integers from the vector, save the result in a file called "trimmed.txt" void fillRandom(vector & nums, int howMany); // fill with specified number of integers int minValue(vector nums); // return smallest value in vector int maxValue(vector <;int> nums); // return largest value in vector void writeFile(string outFileName, vector nums ); // writes...
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
1. Value the following scenario, assuming there is no end to the timeline and the following...
1. Value the following scenario, assuming there is no end to the timeline and the following data: Cost of equity = 17.75% Cost of debt = 6.83% Debt = $583MM Equity = $1237MM Tax rate = 40% Long-term growth expectations = 3.6% Future dividends are forecast as follows: Year 0: n/a Year 1: 128 Year 2: 149 Year 3: 162 Year 4: 175 Year 5: 182 (Round your answer to the nearest cent) 2. Use the data below to compute...
I need to convert the following into C++. The general idea is to create an application...
I need to convert the following into C++. The general idea is to create an application that can support 10 ID's and 10 grades. It needs to calculate the grade average of said ID, and determine if it is an below or above a certain average, ergo A or C. string[10] studentIDArray int[10] gradeArray int averageGrade for(int i = 0; i < gradeArray; i++) { averageGrade += gradeArray[i] } averageGrade = averageGrade / sizeof(gradeArray) for(int i = 0; i <...
c)Assuming that ROCE (return on common equity), g (the growth rate of the book value of...
c)Assuming that ROCE (return on common equity), g (the growth rate of the book value of common shareholders’ equity) and rE (the cost of equity capital) are constant, that markets are efficient, and:the company’s dividend payout ratio d is 20%,g is 8%,the company’s stock has an equity beta of 1.2,the risk free rate is 1% and the market risk premium is 6%, what is the ROCE priced into the market? Continuing with the information given in part (c), what will...
Do only in C# (Not in C++ or C ) Minimum number of operations to convert...
Do only in C# (Not in C++ or C ) Minimum number of operations to convert array M to array N by adding an integer into a subarray Given two arrays M and N , the task is to find the minimum number of operations in which the array M can be converted into array N where each operation consists of adding an integer K into a subarray from L to R. M= {3, 7, 1, 4, 1, 2}, N...
Assuming that the transaction for a bank loan has already been recorded, if a company takes...
Assuming that the transaction for a bank loan has already been recorded, if a company takes the proceeds from a bank loan and uses it to purchase merchandise, which of the following effects is true for this transaction? Expenses increase. Assets do not change. Liabilities increase. Assets increase. Liabilities decrease. Assets decrease.
How can I convert inRange in cv2 to PIL? For example, I already have mask =...
How can I convert inRange in cv2 to PIL? For example, I already have mask = cv2.inRange(img, array1, array2) but I'm trying to keep everything in PIL. Any ideas would be appreciated!
Which of the following is wrong? a)The p-value is a probability. b)The p-value is calculated assuming...
Which of the following is wrong? a)The p-value is a probability. b)The p-value is calculated assuming that the null hypothesis is true. c)The p-value is the probability that H0 is true. d)The p-value can be different for different samples in the same test.
Convert the following C++ code into MIPS assembely. For testing I will be change the values...
Convert the following C++ code into MIPS assembely. For testing I will be change the values for q,y,x with few different values. //q -> $s0 //y -> $s1 //x -> $s2 int main(){ int q = 5; int y = 17; int x = 77; if ( q < 10){ cout << "inside if"; } elseif ( x > 0 || y < 10) { cout << "inside elseif"; } else { cout << "inside else"; } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT