Question

In: Computer Science

(1) Explain that a counter-controlled loop is useful when you know how many times a set...

(1) Explain that a counter-controlled loop is useful when you know how many times a set of statements needs to be executed.

(2) Discuss the implementations of the Fibonacci Number program at the end of this section to consolidate the students’ understanding of the while loop repetition structures.

Solutions

Expert Solution

Counter controlled loop:

The loop which is controlled by counter variable.

For example if we want to print 1 to 10 numbers, we can use while loop or  for loop .

WHILE loop:

Initially we have to assign initial value to counter variable, and condition is placed in while loop upto what value we have to continue loop. Inside the loop we have to increment or decrement the counter variable.

//C Code for printing 1 to 10 numbers using while loop

int main()

{

int counter =1; //Initally counter=1

while(counter<=10) // loop runs until value of counter is less than or equal to 10

{

printf(" %d ",counter);

counter++;

}//loop exits when the value of counter becomes 11.

}//close main function

Another example to print message "Hello" for 5 times

counter=1;

while(counter<=5)

{

printf("Hello");

counter=counter+1;

}

2) Printing n th  Fibonacci number

Fibo(n)=Fibo(n-2)+Fibo(n-1),

N th  Fibonacci number is sum of its two previous fibonacci numbers in the series

Initially first Fibonacci number A=1, Second Fibonacci Number B=2 , then third Fibonacci number C is A+B.

To compute 4th Fibonacci number, we have to update the values of A and B

put B value in A, Put C value in B and new Fibonacci number is A+B

int main()
{

int A=1,B=2,C, counter=1;

while(counter<=10)

{

C=A+B;

printf("%d ", C);

// Now update A and B values

A=B;

B=C;

counter=counter+1;

}

return 0;

}

For any queries, post in comment section.


Related Solutions

Summary In this lab, you use a counter-controlled while loop in a Java program provided for...
Summary In this lab, you use a counter-controlled while loop in a Java program provided for you. When completed, the program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. The data file contains the necessary variable declarations and some output statements. Instructions Ensure the file named Multiply.java is open. Write a counter-controlled while loop that uses the loop control variable to take on the values 0 through 10. Remember to initialize...
Determine how many times the innermost loop will be iterated when the algorithm segment is implemented...
Determine how many times the innermost loop will be iterated when the algorithm segment is implemented and run. (Assume that m and n are positive integers.) for j := 1 to m     for k := 1 to n         [Statements in body of inner loop.         None contain branching statements that         lead outside the loop.]     next k next j
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of...
How many times will the following while loop iterate? int i = 1; while (i <...
How many times will the following while loop iterate? int i = 1; while (i < 5) {     i = i + 1;     System.out.println(“Hello!”); } Group of answer choices 4 0 5 It will iterate infinitely
How many times will an "X" appear in the following nested loop? for (outer = 0;...
How many times will an "X" appear in the following nested loop? for (outer = 0; outer <=5; outer++) {for (inner = 1; inner <=4; inner+1) {cout<<("X");} } 9 10 20 24 none of the above
C# programming When a loop might execute many times, it becomes increasingly important to consider the...
C# programming When a loop might execute many times, it becomes increasingly important to consider the number of evaluations that take place. How can considering the order of evaluation of short-circuit operators affect the performance of a loop?
Explain the similarities and differences of counter-controlled iteration and sentinel-controlled iteration. Also, give an example of...
Explain the similarities and differences of counter-controlled iteration and sentinel-controlled iteration. Also, give an example of how each would be used in a computer program.
3. (5pts.) When should a Count controlled loop be used? C++
3. (5pts.) When should a Count controlled loop be used? C++
How do you know the income is useful for investors?
How do you know the income is useful for investors?
Think back to when you were becoming an RN and how many times you were told...
Think back to when you were becoming an RN and how many times you were told to be an advocate for your patient. discuss how you as a nurse can advocate for ethical policies that promote access, equity, quality, and cost.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT