0% found this document useful (0 votes)
2 views2 pages

hw3 Fall2011

The document outlines the homework assignment for COMP2002 at Sultan Qaboos University, requiring students to write a C++ program that calculates exact and estimated square roots for a list of numbers. The program must handle input from the keyboard, generate random numbers, or read from a file, and output results to 'square_roots.txt' in a specified format. It also includes grading criteria and policies on late submission and academic integrity.

Uploaded by

lab67623
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

hw3 Fall2011

The document outlines the homework assignment for COMP2002 at Sultan Qaboos University, requiring students to write a C++ program that calculates exact and estimated square roots for a list of numbers. The program must handle input from the keyboard, generate random numbers, or read from a file, and output results to 'square_roots.txt' in a specified format. It also includes grading criteria and policies on late submission and academic integrity.

Uploaded by

lab67623
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Sultan Qaboos University

College of Science - Department of Computer Science


COMP2002: Introduction to Computer Programming for Engineers, Fall 2011
HOMEWORK ASSIGNMENT NO 3
Due Date: 28th November, 2011

 Your name, ID number, and section should be written as comments in your program.
 Name your program file HW3-ID.cpp replacing ID by your student ID number.
 Submit your program file on Moodle by the above deadline.

The <cmath> function sqrt(x) is used to calculate the exact square-root value of the number x such
that x is a non-negative real number. The square root value of x can also be estimated using the
following mathematical equation:
1
ln(x )
x e 2

Where e = 2.718282, and ln is the natural logarithm; called log() in <cmath>.

Write a C++ program that calculates and writes to an output file the exact and estimated square
roots of a list of numbers. The list of numbers is either read from the keyboard, generated
randomly or read from an input file. The program displays to the user the following menu and
performs the corresponding tasks based on the user’s selection. The program should keep running
until the user enters ‘Q’ or ‘q’ from the options list.
K: Read the list of numbers from the keyboard
R: Generate the list of numbers randomly
F: Read the list of numbers from an input file
Q: Quit the program

 If the user enters ‘K’ or ‘k’, the program should read a list of numbers terminated by Ctrl+z
from the keyboard.
 If the user enters ‘R’ or ‘r’, the program should ask the user to specify how many numbers
to generate randomly and then generates that many random values. The generated
numbers MUST range between 5.000 and 20.000. For this purpose use the rand() function
to return values between 5000 and 20000 and divide them by 1000.
 If the user enters ‘F’ or ‘f’, the program should read the list of numbers from an input file
which could be empty or contain any number of values given individually on separate lines.
The name of the input file is provided by the user at run-time.
 For each of the 3 menu options K, R and F, the program should calculate the exact and
estimated square roots of the obtained list of numbers and write the results in an output
file called ‘square_roots.txt’. The program should ignore (skip) any negative values in the
list of numbers and do the calculations and display the results only for the non negative
values.
 The output should be written to the output file in tabular format including, for each non
negative number x in the list, the number x itself, its exact root, its estimated root, and the
absolute value of the difference between the two roots. Format the x’s with 3 digits after
the decimal point and format the roots and the difference between them with 6 digits after
the decimal point.
 BONUS (1 Mark): Display the maximum difference between the exact and the estimated
roots.

Page 1 of 2
Grading Sheet: COMP2002-Fall2002-HW3 0.0 ¼ ½ ¾ 1.0 1¼ 1½ 1¾ 2.0

Style (Comments, naming, indentation)

Declarations (files and variables)

Handling of conditional statements.

Handling of looping.

Handling of files.
Handling of random number generation and
calculations

Total

BONUS

LATE SUBMISSION AND COPYING PENALTY POLICY:

1. Late submission penalty: 50% per day.


2. Students involved in copying (e.g. copying in an exam, submitting similar or exact assignment solutions, sharing
Moodle class accounts) will be severely penalized. A zero mark will be given the first time a student is caught
involved in copying in any course item (e.g. Exam, Assignment) and his/her name will be added to a watch list
maintained by the Head of Department.
Further repeated involvement in copying will cause the student to get an F grade in that course. This is in line
with the university academic regulations. (See pages 36-37 of the 2005 edition of the university academic
regulations booklet).

Page 2 of 2

You might also like