You are on page 1of 2

Sultan Qaboos University

College of Science – Department of Computer Science


COMP2102: Problem Solving and Programming, Spring 2011
Assignment #1

Due Date: Saturday 5th March 2011 @ 11:55 pm


Important:
 Name your program As1-ID.cpp (e.g. As1-1234.cpp)
 Submit only your program .cpp file to Moodle

Write a menu-driven program that displays the first n numbers of the selected numerical series. Your program
should repeatedly display the following menu and stop when the user selects the Exit option:
=====================================
Print Series
=====================================
(1) Primes series
(2) Fibonacci series
(3) Exit
=====================================
Enter your selection (1,2,3):

Your program should implement every menu option as a separate function. Your program should include the
following functions:
 Primes(…..): receives a positive integer number n that represents the number of the required series terms
and returns no value. The function displays on the screen the first n numbers in the Primes series. For
instance if n=10 then the function displays:
1 2 3 5 7
11 13 17 19 23
 Fibonacci (…..): receives a positive integer number n that represents the number of the required series
terms and returns no value. The function displays on the screen the first n numbers in the Fibonacci
series. Starting with 0 and 1, the Fibonacci series term (from 3rd term on) is equal to the sum of its
previous two terms. For instance if n=10 then the function displays:
0 1 1 2 3
5 8 13 21 34
 Menu (…..): displays program menu on the screen and returns user choice. The function should return
only a valid user choice.

 GetN (…..): prompts the user for a positive integer and returns an integer value representing the number
of series terms required. The function should loop until a positive integer number is entered by the user.

 IsPrime(…..): receives a positive integer number n and returns true if n is a prime number otherwise it
returns false. A prime number is a number divisible only by itself and number 1

The functions Primes(…..) and Fibonacci (…..) should terminate the series if a series term exceeds C++
Maximum integer value (Tip: use built-in INT_MAX constant to check if a term has not exceeded
INT_MAX).

Display the selected series in a tabular format with five series terms per line. Function main should act as a
driver program serving various menu operations by calling relevant functions. Functions: Menu(), GetN(),
Primes() and Fibonacci() are called from function main. Function IsPrime() is called by function
Primes() to check if a number is a prime or not.

Sample program run is given below:

1 AB
Sample Program run:
===================================
Print Series
===================================
(1) Primes Series
(2) Fibonacci Series
(3) Exit
===================================
Enter your selection (1,2,3):1
Primes: Enter the number of terms: 20
1 2 3 5 7
11 13 17 19 23
29 31 37 41 43
47 53 59 61 67
===================================
Print Series
===================================
(1) Primes Series
(2) Fibonacci Series
(3) Exit
===================================
Enter your selection (1,2,3):2
Fibonacci : Enter the number of terms: 50
0 1 1 2 3
5 8 13 21 34
55 89 144 233 377
610 987 1597 2584 4181
6765 10946 17711 28657 46368
75025 121393 196418 317811 514229
832040 1346269 2178309 3524578 5702887
9227465 14930352 24157817 39088169 63245986
102334155 165580141 267914296 433494437 701408733
1134903170 1836311903
Fibonacci series term exceeds C++ INT_ MAX value after 47 terms.
===================================
Print Series
===================================
(1) Primes Series
(2) Fibonacci Series
(3) Exit
===================================
Enter your selection (1,2,3):3
Press any key to continue

Assignment Grading: Name: ID:


0 .5 1 1.5 2
Comments and Indentation
Variable Declarations
Function main()
Function GetN(…) + validate positive N
Function Menu(…) + validate choice
Function IsPrime(…)
Function Primes(…)
Function Fibonacci(…)
Check MAX_INT for Fibonacci series
Tabular format for both series terms
Program runs without syntax errors
Total _______/15

Late Submission and Copying Policies Apply as described in the course outline
2 AB

You might also like