Assignment #1
CS 162: Introduction to Computer Science
Submit your assignment to the D2L Dropbox
Email a backup copy to karlafgr@cs.pdx.edu
***Assignments
in CS162 consists of written homework and programming***
***All
parts are required to get a grade on this homework ***
WRITTEN
HOMEWORK QUESTIONS
Algorithms
should be written using complete sentences in a way that is easy to follow –
consider using outline form for clarity.
C++
PROGRAMMING PORTION
The
purpose of this first program is get you familiar with the fundamental syntax
of C++ and the process of writing and turning in programs at PSU. Think of this
program as a way to demonstrate some simple interactions between the user and
program. For those of you who already know C++, use this program as a refresher
and stretch yourself to do more than is assigned!
To get full credit for the programming
portion, you will need to:
1.
Turn in an algorithm written using full English
sentences (the algorithm will be pass/no-pass for the first assignment). It
should be supplied as part of your header comments. This is NOT the same
algorithm as the homework portion previously described. This is about the
algorithm for solving the programming part of this assignment. This is worth
20% of the assignment’s grade
2.
Program using a consistent style of indentation,
header comments for each function, inline comments for
each major block of code. This is also worth 20% of the assignment’s grade.
3.
Submit an electronic copy of your .cpp file as an attached file to the
dropbox on D2L (go to: http://d2l.pdx.edu/ to
login). Make sure to hit the submit button after uploading your files
(otherwise they will be lost)
4.
As a backup, please also email your work (as
attached file(s)) to karlafgr@cs.pdx.edu
Program Assignment:
Background:
A perfect number is a positive integer that the sum of the proper divisors
equals that number. A proper divisor is one that goes into the number without a
remainder (a remainder of zero). So, 28 is a perfect number because all of its
proper divisors add up to be 28 (1 + 2 + 4 + 7 + 14). If you add up all of
these numbers you get 28. If the sum of the divisors is less than the number,
it is called a deficient number. If it is greater, then it is called an
abundant number.
Your job will be to write a simple game program where the
computer will randomly pick a number between 1 and 50. The player must decide
if the number if proper, deficient, or abundant If they answer correctly, they get a
point. The game ends when a player gets 10 points.
Extra credit – allow for two players and alternate
between those two players. The game ends when someone wins with 10 points.
Beyond this, you have the opportunity to create your own
algorithms!
Random Numbers:
In this program you will
need to use the random number generator. There are two function needed to use
this from the cstdlib library (standard library
header file) and ctime:
·
srand(time(0);
to seed the random number generator at the beginning of your program (i.e., at
the beginning inside of main() )
·
rand()
which will return a random number when you need it:
value = rand();
·
The
mod operation (%) is great to limit the value returned from the random number generator,
such as limiting the number to a range of 1-50. variable = some_number % maximum; The % gives you the remainder (which
is what is left over after an integer division).
The Basic Algorithm:
1.
Initialize
the player’s score to zero
2.
Prompt
the user to begin
3.
Display
a number between 1 and 50, using a random number generator
4.
Ask
the user for their guess (Deficient, Perfect, Abundant)
5.
Apply
the algorithm to find out if the number is perfect (*** You
need to work on this!)
6.
Compare
the answer calculated to the user’s answer
a.
If is
matches, add 1 to the player’s score
7.
Display
the results
8.
Continue
until the user wants to quit or a score of 10 is received
You may add more to this program!
Things
you should know...as part of your program:
1.
Make
sure to prompt the user for any input requested. Make sure it is clear from
your prompts what the user is expected to do.
2.
The
program should continue until the user wants to quit. Allow them to continue
until they are done.
3. You
may not use any global variables in this program!
4. You
may not use the string class – instead use arrays of characters
5.
Make
sure to use C++’s I/O (iostream library) for I/O
On the due date, turn in:
1.
Submit
your assignment to the D2L
Dropbox (sign on via http://d2l.pdx.edu/)
2.
Email
a backup copy to karlafgr@cs.pdx.edu
3.
Remember
to turn in an algorithm with your
programming portion (in the header comments). It is worth 20% of your program
grade.
4.
And, don’t forget to add comments and to work on your program’s readability;
this is another 20%!!