|
Contact Mr. Green
|
Computer Programming
Course
Information
Textbook Files
Heading for all programs
// Computer Programming
// First and Last Name
// date
// filename.cpp
//
// Description of program
Craps Executable
Homework
Due Fri, April 4
Write a program that will ask the user for at least 25 sets of 4 or 5 pieces of data. The data should be written to a text file. Examples of data include:
- wins, losses, and ties for each of the past 25 years for the Washington Redskins
- the top 10 grossing movies and what they grossed (dollar amount) for each of the past 25 weeks
- ask 25 of your friends the same 4 or 5 questions (similar to what we did in class, but different questions of course
Remember to use cin.get() followed by cin.ignore() if the user is entering data that contains white space, such as a movie title.
Due Tue, March 18
Important: Complete your assignment below and print your code before coming to class.
Part I
In class we wrote a program called monthlyrainfall.cpp that keeps track of the monthly rainfall for a year. We declared an array of floats of size 12 and then asked the user for each month's rainfall. The code looked something like this:
const int SIZE = 12;
float rainfall[SIZE];
// fills rainfall array with user input
for (int i = 0; i < SIZE; i++)
{
cout << "Enter rainfall for month " << i+1 << ": ";
cin >> rainfall[i];
}
// prints rainfall array
for (int i = 0; i < SIZE; i++)
cout << "Month " << i+1 << ": " << rainfall[i] << endl;
Expand this code to keep track of the (1) maximum monthly rainfall, (2) minimum monthly rainfall, and (3) average monthly rainfall.
Part II
Modify your code above to keep track of the same statistics (max, min, and avg) for the daily rainfall for April (30 days). Save your new program as dailyrainfall.cpp. Your code changes should be minimal if you did Part I correctly.
Important: Bring your code to class already printed. If you need to print at school please do so at home or before homeroom. You will not be able to print your code in class. Failure to submit your printed code will result in a zero for this assignment. Late assignments will not be accepted.
Due Mon, March 10
Group A: Continue working on your program that will:
- strip out all spaces and punctuation from a string of characters (already given)
- capitalize all the letters of the input string (already given)
- NEW: Check to see if the phrase is a palindrome. Compare the first letter to the last letter. Then compare the 2nd character to the next to the last character, etc. Repeat this until you get to the middle index of the string.
Example:
input string: Madam I'm Adam.
spaces and punctuation stripped: MadamImAdam
all caps: MADAMIMADAM
Is the string a palindrome: Yes, it is a palindrome!
Group B: Continue working on HTML generator. You should ask the user for the specific input. i.e. filename, title, paragraph text, etc. Your C program should output the HTML code to the specified file name.
Due Fri, Feb 22
- Group 1: Read Section 10.2 and answer 10.2 questions
- Group 2: Read Chapter 11 and do Problem 11.2.1 (p. 215) and Problem 11.3.2 (p. 223)
Due Wed, Feb 20
- Section 10.1 questions and Problem 10.1.1
Due Fri, Feb 8
- p. 174, Project 9-3 - 2nd chance to complete correctly
Due Wed, Feb 6
- p. 174, Project 9-3 (make sure to include the requirements discussed in class)
Due Mon, Feb 4
- Dice Probabilities: Create a simulation whereby you will roll two dice 100,000 times, keeping track of the total rolled each time. After running your program, you should be able to show tabulated output that includes both the number of times each number was rolled as well as the percentage of total rolls. Sample output might look like this:
total on dice # rolled percentage
2 2175 2.2%
3 5254 5.3%
...
Due Wed, Jan 23
Due Fri, Jan 18
- Read and complete exercises in ch. 9
Craps Game - due Mon, Jan 14
The Craps Game does not involve gambling. It is a simplified version that has the following rules:
- A player rolls two dice. (This can be simulated with adding two random numbers between 1 and 6 together)
- If the first roll (the come-out roll) is a 2, 3, or 12, the player "craps out" or loses.
- If the come-out roll is a 7 or 11, it is a called a natural or a win.
- Any other roll (not a 2, 3, 7, 11, or 12) on the come-out roll is called point.
- If the player rolls point, then the player tries to roll point again in which case it is a win. If the player rolls 7 before point is rolled again, the player loses. Any other roll means roll again.
- When the player finishes rolling (wins or loses), ask the player if they would like to play the game again.
Due Wednesday, December 12
- Complete Practice Midterm Exam
Due Tuesday, December 4
- hailstone.cpp An interesting (yet unsolved) question in mathematics concerns "hailstone numbers." This series is produced by taking an initial integer, and if the number is even, dividing it by 2. If the initial number is odd, multiply it by 3 and add 1. This process is repeated. For example, an initial number of 10 produces:
10, 5, 16, 8, 4, 2, 1, 4, 2, 1, ...
- An initial value of 23 produces:
23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, ...
Due Monday, November 26
- The following code generates a random number between 1 and 3. You must include math.h in your compiler directive.
rand()%3 + 1
This code will generate a random number between 15 and 30:
rand()%16 + 15
At the beginning of your main() function, you must seed your random number generator, so that it is truly random. This makes sure that the first number generated is always different. time(NULL) is a different number each time you call it--the number of seconds that have passed since the new year of 1970. You must include time.h for this.
srand ( time(NULL) );
nim.cpp
The game starts with a random number of stones between 15 and 30. Two players alternate turns and on each turn may either take 1, 2, or 3 stones from the pile. The player forced to take the last stone loses.
Write a program that allows the user to play Nim against the computer. The specification: The computer generates the random number of stones, and the user is allowed to go first. On each user turn, the program displays the current number of stones and asks for the number to be removed. On each computer turn, the program displays the number of stones and the number taken (a random number from 1 to 3.) When the last stone is drawn, the program displays the winner. Code should be included to prevent the user or the computer from trying to take an illegal amount of stones. In particular, neither should be allowed to take three stones when there are only 1 or 2 left.
Due Monday, November 19
-
necklace.cpp An interesting problem in number theory is sometimes called the "necklace problem". This problem begins with two single-digit numbers. The next number is obtained by adding the first two numbers together and saving only the ones-digit. This process is repeated until the "necklace" closes by returning to the original two numbers. For example, if the starting numbers are 1 and 8, twelve steps are required to close the "necklace":
1 8 9 7 6 3 9 2 1 3 4 7 1 8
Write a program that asks the user for two starting numbers, and then displays the sequence and the number of steps taken. The program output should look similar to:
Enter first number: 1
Enter second number: 8
1 8 9 7 6 3 9 2 1 3 4 7 1 8
Your numbers required 12 steps.
Due Thursday, November 15
- statistics.cpp Write a program that allows the users to enter a series of numbers one by one as follows. When the user enters a negative number, the program should print 1) the total of all the numbers 2) the average of the numbers 3) the minimum (smallest) number and 4) the maximum (largest) number. An example follows:
Enter a number (negative to quit): 3.5
Enter a number (negative to quit): 5.2
Enter a number (negative to quit): 2.5
Enter a number (negative to quit): 1.9
Enter a number (negative to quit): -5
The total is: 13.1
The average is: 3.275
The maximum is: 5.2
The minimum is: 1.9
Due Tuesday, November 13
- In class you wrote a program using a for loop that prints the even numbers from 2 up to a number MAX (which is declared as a constant). Write the same program using a while loop (called evenwhile.cpp) and using a do-while loop (called evendowhile.cpp)
- Write a program, using any type of loop, that finds the running total of the previous problem (even numbers from 2 up to MAX). For example if the if MAX is 10, then your program would add 2+4+6+8+10 which is 30 and print that result to the screen. Call your program evensum.cpp. Hint: Use a variable that keeps track of the running total each time through the loop. Each time through the loop it must add the new number in the sequence to the current total.
Due Thursday, November 8
- Write a program called fibonacci.cpp that prints the Fibonacci Sequence to the screen. Determine the 100th number in the sequence.
- Write a program consisting of nested for loops that gives the following output:
5 0
5 1
5 2
4 0
4 1
4 2
3 0
3 1
...
etc.
Due Tuesday, November 6
- p. 142, Section 8.1 Questions, #6-7. #6 should be called sequence1.cpp and #7 should be called sequence2.cpp.
- p. 142, Problem 8.1.1 (oddloop.cpp)
Due Wednesday, October 31 -- Booo!
- Test on Chapter 7.
- Past homework has been graded and can be picked up in room 220 anytime.
Due Monday, October 29
- Complete the snack bar program (call it snackbar.cpp). Your program should print a simple menu of four items and ask the user to make a selection. After choosing something from the snack bar, your program should tell the user how much they owe. You must use a switch structure.
- Test on Chapter 7 will be Wednesday, October 31.
Due Thursday , October 25
- Complete a logic program where there are three variables. ans1 and ans2 are char and ans3 is an int. The rules are as follows:
- ans1 is 'a' --> "blah"
- ans1 is 'b' --> ans2 is 'y' or 'z' --> "yeah"
- ans1 is 'c' --> ans2 is 'x' or 'z' --> ans3 is 2 --> "omg"
- ans1 is 'c' --> ans2 is 'y' --> ans3 is 2 or 3 --> "ok"
- anything else --> "foo"
Due Tuesday, October 23
- Write a program that asks the user two questions. After the user answers the two questions, your program should tell the user what percentage of the problems he/she got correct.
- Sample output:
What is the capital of Tanzania?
A) Nairobi
B) Kampala
C) Dar es Salaam
D) Washington, DC
Please enter your answer: C
The sky is blue. (T/F): F
You scored a 50%.
Due Friday, October 19
- Write a program that will print out the values of the boolean expressions in Workbook Section 7.1. In particular, your program should print out the values of the expressions in the section entitled "Boolean Expressions" and #4 in the "Completion" section.
Due Tuesday, October 16
- Read and complete exercises, Chapter 7
In-class quiz - quizone.cpp
- Declare two character arrays, foo and blah, both of length 20.
- foo should be "Caron"
- blah should be "Butler"
- Copy the contents of blah to foo.
- Print the third letter of foo to the screen using the [] notation.
Due Friday, October 12
- Write down the ASCII values of each letter in your first and last name (including the space). Then, add them together to get the sum total.
- p. 97 6.1 Questions
- p. 106 6.2 Questions
- p. 97 Problem 6.1.1 or 6.1.2
Due Wednesday, October 10
- Read and complete exercises in chapter 6
In-Class Homework Thurs, Sept 27
- pizza.cpp
- The cost of making a pizza at Alverto's Pizza Shop is as follows:
- A labor and rent cost of $1.20 per pizza, regardless of size.
- The cost of materials that varies by the size of the pie according to the formula $0.05*diameter*diameter.
- Write a program that allows the user to enter a diameter, and then displays the cost of making a pie of that size. The program output should look similar to:
Enter the size of the pizza: 10
The cost is $6.20
- time.cpp
- Write a program that allows thte user to enter a time in minutes and then displays the time in hours:minutes format. The program output should look similar to:
Enter the number of minutes: 327
This is 5:27
- Be sure to consider times where the number of minutes left over is fewer than 10. For example, 184 minutes should display 3:04. (Hint: use the modulus operator.)
- change.cpp
- Write a program that displays the coins necessary to make the change entered by the user. The change can be made up of quarters, dimes, nickels, and pennies. Assume that the amount of change is less than $1.00, and use the minimum number of coins. The program output should look similar to:
Enter the amount of change in cents: 68
Change:
quarters: 2
dimes: 1
nickels: 1
pennies: 3
- Test review: Remember your test will be on Wednesday, October 3. From chapter 1, you only need to know basic computer architecture (section 3). Don't worry about details of the microprocessor. Refer to your notes to review for this. For chapter 2, you only need to know binary to decimal (and decimal to binary) number conversions. You should know chapters 3 through 5 in their entirety. In addition to written questions on the test, there will be one or two programs you will need to write (similar to those done in class or for homework). To study for the written questions on chapters 3 through 5, feel free to use the practice tests below. Don't look at the solutions until you've finished the practice test!
Due Thurs, Sept 27
- WB Sections 5.1 and 5.2
- p. 87-8 Projects 1 and 9
|