Announcements

  • (12/01) Well, grades are finally posted on the website. As always if you have any questions I'd be glad to explain why your grade is what it is. I'm not sure when your official grades will show up in myCSUB, but I expect it won't be long. I hope the class was acceptable and no one is blindsided by their grades. For those truly interested I will put up the prompts for the coding exams and also the solutions. (don't count on them being out in the open forever). I don't have a class next quarter, so if you want to see your final exam (see, not keep) you'll have to get in touch with me. Anyway, I really enjoyed hanging out with you guys this quarter, I'm gonna miss being able to do that... If you ever see me again please say hi, it'd brighten my day! Enjoy your month off and good luck with your college careers!
  • (11/30) I'm still working out the final grades - please be patient. I will update the grades on the website and on mycsub roughly around the same time so feel free to check either. I won't have the grades ready until late tonight though. Look for this message to change and you'll know the grades have been posted.
  • (11/23) Just a reminder to those of you who are taking the final tomorrow, you should meet me at my office around 6:00 PM and I'll find a room for you all to take it in. No one made other special arrangements with me so if I don't see you today or tomorrow at 6:00 PM you will not be able to take the final so please be careful.
  • (11/20)

    There was a HUGE mistake regarding the time for Monday's final! It is from 02:00 PM - 04:30 PM! I apologize for the mistake. I hope everyone becomes aware of this and double checks their schedules to verify that this time doesn't cause any conflicts or other issues.

  • (11/08) Grades for all assignments up until the written part of exam II are now posted, along with your two grade percentages. Be sure to read the note at the bottom of the page explaining the last column! Hw05 is also posted and will be due next Monday.
  • (10/31) Students, remember that hw04 was due last night and will now be late if you haven't submitted it. Also, hw05 will be posted sometime next week and will be due the following week - I will be mindful that the exam is this week so I will give you enough time to complete hw05 without interfering with your studying for the exam. Don't forget to study this weekend and bring questions to the review session on Monday. Have a safe and happy Halloween!
  • (10/23) Homework 04 is now posted. It focuses on functions, and will be due a week from today, at midnight. That way you have an entire week to work on it.
  • (10/19) I have updated the grades to include all assignments to date. Also you will see a percentage next to your grades now. That percentage represents your current grade given ONLY what's been assigned up to this point - it does not take into account future assignments or exams.
  • (10/11) Grades have been updated. They now include all labs and the written exam. Grades should be completely up to date at some point during the week.
  • (10/04) I've added the list of topics that will covered in the first exam under the examples/notes tab and have added links to the various examples we've done in class.
  • (10/03) I've updated the syllabus to say that assignments are now due before midnight instead of before the following class.
  • (09/24) I think today's lab went very well and I was happy to see you all working so hard on it. I understand it was difficult and I do want everyone to have the chance to turn it in on time so I'm extending the due date to Friday at midnight so everyone has more time to get all parts done in time for full credit. This also gives us the opportunity to talk about it in class tomorrow.
  • (09/23) I've decided to make all homeworks due on Wednesdays from now on, and I've updated the syllabus to say as much. I haven't posted homework 02 yet, but it will be due next Wednesday.
  • (09/17) There were some issues with the first homework so I've extended the due date to Wednesday before class. I've also fixed the issues, it should be complete now. Finally, the first quiz is tomorrow - don't forget!
  • (09/16) If you can understand CS 150 on your own, the professor for that class would prefer that you do that instead of going to his class - it could be very full.
  • (09/14) Welcome!
Topics Reading
Week 01
09/14 - 09/18
Introductions
Linux Operating System
Program I/O
Prompts
Programming Terminology
Variables & Literals
Six parts of a variable

Ch. 02-03
Week 02
09/21 - 09/25
If-else statements
Input validation
Nesting if statements
Switch statements?
Boolean expressions
Boolean flags
Scope
Ch. 04
Week 03
09/28 - 10/02
Switch statements
Better input validation
Loops
Ch. 04-05
Week 04
10/05 - 10/09
Review
Functions
Exam I
Week 05
10/12 - 10/16
Functions
Passing by value/reference
Global variables
Default arguments
Ch. 06
Week 06
10/19 - 10/23
Arrays
Two dimensional arrays
File I/O
C-strings
Ch. 05, 07, 10
Week 07
10/26 - 10/30
Pointers
More functions, arrays, and file I/O
Week 08
11/02 - 11/06
Review
Exam II
Ch. 08-09
Week 09
11/09 - 11/13
Structures
Veteran's Day Wednesday - No class
Sorting arrays
Command-line arguments
Ch. 11-12
Week 10
11/16 - 11/20
Extra credit lab
Classes
Final Review
Week 11
11/23 - 11/25
Final Exam

November 23, 2015 @ 02:00PM in RM 311

Homework 05 - Preparation for the final

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.

Homework 04
Part 1 - Completing functions

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 .

Part 2 - Reference parameters

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 printUp(int);
void printUpRef(int &);
void printDown(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.

Homework 03
Part 1 - Create a menu

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.

Part 2 - Running total

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.

Part 3 - Calculating prime numbers

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.

Homework 02

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:

  • First declare several boolean flag variables.
  • Then ask the user a yes or no question and take read their input. Set each flag as true or false based on whether the user types 'y' or 'n' for each question you want to track.
  • After you've prompted the user and set ALL the flags decide how to show the result to the user. So if one flag is set a certain way, you will output a certain answer or check the other flags for a more specific answer.

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.

Homework 01

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.

Part 1

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.

Part 2

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.

Lab 07
Part 1 - Coding Exam Solution

The first part of the lab will be spent going over the solutions to the second coding exam.

Part 2 - Practicing command line arguments

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

./lab07 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.

Lab 06
Debugging with gdb

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.

Lab 05
Part 1 - File I/O with arrays

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

Lab 04
Part 1 - Fibonacci function

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.

Part 2 - Project Euler

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.

Lab 03
Part 1 - Tracking max/min and nesting loops

This week you only have to do one part of the lab to get credit!

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.

Part 2 - Formatting input

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.

Lab 02
Part 1 - Create assignments directories

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 commandls -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.

Part 2 - Compound boolean expressions

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).

Part 3 - Switch vs. If-else

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.

Lab 01

Part 1 - Program output

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.

Part 2 - Program Input

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

Percentage breakdown

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
Points Possible101010101010101010101010101010101010103620362058
0129101010090910080506091008100910101009103119331854
0174090910100910080606080705060710101008102517161646
0182101010090810--07060610100808100909----2720282044
051610101009--101008060608--070909100908101917251636
1157100910081010100410101010081010101010102418242044
1487101010101010100204080906081010100909102119291949
1615100910080910100610080508100710101010102820281953
2023100910081010100400050304060108090904102315060915
2071100809050910100610040708050810090405101217201441
2685100810101010100506070709070810101010101818261546
2869100910001010100806020305050609090807102117231442
2919100810050810100804060206070810100705101311171034
3023101010081010100804060806040808101009102220182042
3227090810091010100804080706040809091010102316151740
3358100910061010--0606060809060709090808102720231342
3521101010080810100508070910090909090909102520271957
3775100810----10--060406--------101007----1515----0
428010080905051000020602050908--090908----13--131219
4603--1010100710090804040908090810090907102714261347
4835101010100910100406040410090610100804102118181640
5840101010100810100808100307090510090802102517161442
6059100910070810--06100404060606100909--101519121721
6138101010100810100602050504050508101008101919170943
62191008101010101004060409090805081010--101712131026
640510101005--10--04040505------081004----2014----0
7255101010070710100206040503080708090708101917201143
7593101010100810100608080705050309100909101917171034
7775100910061010100404100606070609101009102219291838
7831101010100710080608050305050709090909102420141636
8218101010090810100810060908100810100909102717271762
8340100910080510--050304050407--08090301--111511120
8427101010101010100810080910101010101010103020351971
844910101005--10--05100409060005101003----3320191246
873810081000091008060404--0504050809--00102111131026
8848101010091010070610--1010091010100810102919331950
9277100910051010100504100805060710090304101414131728
9667100809081010100206060607040610091005102017181240
9749101010100910100708040806091010091010102519202044