The following exercises ask you to create 4 different types of loops %u2013 you
ID: 3538189 • Letter: T
Question
The following exercises ask you to create 4 different types of loops %u2013 you should add these in order to your source file, until you have 4 functioning loops in your code. Direct all output to the console window. Before each type of loop, add documentation to identify the new loop. Also, send a line of output to your console window identifying which loop is creating the next series of prompts and output.
Count-Controlled While Loop:
1. Add the following code for a count-controlled loop to your source file. Save, compile, and run it, and observe the results:
int count = 1;
while (count <= 10)
{
cout << count << %u201C, %u201C;
count = count + 1;
}
2. Modify this segment of code in the following way. Instead of simply printing the numbers from 1 to 10, this while loop should ask the user to enter 10 integer values. The user should be prompted in a meaningful way for each value, such as %u201CEnter number 1:%u201D, %u201CEnter number 2:%u201D, etc. When the values have been entered, print the sum and average of the values.
3. Now modify this code so that the user can specify how many values are to be entered. You%u2019ll need to ask the user for the number of values prior to starting the loop. Your prompts should stay meaningful, and after the values have been entered, print the count, sum, and average of the values entered.
Count-Controlled For Loop:
4. Create a second loop in your program (leave your first loop in your code!). This loop should do exactly what your loop from problems #1-3 does, except this loop must be implemented with a for loop structure. Note that when you run your code, you%u2019ll be entering two lists of numbers as the user.
Sentinel-Controlled While Loop:
5. Create a third loop in your program. This loop should be a sentinel-controlled while loop; this loop should prompt the user for any number of integers until a value of 0 is entered. Declare and use a global constant for this sentinel value. Recall that the general algorithm for a sentinel-controlled loop is as follows:
get the first value from the user (this is the %u201Cpriming read%u201D)
while (the value entered by the user is NOT the sentinel value)
{
process the value
get the next value from the user
}
Once again, after all values have been entered, this program should print the count, sum, and average of the numbers.
Flag-Controlled While Loop:
6. Create a fourth loop in your program. This should be a flag-controlled loop that uses a boolean variable to control the loop. This loop should ask the user for numbers, one integer at a time. After entering a value, ask the user if they would like to continue (the user could enter a y or n to indicate whether or not they wish to continue). Print the same results as before %u2013 the count, sum, and average of the values entered.
Explanation / Answer
#include <iostream>
#include <string>
#define SENTINEL 0
using namespace std;
int main()
{
//Count-Controlled While Loop:
cout<<"Count-Controlled While Loop:"<<endl;
int count=1,tmp,values;
int sum=0;
double avg;
cout<<"Enter Number of values to be entered: ";
cin>>values;
while (count <= values)
{
cout<<"Enter number "<<count<<": ";
cin>>tmp;
sum=sum+tmp;
count=count+1;
}
avg=(double)(sum/(count-1.0));
cout<<"Count: "<<count<<endl;
cout<<"Sum: "<<sum<<endl;
cout<<"Average: "<<avg<<endl;
cout<<endl;
//Count-Controlled For Loop:
cout<<"Count-Controlled For Loop:"<<endl;
sum=0;
cout<<"Enter Number of values to be entered: ";
cin>>values;
for(count=1;count <= values;count++)
{
cout<<"Enter number "<<count<<": ";
cin>>tmp;
sum=sum+tmp;
}
avg=(double)(sum/(count-1.0));
cout<<"Count: "<<count<<endl;
cout<<"Sum: "<<sum<<endl;
cout<<"Average: "<<avg<<endl;
cout<<endl;
//Sentinel-Controlled While Loop:
cout<<"Sentinel-Controlled While Loop:"<<endl;
sum=0;
count=1;
cout<<"Enter number "<<count<<": ";
cin>>tmp;
while(tmp!= SENTINEL )
{
sum=sum+tmp;
count=count+1;
cout<<"Enter number "<<count<<": ";
cin>>tmp;
}
avg=(double)(sum/(count-1.0));
cout<<"Count: "<<count<<endl;
cout<<"Sum: "<<sum<<endl;
cout<<"Average: "<<avg<<endl;
cout<<endl;
//Flag-Controlled While Loop:
cout<<"Flag-Controlled While Loop:"<<endl;
bool flag=true;
char choice;
count=1;
sum=0;
while(flag)
{
cout<<"Enter number "<<count<<": ";
cin>>tmp;
sum=sum+tmp;
cout<<"Would you like to continue? (y/n) :";
cin>>choice;
if(choice=='y') { flag=true; }
else if(choice=='n') { flag=false; }
count=count+1;
}
avg=(double)(sum/(count-1.0));
cout<<"Count: "<<count<<endl;
cout<<"Sum: "<<sum<<endl;
cout<<"Average: "<<avg<<endl;
cout<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.