CSE100: Principles of Programming with C++
FALL 2007
Assignment 4 (Due on 10/9 in class)
The written portion of the assignment must be put in a standard sized 9" X 12" envelope and submitted to me in class. See the syllabus for labeling instructions. Points will be deducted if your assignment is not in such an envelope.
Your program must be submitted online using the Online Submission link. LATE ASSIGNMENTS ARE NOT ACCEPTED. Do not slide assignments under office doors. Do not submit assignments to the computer science department office or to the TA/grader. Do not ask to have them put into my mail box.
Topics:
Functions- Value Returning, Return values & Parameters
Use the following Coding Guidelines:
Give identifiers semantic meaning and make them easy to read (examples num_students, gross_pay, etc).
Keep identifiers to a reasonably short length.
User upper case for constants.
Use lower case with ‘_’ separators for all other identifiers.
Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, functions and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.
Use white space & blank lines to make your program more readable.
Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. (ex. }// end of function main)
Part1: Written Exercises (6 points)
Turn in answers to the following questions. Please write only your answers neatly on a sheet of paper with your name printed at the top. Submit the sheet in the envelope. You will not receive any partial points for an incorrect answer. Make sure you do the correct exercises.
1. Ch6. Q4 (2 pts)
2. Ch6. Q9 (2 pts)
3. Ch7 Q6 (1 pt)
4. Ch7. Q7 (1 pt)
Part 2: Programming Exercises(14 points)
Write a complete C++ program in a source file to be named Assignment4.cpp. Before you begin writing your program, you are required to plan and write down the pseudocode for your solution.
Your program will be an interactive menu for a geek, who likes answering several questions such as “is this year a leap year?”, “what is the sum of all numbers between two numbers?” among other things. In order to prevent the geek from getting carried away, your program must also keep track of the total number of questions asked so far and then display the message “Total Number of Questions Exceeded!” and automatically quit, once the total number of questions asked so far exceeds 10.
Your program must have the following functions:
a getName function which will input the name of the Geek
an isEven function that takes two integers and returns a boolean value indicating if the sum of the numbers is even or not
a sum function that takes two integers and computes and returns an int which is the sum of all numbers between the two inclusive (include the numbers in the sum) – for full credit this method should work even if the two numbers are the same (the sum is just one of the numbers) or if the first number is larger than the second. Also, you cannot assume which number will be greater.
a isLeapYear function that takes an integer and returns a boolean value indicating if the number is a leap year. A leap year is one with 366 days. A year is a leap year if it is divisible by 4 (for example, 1980), and if it is not divisible by 100 (for example 1900); however, it is a leap year if it is divisible by 400 (for example, 2000).
a display function which will display the geeks name and the number of questions that have been asked so far. This question is not included in calculating the number of questions asked so far.
The four functions should have the following prototypes:
string getName(int & countQuestions);
boolean isEven(int firstNumber, int secondNumber, int & countQuestions);
integer isEven(int firstNumber, int secondNumber, int & countQuestions);
boolean isLeapYear(int year, int & countQuestions);
void display(string name, int countQuestions);
The program will ask a user to enter one of the following commands. Based on the user's choice, the program needs to perform corresponding operation. The program will terminate when the user enters 'q'.
Command Options
-----------------------------------
a : Geek's name
b : Is sum even ?
c : Sum between two integers ?
d : Leap year?
? : Display
q : Quit
Sample Output (user input is in bold):
Command Options
-----------------------------------
a : Geek's name
b : Is sum even ?
c : Sum between two integers ?
d : Leap year?
? : Display
q : Quit
Please enter a command or type ?
a
Geek
Please enter a command or type ?
b
Enter a number: 2
Enter another number: 3
5 is not even
Please enter a command or type ?
c
Enter the first number: 4
Enter the second number: 7
The sum between 4 and 7 is 22
Please enter a command or type ?
d
Enter a year: 1980
It is a leap year!
Please enter a command or type ?
d
Enter a year: 1982
It is not a leap year!
Please enter a command or type ?
d
Enter a year: 2000
It is a leap year!
Please enter a command or type ?
?
Name : Geek
Question Count : 6
Please enter a command or type ?
q
Press any key to continue . . .
Helpful hints for doing this assignment:
· work on it in steps – write one function, test it and make sure it works before going on to the next
· always make sure your code compiles before you add another function
· your functions should be able to be called in any order
Assignment Submission
Type out your pseudocode and submit a hard copy of your pseudocode and your program in the folder, in class.
Enclose a sheet with the answers for the written exercises (Part 1).
Submit ONLY the .cpp file. Do not submit any other files.
Go to the course web site (my.asu.edu), and then click on the On-line Submission tab. Submit your Assignment4.cpp. Make sure to choose HW4 from the drop-down box.
Also your envelope should be labeled with your name (Print), and your lab details (include the lab letter, the day and the time). The assignment is due in class, at the beginning of class on the due date.
NO LATE ASSIGNMENTS WILL BE ACCEPTED.