You are on page 1of 2

Sultan Qaboos University

College of Science
Department of Computer Science
COMP2002, Spring 2012
Week 4 Lab Exercises

Exercise 1 (suggested time 10 minutes): Create a new project for Lab3 and add the following C++ code:

#include <iostream>

using namespace std;

int main ()

{
double a, b, c;
a = 3.1415926534;
b = 2006.0;
c = 1.0e-10;

cout << a << '\t' << b << '\t' << c << endl;
cout << fixed << a << '\t' << b << '\t' << c << endl;
cout << scientific << a << '\t' << b << '\t' << c << endl;

return 0;
}
Observe the differences in the format of the numbers using different output manipulators.

Exercise 2 (suggested time 15 minutes): Modify the code to get the output as shown below.

Exercise 3 (suggested time 15 minutes):

Write, compile, link and execute a C++ program to calculate and display the average of 4 real numbers
given by the user. The output should be formatted as shown below.

The average of the 4 numbers is: xxxx.xxx

where xxxxx.xx represents a field of width 8 including 3 digits to the right of the decimal point.
Exercise 4:

Write, compile, link and execute a C++ program to calculate and display the natural logarithm deviation
of 4 real numbers given by the user. The output should be formatted as shown below.
The natural logarithm deviation of n numbers is given by:

a) Using this formula write a C++ program that calculates and displays the natural logarithm deviation
of 4 real numbers given by the user. The result should be displayed as shown below.

The natural logarithm deviation of the 4 numbers = xx.xxxx

where xx.xxxx represents a field of width 8 including 4 digits to the right of the decimal point.

b) Compile, link and execute your program.


c) Name your source file lab3_xxxxx.cpp (where xxxxx is your student ID) and upload it on Moodle
before the end of the lab session.

You might also like