Description
1. Use a one-dimensional array to solve the following program: Read in 20 numbers, each of
which is between 10 and 100, inclusive. As each number is read in, validate it and store it in
the array only if it is not a duplicate of a number already read. After reading all the values,
display only the unique values that the user has entered.
Expected Output:
Enter 10 integers between 10 and 100:
1
Invalid number.
4
Invalid number.
1999
Invalid number.
11
12
13
14
19
11
Duplicate number.
22
12
Duplicate number.
23
345
Invalid number.
45
46
47
The nonduplicate values are:
11 12 13 14 19 22 23 45 46 47
2. Write a recursive function that computes F(n) = 2 + 4 + 6 + … + 2n. Calculate F(n) in
function by using the ”pass-by-reference” technique.
Expected Output:
(i)
Enter n for computing F(n) : 1
F( 1 ) = 2
(ii)
Enter n for computing F(n) : 1
F( 1 ) = 2
(iii)
Enter n for computing F(n) : 2
F( 2 ) = 6
(iv)
Enter n for computing F(n) : 3
F( 3 ) = 12
(v)
Enter n for computing F(n) : 4
F( 4 ) = 20
2
(vi)
Enter n for computing F(n) : 1234
F( 1234 ) = 1523990
(vii)
Enter n for computing F(n) : 123456
F( 123456 ) = 15241507392
3. Write a program that accepts from the user homework 1, hw2, hw3, midterm and final exam
scores for up to 10 students. Declare 5 arrays, one for each of hw1, hw2, hw3, midterm and
final, to read in the scores of the students. Use sentinel controlled loop and terminate accepting values when the user enters -1 or the maximum number of students have been entered.
Display the average, standard deviation, largest, smallest and second smallest of all five grading components in a tabular format. In another table, display each student’s overall grade
(same as hw1-q1 grading scale) and the corresponding letter grade based on our grading scale
from our syllabus that is available on the BB.
Make sure you check your output carefully with respect to the following excepted output;
program terminates for (i) and (ii):
Expected Output:
(i)
Homework #1 score for Student #1 (or -1 to exit) : -1
(ii)
Homework #1 score for Student #1 (or -1 to exit) : 1234
Invalid score; must be in [0, 100].
Enter Homework #1 for Student #1 one more time : -1
If you want to exit, enter -1 one more time, or
Enter Homework #1 for Student #1 one more time : -1
(iii)
Homework #1 score for Student #1 (or -1 to exit) : 10
Homework #2 score for Student #1 (or -1 to exit) : 10
Homework #3 score for Student #1 (or -1 to exit) : 10
Midterm score for Student #1 (or -1 to exit) : 10
Final Exam score for Student #1 (or -1 to exit) : 10
Homework #1 score for Student #2 (or -1 to exit) : -1
If you want to exit, enter -1 one more time, or
Enter Homework #2 for Student #2 one more time : -1
3
************ Accepted Student Count = 1. *************
Homework1 Homework2 Homework3 MidTerm Final
========= ========= ========= ======= =====
Average 10 10 10 10 10
Std.Dev 0 0 0 0 0
Largest 10 10 10 10 10
Largest2 10 10 10 10 10
Smallest 10 10 10 10 10
Smallest2 10 10 10 10 10
========================================================================
Total Grade Letter Grade
=========== ============
Student # 1 10 F
(iv)
R:\teaching\UMF\CSC175\F2007\Homeworks\hw10>hw10-q3.exe
Homework #1 score for Student #1 (or -1 to exit) : 1000
Invalid score; must be in [0, 100].
Enter Homework #1 for Student #1 one more time : 100
Homework #2 score for Student #1 (or -1 to exit) : 100
Homework #3 score for Student #1 (or -1 to exit) : 100
Midterm score for Student #1 (or -1 to exit) : 100
Final Exam score for Student #1 (or -1 to exit) : 100
Homework #1 score for Student #2 (or -1 to exit) : 59
Homework #2 score for Student #2 (or -1 to exit) : 59
Homework #3 score for Student #2 (or -1 to exit) : 59
Midterm score for Student #2 (or -1 to exit) : 59
Final Exam score for Student #2 (or -1 to exit) : 59
Homework #1 score for Student #3 (or -1 to exit) : 60
Homework #2 score for Student #3 (or -1 to exit) : 60
Homework #3 score for Student #3 (or -1 to exit) : 60
Midterm score for Student #3 (or -1 to exit) : 60
Final Exam score for Student #3 (or -1 to exit) : 60
Homework #1 score for Student #4 (or -1 to exit) : 81
Homework #2 score for Student #4 (or -1 to exit) : 81
Homework #3 score for Student #4 (or -1 to exit) : 81
Midterm score for Student #4 (or -1 to exit) : 81
4
Final Exam score for Student #4 (or -1 to exit) : 81
Homework #1 score for Student #5 (or -1 to exit) : 49
Homework #2 score for Student #5 (or -1 to exit) : 49
Homework #3 score for Student #5 (or -1 to exit) : 49
Midterm score for Student #5 (or -1 to exit) : 12
Final Exam score for Student #5 (or -1 to exit) : 100
Homework #1 score for Student #6 (or -1 to exit) : -12
Invalid score; must be in [0, 100].
Enter Homework #1 for Student #6 one more time : 21
Homework #2 score for Student #6 (or -1 to exit) : 23
Homework #3 score for Student #6 (or -1 to exit) : 99
Midterm score for Student #6 (or -1 to exit) : 1
Final Exam score for Student #6 (or -1 to exit) : 89
Homework #1 score for Student #7 (or -1 to exit) : 123
Invalid score; must be in [0, 100].
Enter Homework #1 for Student #7 one more time : 33
Homework #2 score for Student #7 (or -1 to exit) : 33
Homework #3 score for Student #7 (or -1 to exit) : 33
Midterm score for Student #7 (or -1 to exit) : 33
Final Exam score for Student #7 (or -1 to exit) : 99
Homework #1 score for Student #8 (or -1 to exit) : 23
Homework #2 score for Student #8 (or -1 to exit) : 34
Homework #3 score for Student #8 (or -1 to exit) : 34
Midterm score for Student #8 (or -1 to exit) : 92
Final Exam score for Student #8 (or -1 to exit) : 23
Homework #1 score for Student #9 (or -1 to exit) : 0
Homework #2 score for Student #9 (or -1 to exit) : 0
Homework #3 score for Student #9 (or -1 to exit) : 0
Midterm score for Student #9 (or -1 to exit) : 0
Final Exam score for Student #9 (or -1 to exit) : 0
Homework #1 score for Student #10 (or -1 to exit) : 199
Invalid score; must be in [0, 100].
Enter Homework #1 for Student #10 one more time : 100
Homework #2 score for Student #10 (or -1 to exit) : 100
Homework #3 score for Student #10 (or -1 to exit) : 100
Midterm score for Student #10 (or -1 to exit) : 100
Final Exam score for Student #10 (or -1 to exit) : 1
Max number of students is 10. Cannot accept any more!
5
************ Accepted Student Count = 10 *************
Homework1 Homework2 Homework3 MidTerm Final
========= ========= ========= ======= =====
Average 52.60 53.90 61.50 53.80 61.20
Std.Dev 32.30 31.25 32.04 37.94 38.00
Largest 100 100 100 100 100
Largest2 81 81 99 92 99
Smallest 0 0 0 0 0
Smallest2 21 23 33 1 1
========================================================================
Total Grade Letter Grade
=========== ============
Student # 1 100.00 A+
Student # 2 59.00 F
Student # 3 60.00 D
Student # 4 81.00 BStudent # 5 58.30 F
Student # 6 50.20 F
Student # 7 59.40 D
Student # 8 45.90 F
Student # 9 0.00 F
Student #10 60.40 D
4. Write a program that randomly selects from a bag of eight objects. Each object can be red,
blue, orange, or green and it can be a ball or a cube. Assume the bag contains once object
for each combination, i.e. one red ball, one red cube, etc. Using one character array for color
and one for the shape, write a program to draw random object from the bag with replacement.
Expected Output:
Enter no. of items to draw (0 to exit): 10
blue ball
orange ball
red ball
blue ball
red cube
red cube
green cube
green cube
red ball
6
blue ball
Enter no. of items to draw (0 to exit): 15
green ball
blue ball
blue cube
red ball
orange ball
green ball
orange cube
orange cube
blue ball
red ball
green cube
blue ball
green ball
orange ball
blue ball
Enter no. of items to draw (0 to exit): 2
red cube
blue cube
Enter no. of items to draw (0 to exit): 0
5. Repeat q5 above but this time without replacement, that is, one red ball is drawn, it cannot
be drawn again.
Expected Output:
(i)
Enter no. of items to draw (0 to exit): 9
Only 8 objects! Exiting…
(ii)
Enter no. of items to draw (0 to exit): 2
green cube
red ball
Enter no. of items to draw (0 to exit): 2
red cube
orange ball
Enter no. of items to draw (0 to exit): 2
green ball
orange cube
Enter no. of items to draw (0 to exit): 2
blue cube
blue ball
Enter no. of items to draw (0 to exit): 0
7
(iii)
Enter no. of items to draw (0 to exit): 8
blue cube
orange ball
orange cube
green ball
red cube
green cube
red ball
blue ball
Enter no. of items to draw (0 to exit): 0
6. The factorial function results reported in the expected output for hw8-q3 were not correct.
See http://www.nitrxgen.net/factorialcalc.php
Write an accurate factorial function. Hint: Store the individual digits of the factorial in an
integer array.
Expected Output:
(i)
Enter a number for factorial calculation: 5
120
(ii)
Enter a number for factorial calculation: 10
3628800
(iii)
Enter a number for factorial calculation: 25
15511210043330985984000000
(iv)
Enter a number for factorial calculation: 36
371993326789901217467999448150835200000000
(v)
Enter a number for factorial calculation: 38
523022617466601111760007224100074291200000000
(vi)
Enter a number for factorial calculation: 73
44701154615126843408912571381250511100768007002829050158190800923704221040671833
17016903680000000000000000
8
(vii)
Enter a number for factorial calculation: 100
93326215443944152681699238856266700490715968264381621468592963895217599993229915
608941463976156518286253697920827223758251185210916864000000000000000000000000
(viii)
Enter a number for factorial calculation: 200
78865786736479050355236321393218506229513597768717326329474253324435944996340334
29203042840119846239041772121389196388302576427902426371050619266249528299311134
62857270763317237396988943922445621451664240254033291864131227428294853277524242
40757390324032125740557956866022603190417032406235170085879617892222278962370389
7374720000000000000000000000000000000000000000000000000
(ix)
Enter a number for factorial calculation: 300
30605751221644063603537046129726862938858880417357699941677674125947653317671686
74655152914224775733499391478887017263688642639077590031542268429279069745598412
25476930271954604008012215776252176854255965356903506788725264321896264299365204
57644883038890975394348962543605322598077652127082243763944912012867867536830571
22936819436499564604981664502277165001851765464693401122260347297240663332585835
06870150169794168850353752137554910289126407157154830282284937952636580145235233
15693648223343679925459409527682060806223281238738388081704960000000000000000000
0000000000000000000000000000000000000000000000000000000
9
Deliverables:
1. Source Code: (.cpp file) that must start with a comment block similar to the following:
/*************************************************************************
** Author : Suleyman Uludag
** Program : hw1, q1
** Date Created : September 15, 2013
** Date Last Modified : September 16, 2013
** Usage : No command line arguments
**
** Problem:
Accept the following information from the user (keyboard):
– Hw1, hw2 and hw3 (out of 100)
– Midterm (out of 100)
– Final exam (out of 100)
Calculate the total grade out of 100 based on the following grading scale:
Hws –> 30% (10% each)
Midterm –> 30%
Final Exam –> 40%
** Pseudocode:
** 1)
** 2)
*************************************************************************/
2. Executable (.exe file under windows). You must explicitly state the platform of your executable (such as Linux, etc.) if it is not Windows. Please name your file by using the
question number: hw1-q1.exe (for Windows)
3. Screenshot of your app. For screenshot, you can use the following free program on windows:
http://www.wisdom-soft.com/downloads/setupscreenhunterfree.exe
For Linux/Unix, there are many alternatives. I personally like shutter.
File naming convention example:
hw1 q1.png (or .jpg or another graphics format)
4. You must zip all the above three files into ONE .zip file and submit your assignment by the
deadline on moodle system. Name your file as Lastname-Firstname-hw#.zip. For example,
Uludag-Suleyman-hw1.zip
For generating .zip file, you may use the following free software on Windows:
http://www.7-zip.org/download.html
Linux/Unix has many built-in.
10