All programming languages have a loop capability. Looping is the program logic’s ability to repeat one or more lines of code either:A set number of times, a “for loop” (i.e. 10, 100, 500, 763 or more times)An “unknown” number of times (i.e. loop terminates if a tested condition becomes “true” or as long as the tested condition remains “false”), a “while” loop, a “do until” loop, a “do while” loop. This Lab exercise demonstrates the use of the “For Loop”, a standard loop in all programming languages. C++ standard For Loop format looks like:                For (n = 1; n <= 100; n++)            {                        Loop line 1 code;                        Loop line 2 code;                        Loop line 3 code;                        Loop line N code;            }   The loop code needs:A “loop counter variable” (n in the above example) to keep track of how many iterations this loop has runA starting value (i.e. 1 in the above example)A stop test (i.e. run as long as the loop counter variable is 100 or less in the above example) The loop will start at 1 and increment by 1 until the counter exceeds 100. The loop will then stop at that point. We can substitute variables for the start and stop numeric constant values such as:for (n = 1; n <= NumberofTimes; n++){            Lines of Loop code;} This Lab Assignment will allow the user to:Enter a positive number (we will dispense with GIGO code)Run a For Loop from 1 to N times keeping a running count of the loop iteration values (i.e. 1 + 2 + 3 + 4 + 5 + 6 +…+ N) Display the number of iterations and the total. You might also wish to display the iteration count as the loop progresses: You ran the for loop 5 times1 + 2 + 3 + 4 + 5 = 15

Question
Asked Oct 13, 2019
5 views

All programming languages have a loop capability. Looping is the program logic’s ability to repeat one or more lines of code either:

  1. A set number of times, a “for loop” (i.e. 10, 100, 500, 763 or more times)
  2. An “unknown” number of times (i.e. loop terminates if a tested condition becomes “true” or as long as the tested condition remains “false”), a “while” loop, a “do until” loop, a “do while” loop.

 

This Lab exercise demonstrates the use of the “For Loop”, a standard loop in all programming languages. C++ standard For Loop format looks like:    

            For (n = 1; n <= 100; n++)

            {

                        Loop line 1 code;

                        Loop line 2 code;

                        Loop line 3 code;

                        Loop line N code;

            } 

 

The loop code needs:

  1. A “loop counter variable” (n in the above example) to keep track of how many iterations this loop has run
  2. A starting value (i.e. 1 in the above example)
  3. A stop test (i.e. run as long as the loop counter variable is 100 or less in the above example)

 

The loop will start at 1 and increment by 1 until the counter exceeds 100. The loop will then stop at that point. We can substitute variables for the start and stop numeric constant values such as:

for (n = 1; n <= NumberofTimes; n++)

{

            Lines of Loop code;

}

 

This Lab Assignment will allow the user to:

  1. Enter a positive number (we will dispense with GIGO code)
  2. Run a For Loop from 1 to N times keeping a running count of the loop iteration values (i.e. 1 + 2 + 3 + 4 + 5 + 6 +…+ N)

 

Display the number of iterations and the total. You might also wish to display the iteration count as the loop progresses:

 

You ran the for loop 5 times

1 + 2 + 3 + 4 + 5 = 15

check_circle

Expert Answer

Step 1

Program:

//inlcude the header files

#include <iostream>

using namespace std;

 

//definition of main method

int main()

{

    //declare the required variables

    int n, sum = 0;

 

    //get the input from the user

    cout << "Enter a positive integer: ";

    cin >> n;

 

    //display the number of iterations

    cout << "You ran the for loop " << n << " times\n";

 

    //iterate 1 to n numbers

    for (int i = 1; i <= n; ++i)

    {

        //add the values

        sum += i;

 

        //display the "i"

        cout << i << " + ";

       

    }

 

    //remove the last "+" in the above statement

    cout << '\b' << " " << '\b'<< '\b';

 

    //display the sum

    cout << " = " << sum;

 

    //return statement

    return 0;

}

 

Step 2

Screenshot:

...
help_outline

Image Transcriptionclose

1/inlcude the header files #include <iostream using namespace std; definition of main method int main /declare the required variables int n, sum 0; 1/get the input from the user cout <"Enter a positive integer: "; cin >>n; /display the number of iterations cout << "You ran the for loop <<n << times\n" /iterate 1 ton numbers for (int i 1; i n; +i) add the values sum i; /display the 1 cout << i <"" /remove the last "+in the above statement cout << \b « «b'< \b'; 1/display the sum <<sum; cout << /return statement return e

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: I am having trouble making this code work in C++. int main() {int songNum; songNum = 5;cout &lt;&lt;...

A: Corrected code:Note: The modified statement is highlighted.#include &lt;iostream&gt;using namespace ...

question_answer

Q: Using the types of entities found in the Colonial Adventure Tours database (trips, guides, clients, ...

A: The Set of entities provided for Colonial Adventure Tours Database includes Trips, Guides, Clients a...

question_answer

Q: A bag of cookies holds 40 cookies. The calorie information on the bag claims that there are 10 servi...

A: Program AlgorithmDeclare the required variable in the main().Get the input from the user for the num...

question_answer

Q: 1.

A: There are different business rules given in the question which form the entities like Volunteer, Are...

question_answer

Q: First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ...

A: Create a JAVA class of name AtmSimulator. Import DecimalFormat class to display balance up to 2 deci...

question_answer

Q: In C++ 1.  Create function called DoubleFillArray that takes in an empty array of size 100 and fills...

A: Program Instructions:In function DoubleFillArray, use rand() method to fill random numbers with doub...

question_answer

Q: Create a conditional expression that evaluates to string "negative" if userVal is less than 0, and "...

A: To create a conditional expression, by adding some code statements to the given program code to chec...

question_answer

Q: In C++ Create a function called QuadraticFormula that takes in a,b, and c and does the quadratic for...

A: Program AlgorithmRead the value of coefficients and call the function QuadraticFormula (num1, num2, ...

question_answer

Q: Suppose that a maximum flow network contains a node, other than the source node, without incoming ar...

A: Maximum flow problem:“Yes”, it is possible if the maximum flow network does not contain source node ...