09/14 - 09/18
Linux Operating System
Variables & Literals
Six parts of a variable
09/21 - 09/25
Nesting if statements
09/28 - 10/02
Better input validation
10/05 - 10/09
10/12 - 10/16
Passing by value/reference
10/19 - 10/23
Two dimensional arrays
|Ch. 05, 07, 10|
10/26 - 10/30
More functions, arrays, and file I/O
11/02 - 11/06
11/09 - 11/13
Veteran's Day Wednesday - No class
11/16 - 11/20
Extra credit lab
11/23 - 11/25
November 23, 2015 @ 02:00PM in RM 311
There isn't much new material to be covered in this class, so from this point on you should be focused on preparing for the final. To guide you on that path, this homework is a sort of review.
Write a single menu program that performs at least 4 of your previous assignments. Every option on your menu should call a function that does what a previous assignment did and upon completion should return to the menu for the user to select another option.
You may choose any lab, homework, or even coding exam you wish as long as you have at least 4 in your menu. As this is a review assignment however, I'd suggest you revist assignements you didn't complete or remember having trouble with. I'm not asking you to redo everything from scratch, obviously you can refer to your old completed assignments, but this would be a good opportunity to see how much of the early material you can rememeber.
The requirements for this assignment are as difficult as you want to make them, just don't squander this opportunity to review the course material.
This assignment is due Monday Novemeber 16th at midnight.
An important part of understanding functions is the ability to create functions based on predetermined criteria. For this part of the homework, you must start from this file, which is an incomplete program, and write the function bodies based on the prototypes already defined in the program. Pay careful attention to the return types of the functions, the types of the paramters that are passed in, and the way the functions are called. You can copy this file directly to your account by executing the following command:
cp ~austin/public_html/examples/hw04_1.cpp .
You must not edit the main function or any of the prototypes to get the program to compile and behave as required. The descriptions of the functions should be self explanatory, but if you want to see how the output will look please refer to this sample executable. You may copy the executable to your account using the following command:
cp ~austin/public_html/examples/hw04 .
Write a program that highlights the difference between passing by reference and passing by value. To accomplish this use the following 4 function prototypes EXACTLY:
void printUpRef(int &);
void printDownRef(int &);
In the first two functions print out all integers, in ascending order, that are less than 16. Use the passed argument as the starting point for this count.
For the second two functions print out all integers, in descending order, that are greater than 0. Use the passed argument as the starting point for this count.
In main() call the functions in this order:
printUp, printUpRef, printDown, printDownRef, printDown, printUp, printDown, printUpRef, printUp, printDown, printDownRef
Also, before calling each function print out which function you're calling and what value the variable you are passing has. For example:
cout « "In main(), calling printUp(" « counter « ")\n";
Before running your program trace your code to write down what you expect to happen, and then compare that to what actually happens. If your expectation doesn't match the result, think about why that is or ask me (or a tutor) to explain so that you can better understand how reference variables work.
There are two things I will check for in your solution.
One:the printUp & printUpRef functions have the EXACT same code inside them and the printDown & printDownRef functions have the EXACT same code inside them.
Two. You only declare ONE integer variable within the body of your main() function and initialize it to zero. Do not create additional variables within the bodies of the four required functions. Each of these functions already receives an integer parameter from main() that you must use instead.
A useful implementation of loops is a menu. A menu is basically a switch statement wrapped in a loop so that each iteration of the loop allows the user to select a different option each time.
For the first part of this homework you will create an 'dummy' menu. Your menu will offer 4 choices, including one for exiting the menu, and display an error message if the user doesn't input any of your choices. For this assignment you don't need to have complex code for each menu option, just indicate which menu option the user chose.
Another application of loops is to keep a running total, perhaps so you can do calculations on a series of numbers.
Create a program that allows a user to enter a number of grades, then calculate the average grade, the highest grade, and the lowest grade. You should know how to do most of these tasks, but keep in mind that in order to properly calculate the average you must divide the sum of all grades by the number of grades entered.
average = total / sum
You should also let the user decide how many grades they want to enter.
Prime numbers are numbers that can be divided only by 1 and themselves. For example: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 are the first 10 prime numbers.
You are to create a program that asks the user for a number and then determines whether or not that number is prime. There are several ways to do this, but all require you to use a loop of some sort. You may approach this problem in any way, but I recommend using the modulus operator so that you can determine whether there is a remainder or not between any two numbers.
For this week's homework you will be practicing flow control and flags by creating a short, fun quiz.
The quiz will be based on a few yes or no questions that you will keep track of using boolean flags. There are two main types of quizzes that you can do with this kind of setup: a quiz that displays each answer as the final result or a quiz where each combination of answers leads to a specific result. In my sample output below I have ideas you might use for either type of quiz, but you only need to do one quiz! My two examples are just to give you an idea for the different ways you can approach this assignment.
If you'd like to run this example for yourself you can copy it into your account by typing the command:
cp ~austin/public_html/examples/hw02 .
Your quiz doesn't have to work exactly like mine; you can choose more interesting or fun topics for your quiz. However your quiz MUST use boolean flags, have a different result for each combination of answers, and have at least 3 questions.
To make sure you are using flags correctly, I'll suggest you follow this logic in your program:
Make sure your .cpp file is located in your
cs221/homeworks/hw02/ directory by Wednesday September 30 by 12:45 or it will be late.
The purpose of this first homework is to make sure you are completely capable of properly setting up I/O on your own. The first lab should have been a big help in this regard. But more than that this homework will hopefully give you a feel for the class in terms of problem solving with as little guidance as needed.
You will write a program that will prompt the user for input 2 times. First ask the user for a temperature in degrees Farenheit, then perform a calculation on that input to convert it to Celsius. Second, ask the user for a letter and print out the sentence below but do not use endl or spaces. You'll know you've done it right if your output looks exactly like mine below.
For this second part, I want you to try and perform a mod on the variable from the first prompt. If you did the first part correctly this should give you a compiling error. You will do well in this class if you can read and understand errors like this. Try to understand why this error happened on your own first. Get comfortable looking up things you don't fully understand yet in your book. The internet is also a good resource for errors, particularly cplusplus.com. Just don't copy code from the internet, it probably won't help you and I'll probably be able to tell.
After you figure out why this error occurred, fix or remove that line from your program and explain what you found out about the error. Print out your explanation using cout.
The first part of the lab will be spent going over the solutions to the second coding exam.
Write a program that takes opens a file for output and writes a siimple message to it. However the filename must be passed by the comman-line.
For example, if you execute your program as below, your file should output your message to the file myMessage.txt
For your information: command-line arguments are covered under the book's Appendix G, but that appendix is not printed in the book. Looking at the Table of Contents it is only available through registering through the publisher's website. (A practice that I dislike). If you want to read more about command-line arguments I would reccomend finding another source, perhaps somewhere online, to supplement your knowledge.
There appears to be an article on cplusplus.com that covers command-line arguments in more detail than is required for this class, but it is a good reference for understanding this concept. Link.
This week, I will be going over how to use gdb during the lab section and you are encouraged to pay close attention and follow along. I will be operating on this executable. In order to follow along you can copy this executable into your account by executing the command:
cp ~austin/public_html/examples/lab06.cpp .
This is an exercise in advanced debugging and tracing. There are several errors with this program and I will step through the program slowly and point them out to you. You will then be asked to recreate the program without the errors.
For reference, a working version of the lab is here, but you won't be able run this version through gdb as easily so don't even try :p
Using gdb is not a requirement for this class (or even most classes in our department), but being able to use a debugger is (in my opinion) THE most powerful tool you can have as a programmer! So if you plan to go into a programming career I highly recommend practing using gdb from here on out whenever you have errors. There are different debuggers for different languages, but the concepts are all basically the same - so learn gdb! It'll help you even if younever program terminal applications in C again!
TL;DR I give you an executable program that doesn't work, and you will correct the errors and create a source file that works. In order to accomplish this you will have to use gdb.
There is only one part to today's lab. You will create a program that takes input from a file, performs some calculations, and outputs the results to another file. Your program will be taking input from a file that consists only of integers. All integers from the file should be read into an array. (You may want a marker of some sort for your array in order to determine where the values stop. We'll talk about this.) Then, simply calculate the average and standard deviation of the list of numbers and output that to another file.
To calculate the Standard Deviation
1. Calculate the average
2. Subtract the average from each number and square the result
3. Calculate the average of all the differences from step 2
4. Take the square root of the average from step 3.
For example: 1, 4, 9, 16, 25, 36, 49
1. Average = 140/7 = 20
2. (1-20)² (4-20)² (9-20)² (16-20)² (25-20)² (36-20)² (49-20)²
361, 256, 121, 16, 25, 256, 841
3. Average = 1876/7 = 268
4. Standard Deviation = √268 = 16.3707
Write a function that takes one integer parameter. Then calculate the fibonacci number of that parameter and return that result to main(). For example: If you pass 7 to this fibonacci function, it should return the 7th fibonacci number, 13. The fibonacci sequence is defined as:
fib(n) = fib(n-1) + fib(n-2). fib(0) = 0, fib(1) = 1.
For the second part of the lab, solve any two problems from Project Euler. You must solve these problems using programs you've written yourself. In your programs, use functions to perform meaningful calculations. To check your answers you need to create an account on Project Euler to verify you've gotten the correct answer.
Write a program that generates a random number for the user to guess. Select a range and tell the user to guess in that range. If the user enters a number outside of your range tell them to try again. When the user makes a guess, tell them if their number was too high or too low. When a number is too high, update your max and don't let the user enter a number higher than that. Do the opposite if a number is too low.
Give the user a set number of attempts to guess the number, perhaps 10 tries. If the user guessed the number, indicate that and stop asking for more guesses. Otherwise, if the user can't guess the number in enough tries, let them know they missed out.
The second part of today's lab is to learn how to format output. You can format output with cout, though the preferred way is using the printf() function. Derrick has a page of examples for formatting either way; check it out here.
You will be formatting a multiplication table from 1*1 up to 10*10. I will show an example before lab, and you will need to nest loops for this as well.
Since we couldn't last week, the first part of this lab is to create directories for all your coursework that you'll be doing throughout the quarter. It is important that you know how to do this because all your assignments will be graded from these folders. This is also an exercise to help you become more comfortable navigating the terminal.
For this to work properly, you must use the command
mkhwdir cs221 from your home directory. NOTE: If you already have a directory named cs221 you must rename it otherwise your folder will not have the proper permissions. Within the cs221 folder that is created you will create 2 separate directories for homework and labs named homeworks and labs respectively. Also, within each of those folders you must create a corresponding hw/lab folder each week so my script can find it easily. Name each of those hw01, hwo2, etc. for homework and lab01, lab02, etc for labs.
If done correctly your cs221 folder should look like this when you type the command
ls -l ~/ and
ls -l ~/cs221/homeworks/ and
ls -l ~/cs221/labs/
And within each of these you will put your .cpp files for your assignements.
This part of the lab will introduce you to boolean flags, compound boolean expressions, and random number generation. You will first ask the user if he/she would like to enter a number or use a random number in the range 0-100. If the user answers positively, that is 'y' or 'Y', set the flag to true, otherwise set the flag to false. After that, set an integer variable either from the user or the rand() function, depending on the flag. Then based on the integer, output a message stating the letter grade associated with that integer (A:100-90, B:89-80, C:79-70, D: 69-60, F:59-0).
In order to better understand how switch statements are like if statements, you will write a program that uses both. Prompt the user for a double and a character. If the double is a perfect square, say so. If the double is a even number, say so. If the character is a vowel, say so. If the character is a consonant, say so. You must use a switch statement to do one of these and for the other use an if statement. UPDATE: you should use an if statement to find out what to print for the double and you should use the switch for determining if a letter is a vowel or a consonant. Don't forget about type casting! It might be necessary for this problem!
Your program should generate output like the above if done properly.
For this part of the lab you will create a program that generates 20 lines of output. The content can be whatever you wish, all I ask is that you key it in yourself and dont paste it in. The purpose of this is for you to see how text is output to the terminal and to hopefully notice some things about getting what the program outputs to match what you want.
Most programs we write in this class will have at least the following structure. It wouldn't be a bad habit to start all of your programs like this.
Just put what you want to be output where the comment is and this should be an quick assignment.
The final part of this lab is to write a program that takes input from the user and produces a meaningful output. This program will prompt the user for some input, perform a calculation, and output the result. I would like you to practice by calculating the area of a rectangle as below. To receive credit though, you have to calculate the area of a circle.
The formula you should use for the area of a circle is π * r * r where r is the radius and π is 3.14159.
Remember when taking user input you must use an appropriate variable.
Last updated: 12/01/15
|Lab 01||Lab 02||Lab 03||Lab 04||Lab 05||Lab 06||Lab 07||Quiz 01||Quiz 02||Quiz 03||Quiz 04||Quiz 05||Quiz 06||Quiz 07||Hw 01||Hw 02||Hw 03||Hw 04||Hw 05||Exam I (written)||Exam I (coding)||Exam II (written)||Exam II (coding)||Final Exam|
cp ~austin/public_html/examples/*.cpp .
The six parts of a variable