PF Fall 2019 Assignment 3
PF Fall 2019 Assignment 3
PF Fall 2019 Assignment 3
SCIENCES ISLAMABAD
Programming Fundamentals (CS118)
FALL 2019 ASSIGNMENT # 3
Plagiarism: Plagiarism cases will be dealt with strictly. If found plagiarized, both the involved
parties will be awarded zero marks in this assignment, all of the remaining assignments, or even
an F grade in the course. Copying from the internet is the easiest way to get caught!
Deadline: The deadline to submit the assignment is 04th November 2019 at 1:00 PM. Late
submission with marks deduction will be accepted according to the course policy shared earlier.
Correct and timely submission of the assignment is the responsibility of every student; hence no
relaxation will be given to anyone.
Comments: Comment your code properly. Bonus marks (maximum 10%) will be awarded to well-
commented code. Write your name and roll number (as a block comment) at the beginning of the
solution to each problem.
Tip: For timely completion of the assignment, start as early as possible. Furthermore, work
smartly - as some of the problems can be solved using smarter logic.
Note: Follow the given instructions to the letter, failing to do so will result in a zero.
Question 1: Draw a triangle as below of size n (number of rows), where n is input by the user. [5 Marks]
.....*.....
....***....
...*****...
..*******..
.*********.
***********
Question 2: Draw a diamond pattern as shown below of size 2n (number of rows), i.e. n is the number
of the center row and is input by the user. [5 Marks]
For n = 4.
......*......
....*****....
..*********..
*************
*************
..*********..
....*****....
......*......
Question 3: The rand() function generates values on the basis of a distribution called the normal
distribution. One of the favorable properties of the normal distribution is that its average is exactly the
middle value of its range. It means that if we run the rand() function in C++, having the range 0 - 32767,
a large number of times and calculate the average of all the generated values, it would be exactly
16383.5. In this problem, you are required to find the smallest value of n (number of times the rand() is
called) that results in the average equal to 16383.5 +- 0.0001. Specifically, your program shall call rand()
1000 times and see the average, then call it another 10000 times and see the average, and so on until
the average is 16383.5 +- 0.0001. [5 Marks]
Question 4: Given the first value, generate the next seven terms of the sequences like 1, 11, 21, 1211,
111221, 312211, 13112221, 1113213211. [10 Marks]
Question 5: Given the first value, generate the next ten terms of the sequence like 1, 2, 4, 8, 16, 22, 26,
38, 62, 74, 102, 104, … [10 Marks]
Question 6: Draw a circle pattern as shown below of size n (diameter). [10 Marks]
Question 7: Draw the following pattern having alternating dot (.) and underscore (_) characters. It is
important to understand that the alternating sequence is across the lines as well, i.e. if a line starts with
a dot then the next line will start with an underscore and vice versa. The input from the user is the
number of lines until the centerline. [10 Marks]
For n = 5.
*_._.
**._.
***_.
****.
*****
****_
***._
**_._
*._._
Bonus: To get 10 bonus marks, draw the above pattern using only one loop.
Question 8: Draw the graph of x^2, x^4, or any even (positive) power of x. The input from the user is the
maximum absolute value of x (as shown in the picture below). The absolute value of x means that the
graph should be drawn between the negative and positive (-x, x) values of x. You should draw the y-axis
according to the maximum value of x. [20 Marks]
For x^2 and maximum value 3:
09 * *
08
07
06
05
04 * *
03
02
01 *
*
00 *
-3-2-1 0 1 2 3
Question 9: Draw the graph of x, x^3, or any odd (positive) power of x. The input from the user is the
maximum absolute value of x (as shown in the picture below). The absolute value of x means that the
graph should be drawn between the negative and positive (-x, x) values of x. You should draw the y-axis
according to the maximum value of x. [20 Marks]
9
8 *
7
6
5
4
3
2
1 *
0 *
-1 *
-2
-3
-4
-5
-6
-7
-8 *
-9
-2-1 0 1 2
Question 10: Draw a bar chart of 5 values entered by the user, where the y-axis is along the rows and
the x-axis is along the columns of the screen (as shown in the figure below). [10 Marks]
10 *
09 * *
08 * * *
07 * * *
06 * * * *
05 * * * *
04 * * * *
03 * * * *
02 * * * * *
01 * * * * *
1 2 3 4 5
Question 11: In this problem, you are required to use for loop only to implement a calculator using ++
(increment) and -- (decrement) operators only. The operations that your calculator shall perform are
limited, i.e. addition, subtraction, multiplication, division, and remainder. Your program shall ask the
user about the two input values and an operator, and then use the switch-case structure to run the
required operation (addition, subtraction, multiplication, or division). Your program shall run correctly
for positive input values only. [25 Marks]
First Input: 10
Second Input: 12
Operation: Addition
Answer: 22
Note: Any operation that uses arithmetic operators other than ++ or -- will be awarded zero marks.