Sunteți pe pagina 1din 5

Qatar University

College of Engineering
Dept. of Computer Science & Engineering

Computer Programming -GENG106


Lab Handbook
Fall 2012 Semester

Lab6: More on Loop Structures


Objectives:
At the end of this lab, you should be able to
Use for statements.
Use do...while statements.

Quick Review:

for syntax
for ( variable initialization; condition; variable update)
Statement;

dowhile syntax
do Statement;
while (Condition) ;

Note: The Statement is a simple statement or a block of statements (a compound statement)

1) Run the following program


#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<10;i++)
if(i%2==1)
cout<<i<<endl;
return 0;
}

The output of this program is


___________
___________
___________
___________
___________

Modify the for statement by getting rid of the if statement and keep the same output.

Modify your program to print the same output but in descending order.

2) The factorial of a nonnegative integer

is written as

and is defined as follow:

For example
Write a C++ program that reads a nonnegative integer, then use a for loop to compute and print
its factorial.
3) Eulers number, , is the base of the natural logarithm, and is widely used in engineering,
science and business. The value of can be approximated using the formula

Write a C++ program that asks the user to enter the value of (where
), and then uses a for
loop to calculate the sum of the first terms of . Your program should produce intermediate
values of the sum; similar to the following:

Hint: To format your output as in the above screenshot, you have to include iomanip header file
(add the line #include <iomanip> to the top of your program). The following statement was used to
produce the above lines of i (the loop counter) and e (the sum of terms):
cout<<setw(4)<<setprecision(0)<<fixed<<i<<setw(25)<<setprecision(18)<<fixed<<e<<endl;

The used stream manipulators have the following meanings:


setw(width) : used to set the field width on which the variable i should be printed to the
value width (right-aligned).
setprecision(digits): sets the number of decimal digits for the numeric output following it.
fixed: is used to set the number format to fixed-point. For scientific format use scientific
instead.

Now, run your program once with n=10, and once with n=11. Which value produces a more
accurate result? Compare your answers with the value obtained using MS-Windows Calculator :
e= 2.7182818284590452353602874713527
1

What is the name of the built-in function for obtaining ? _________________________

4) Using nested for loops, write a C++ program that produces the following output:
1
22
333
4444
55555

5) Write a C++ program that finds and prints all the prime numbers between 2 and 100.

6) The diameter of some copper pipes are measured and recorded to the nearest tenth of a
millimeter, as detailed in the table below:
Diameter , d (in mm)
35.6
35.7
35.8
35.9

Frequency, f
2
4
1
6

Write a C++ program that reads two numbers at a time representing the diameter and its
frequency. Then calculate and print the mean diameter. The formula for the mean is
where the value

is the number of entries in the above table. Use a for loop.

Output sample:
User Input

7) The following program uses a dowhile loop to display a menu of three options (1, 7 and x) to
the user. A switch statement is used to display another message based on user selection. Run and
test the program.
#include <iostream>
using namespace std;
int main()
{
char choice='X';
do {
cout<<"\n Select a Major from the following list";
cout<<"\n ======================================\n\n";
cout<<" (1) Chemical Eng.\n";
//add more majors here
cout<<" (7) Electrical Eng.\n\n";
cout<<" (x) Exit\n\n";
cout<<" Major?"; cin>>choice;
switch(choice)
{
case '1': cout<<" Code: CHME\n"; break;
//add more cases here
case '7': cout<<" Code: ELEC\n"; break;
case 'x':
case 'X': cout<<" ===== GOOD BYE ======\n"; break;
default:cout<<" Invalid choice. Try again \n";
}
} while (tolower(choice)!='x');
//tolower converts 'X' to 'x'.
return 0;
}

Now modify the above program so that the menu options are similar to the one displayed in
the following screenshot. Also update the switch statement to display the related major code.

Hint: Major Code (in same order): CHME, CVEN, CMPS, CMPE, IENG, MECH and ELEC

8) The power p dissipated in a resistor of resistance R when a current i flows throw it is given by
p=i2R (see Lab-1). Write a C++ program that reads the resistance R (in ohms) and then outputs p
for all i values between 0 and 5, inclusive, in intervals of 0.5 Amps. Use a for loop.

9) The expansion of a steel bridge as it is heated to a final Celsius temperature,


Celsius temperature, , can be approximated using the formula

, from an initial

where is the coefficient of expansion, which for steel is 11.7x10-6 / C (per Celsius degree),
and is the length of the bridge at temperature .
Using this formula, write a C++ program that displays a table of expansion lengths for a steel
bridge that is 7365 feet long at 0C, as the temperature increases to 50C in 5C increments.
Display the temperature in a column which is 5 characters wide and format the expansion length
to the nearest 4 decimal places (fixed format). Properly label the two columns to reflect their
content.

10) Join the rectangles with related loop statements:

The number of
iterations is known

The for loop

The Statement is
executed at least
once

The while loop

The Statement
section must
change the control
variable

The do-while loop

Executes the Statement


and then tests the
Condition

Tests the Condition and


then executes the
Statement

If the condition is left


blank, a true value is
assumed, which results
in an infinite loop.

S-ar putea să vă placă și