The given questions are basically loops.
Question 7)
Here, a while loop has been implemented. The general structure of the a while loop is:
while (loop conditions)
{
...code inside loop...
}
Here, first the loop condition will be checked. If the condition satisfies then the code inside the loop will be executed. After the execution of the code inside the loop, the condition will be checked again and if satisfies then the inside code will be executed. Thus, till the loop condition fails or becomes FALSE, the code inside loop will be under execution. So, it is better to be cautious to update the loop condition in the insider code, else it may fall in an infinite loop.
The given problem:
While (sum<100) or (flag=1)
add 5 to sum
endWhile
Comparing the general notation of while loop, here, the conditions of the while loop are: sum<100 OR flag=1. Since OR is there, if any of the condition satisfies then the loop condition will be consider to be TRUE. Inside the loop, 5 is added to sum (a variable). Thus, till sum is less than 100 OR flag is equal to 1, 5 will be added to sum variable continuously. Obviously it depends on initial value of sum too. If initial value of sum is more than or equal to 100 and flag is not equal to 1 then the loop will not execute for once. But if sum is less than 100 and even if flag is not equal to 1, the loop will go on till the condition satisfies (since OR is there).
It has to notice that flag is not being updated inside the loop, but sum is being updating. So if flag is equal to 1 before entering the loop, the while loop will be under execution for infinite time. Since flag=1 will always satisfy, the loop condition will become independent of the value of sum due to presence of OR. Only if flag is not equal to 1 then the loop will terminate if sum is not less than 100.
Thus, it is better to update the flag variable inside the loop in order to avoid infinite loop. Also flag is no updated inside the loop code, so it can be ignored in the condition in order to avoid the infinite loop. In that case, the new while loop condition will look like: while (sum<100)
Question 11)
Here, for loop has been implemented. It is different from while loop. In while loop, the condition is checked first, and then the rest of the work is done. But in for loop, a loop variable is initialised inside for the first time (in general) and after that the variable is being updated till it fails the condition of the loop. Till the condition is satisfied, the code inside the loop will be under execution.
for( initialization; loop condition check; loop variable update)
{
...code inside loop...
}
Generally, a loop variable is used to check the loop condition in for loop. First it is initialised, whether inside the for notation or outside. Then the loop condition is checked and goes to execute the codes inside the loop if the condition is TRUE. After that the loop variable is updated. The loop goes on till the condition is satisfied.
The given problem:
for count =20 down to 1
add 1 to value
endFor;
Thus, in terms of C language, the equivalent for loop would look like:
for (count=20 ; count >=1 ; count--)
{
value=value+1;
}
Thus, the loop variable here is count, which is initialised to 20. The loop condition will then checked, which is count>=1 or down to 1. Lastly, each time the loop is being executed, the count variable or loop variable is decreased by 1. Inside the loop, the 1 is added to variable value each time.
This loop will not create any problem like infinite loop since the loop variable is being updated and only one condition is there, count>=1. Though the final value of variable 'value' will depend on its initial value. For 20 times, 1 will be added to variable value here. For example, if initially, value=10, then after the execution of the loop, the new value will become, value=10+20=30.
Q7. While (sum <100) or (flag - 1) add 5 to sum endWhile 911. for count...
using C++
1. Convert the following while loop into a for loop: int count = 0; while (count < 10) x = count + sin(x); y = count + cos(y); count++;
Thie What is the output of the following program segment? int count 1; do System.out.print(count (count-) + while(count++ <= 5);
Convert the following while loop into a for loop. int 1 - 50: int sum-07 while (sum < 1000) sum - sum + 1; Question 35 (2 points) Saved Given an int variable k that has already been declared, use a while loop to print a single line consisting of 80 dollar signs. Use no variables other than k. int sum = 0; for(int i = 100;sum < 10000;1-- sum = sum + i;
What is the output of the following C++ code? int count = 1; int num = 25; while (count < 25) { num--; count++; } cout << count « " " « num << endl i 25 1 0 24 1 0 25 0 0 24 0 In this while loop statement, while (counter < 10), where counter is an int variable. Which statement below is an equivalent way to write this while statement? while (10 < counter) while (9...
trace
e following C++ program and provide the correct output: int x = 20, count=0; do { cout << x; x = 4* (x + 5)/5; count++; }while (count < 2);
1) y < 5x +5 645 4 13 다. X
One characteristic measured about high schools is the percent
free lunch, which is the percentage of the student body that is
eligible for free and reduced-price lunches. The top 100 schools,
grouped according to their percent free lunch, is as follows.
Percent free
lunch (x)
Number of top
100
ranked high schools
46
20
12
10
12
If stratified random sampling with proportional allocation is
used to select a sample of 25 high schools, how many would be
selected...
Suppose sin 0 - 5 and 0<o<". Determine sin(20). DO NOT use a calculator
A)
B)
C)
1 Find the Laurent series for 22 +22 for 0 < 121 < 2 Find the Laurent series for (z+2)}(3-2) for 2 – 3) > 5 1 Find the Laurent series for z2(z-i) for 1 < 12 – 11 < V2
A. What is the output of the following C++ code fragment? (all variables are of type int) int count-1; int y-100; while (count 3) y=y-1 ; count+t cout << y << endl; cout<< count <<endl What is the value of x after control leaves the following while loop? (all variables are of type int) B. int x0 while (x < 20) sum- sum+x cout << sum<< endl;