The goal of this lab is to introduce you to basic
concepts in the C++ program language. Concepts you will be familiar with
after this lab include:
- Basic user input/output
- Conditional statements and boolean values
- Loop structures
- Functions
- Using handin35
- Course syllabus and policies
Some of these programs may seem simple, but it is meant to prepare you
for future labs and also to provide practice for in-class topics.
A skeleton directory for your solutions will appear in your
cs35/labs/00 directory when you run update35. The
program handin35 will only submit files in this directory. In
later labs you may have the opportunity to work with a partner,
but for this lab you should work on your own. Be sure that all
solutions are placed in the cs35/labs/00 directory before submitting.
Exercises
- Loops and strings: In the file funStrings.cpp, complete
a program that takes in a series of words from the user and outputs a string
with the words in the reverse order. The loop should stop when
the user enters "done". For example:
$ ./funStrings
Now
is
the
winter
of
our
discontent
Made
glorious
summer
by
this
son
of
York
In Reverse:
York of son this by summer glorious Made discontent our of winter the is Now
- For loops: In the file computeAvg.cpp, write a program
that asks a user to enter up to 10 positive integers and computes the average. Your program should accomplish the following:
- You must use a for loop and no while loops.
- If the user enters 0 or less, the loop should end and compute
the average of the preceding numbers. E.g., if the user enters:
5
10
15
-3
you should report the average as 10
- While the inputs are integers, your average should be allowed to take
on any real value.
- Conditions, Loops, and Functions: In the
file grade.cpp complete a program that obtains a user's
numerical test score and outputs their letter grade
(assume a standard conversion of 90 and above is in A;
80 up to 90 is a B, etc.).
Your program should:
- Ask the user to enter a test score (assume an integer)
- Ensure the user enters a valid score (i.e., from 0 to 100). If not, repeat the question
- Convert the score to a letter grade
- Output the result
- Your solution should employ functions and good design. That is,
your main function should be simple and only a few lines of code. The
major tasks should each be placed in separate functions.
Below is some sample output of a correct solution. Your solution does
not have to completely match this, as long as it correctly meets the
requirements above.
Sample Run
./grade
Welcome to the grade calculator
Enter an integer between 0 and 100: 101
Incorrect entry, try again: 99
You earned a(n): A
./grade
Welcome to the grade calculator
Enter an integer between 0 and 100: -10
Incorrect entry, try again: 83
You earned a(n): B
- Written Response: In a file called README, answer
the following questions to test your understanding of course policies:
- How many late days are allowed for the entire semester?
- Read the academic integrity policy. Is it acceptable to have
an upper-level CS student look at your program to help figure out
why it is not working?
- If you are encountering a problem with your program, what must you
do before asking either a ninja or faculty member for assistance?
- If you are stuck on your program, rank the following support options
by which you should attempt first. Note that some of these should
never be attempted, so leave them off your priority list.
- Email the professor
- Attend a ninja session
- Ask a student ninja that you run into around campus
- Work on the problem with your lab partner
- Ask a fellow student who is not your lab partner
- See either instructor during office hours
- See either instructor during non-office hours when their door is open
- Ask the instructor when you see him at Sharples enjoying an ice cream
General Requirements
Your program should follow good coding practices. Specifically, each file
should maintain the following properties:
- Each file should contain a top-level block comment explaining
the purpose of the program. It should also contain your name
- Each function should contain a comment block explaining the functions
purpose as well as all inputs (i.e., parameters) and outputs (i.e., return
values)
- Any non-obvious code should be commented. But don't over do it.
- Comments should be concise
- Variable names/function names should be meaningful and follow
conventions learned in CS21
The following code snippet is a example of good commenting style.
/* findMax
*
* Inputs: a,b,c: three integers we want to compare
*
* Returns: the maximal input
*/
int findMax(int a, int b, int c) {
int max = a; // initialize to first input
// compare max to b
if(b > max) {
max = b;
}
// now compare max to c
if(c > max) {
max = c;
}
// return maximal element
return max;
}
Submit
Once you are satisfied with your code, hand it in by typing
handin35. This will copy the code from your
cs35/labs/00 to my grading directory. You may run
handin35 as many times as you like, and only the most recent
submission will be recorded.