Assignment #3 - College of Computing and Software …



CSE 1321L: Programming and Problem Solving I Lab

Assignment 4

Loops

Assignment Outcomes

By completing this assignment, students will be able to:

1) Design programs that leverage loops to solve problems

2) Apply concepts from earlier in the semester, including conditional statements

3) Generate and use random numbers

4) Solve problems of increasing complexity

NOTE: Assignment 4 should utilize only the following topics: Variables, Assignment, Input/Output, Selection Structures and repetition structures (i.e. loops).

Program 1: Hungry? In the United States, we tend to be obsessed with eating large animals. We have sayings of “I’m so hungry, I could eat a horse” and ask meaningful questions like “How do you eat an elephant? One bite at a time!” For this assignment, you’re going to put things in perspective. You will ask the user how many pounds the object they will be eating weighs, then calculate both how many bites it takes to eat the object and how long it takes total to eat it. Assume that each bite reduces the object you are eating by 0.05 pounds (lbs). Design (pseudocode) and implement (source code) for this program. BTW, an African Bush elephant can weight 13,000 lbs. How many bites would that be?

Sample run 1:

What type of thing are you eating? cheese

How much does it weigh (in lbs)? 1

You take bite #1 of the cheese that now weighs 0.95 lbs

You take bite #2 of the cheese that now weighs 0.9 lbs

You take bite #3 of the cheese that now weighs 0.84999996 lbs

You take bite #4 of the cheese that now weighs 0.79999995 lbs

You take bite #5 of the cheese that now weighs 0.74999994 lbs

You take bite #6 of the cheese that now weighs 0.6999999 lbs

You take bite #7 of the cheese that now weighs 0.6499999 lbs

You take bite #8 of the cheese that now weighs 0.5999999 lbs

You take bite #9 of the cheese that now weighs 0.5499999 lbs

You take bite #10 of the cheese that now weighs 0.49999988 lbs

You take bite #11 of the cheese that now weighs 0.44999987 lbs

You take bite #12 of the cheese that now weighs 0.39999986 lbs

You take bite #13 of the cheese that now weighs 0.34999985 lbs

You take bite #14 of the cheese that now weighs 0.29999983 lbs

You take bite #15 of the cheese that now weighs 0.24999984 lbs

You take bite #16 of the cheese that now weighs 0.19999984 lbs

You take bite #17 of the cheese that now weighs 0.14999984 lbs

You take bite #18 of the cheese that now weighs 0.099999845 lbs

You take bite #19 of the cheese that now weighs 0.049999844 lbs

Program 2: Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack. When the player attacks, it will pick a random number between 0 and up to the maximum damage the player does, and then subtract that from the monster. The same thing happens when the monster attacks the hero, but damage is to the hero. The program should display rounds and the HP of the hero and monster each round. If the hero or monster dies, it should print that this happened and should NOT continue (i.e. no extra text). To learn how to create random numbers, see the appendix.

Sample run 1:

Enter the hero's starting hit points: 50

Enter the damage the hero’s weapon does per strike: 20

Enter the monster's starting hit points: 40

Enter the monster's damage per strike: 15

====== ROUND 1 ======

Hero attacks for: 10

Monster has 30 HP left

Monster attacks you for: 1

You have 49 HP left

====== ROUND 2 ======

Hero attacks for: 18

Monster has 12 HP left

Monster attacks you for: 7

You have 42 HP left

====== ROUND 3 ======

Hero attacks for: 0

Monster has 12 HP left

Monster attacks you for: 14

You have 28 HP left

====== ROUND 4 ======

Hero attacks for: 18

Monster has -6 HP left

The monster dies and you earn 5 XP

Battle ends...

Sample run 2:

Enter the hero's starting hit points: 50

Enter the damage the hero’s weapon does per strike: 10

Enter the monster's starting hit points: 40

Enter the monster's damage per strike: 20

====== ROUND 1 ======

Hero attacks for: 1

Monster has 39 HP left

Monster attacks you for: 6

You have 44 HP left

====== ROUND 2 ======

Hero attacks for: 5

Monster has 34 HP left

Monster attacks you for: 1

You have 43 HP left

====== ROUND 3 ======

Hero attacks for: 8

Monster has 26 HP left

Monster attacks you for: 8

You have 35 HP left

====== ROUND 4 ======

Hero attacks for: 4

Monster has 22 HP left

Monster attacks you for: 5

You have 30 HP left

====== ROUND 5 ======

Hero attacks for: 7

Monster has 15 HP left

Monster attacks you for: 1

You have 29 HP left

====== ROUND 6 ======

Hero attacks for: 7

Monster has 8 HP left

Monster attacks you for: 9

You have 20 HP left

====== ROUND 7 ======

Hero attacks for: 0

Monster has 8 HP left

Monster attacks you for: 14

You have 6 HP left

====== ROUND 8 ======

Hero attacks for: 4

Monster has 4 HP left

Monster attacks you for: 11

You have -5 HP left

You are killed by the monster and lose 10 gold.

Battle ends...

Program 3: National Debt. One thing that should worry anyone is financial debt. If you’re like most others, you have credit card debt, student loans and so on. Our nation has similar debts, including debt to its citizens (e.g. bonds), debt to social programs, and debt to other nations. If you’d like to see where we stand, check out . If you’d like to see how we compare to other nations, check out the “World Debt Clocks” in the upper-left corner.

How do these debt clocks work? It’s exceptionally complicated, but a lot of national debt comes from issuing Treasure Notes (e.g a 10-year Treasury Note) that currently yield 3.6% interest. Your task is to write a program that prompts the user for a national debt (expressed in billions of dollars), the interest rate, and calculate how much debt is generated per year (assuming no debt is paid off by the GDP) over a 10-year period. For example, if a country currently has “1,000 billion” (i.e. a trillion) dollars of debt, it will generate 36 billion dollars of additional debt in a year. The next year, the debt of 1036 billion would generate 37.296 billion additional dollars of debt, totaling 1073.296 dollars of debt.

Sample run 1:

Enter the starting debt in billions: $1000

Enter the interest rate: 3.6

***** Year 1 *****

Debt generated this year is $36.0

Accumulated debt from interest is now $36.0

Total debt is now $1036.0 billion dollars

***** Year 2 *****

Debt generated this year is $37.295998

Accumulated debt from interest is now $73.296

Total debt is now $1073.296 billion dollars

***** Year 3 *****

Debt generated this year is $38.638657

Accumulated debt from interest is now $111.934654

Total debt is now $1111.9347 billion dollars

***** Year 4 *****

Debt generated this year is $40.029648

Accumulated debt from interest is now $151.9643

Total debt is now $1151.9644 billion dollars

***** Year 5 *****

Debt generated this year is $41.470715

Accumulated debt from interest is now $193.43501

Total debt is now $1193.435 billion dollars

***** Year 6 *****

Debt generated this year is $42.96366

Accumulated debt from interest is now $236.39868

Total debt is now $1236.3987 billion dollars

***** Year 7 *****

Debt generated this year is $44.51035

Accumulated debt from interest is now $280.90903

Total debt is now $1280.909 billion dollars

***** Year 8 *****

Debt generated this year is $46.112724

Accumulated debt from interest is now $327.02176

Total debt is now $1327.0217 billion dollars

***** Year 9 *****

Debt generated this year is $47.77278

Accumulated debt from interest is now $374.79456

Total debt is now $1374.7946 billion dollars

***** Year 10 *****

Debt generated this year is $49.492603

Accumulated debt from interest is now $424.28717

Total debt is now $1424.2871 billion dollars

Submission:

Part 1: Pseudocode:

1. Review the assignment submission requirements and grading guidelines.

2. Upload the pseudocode files (Word doc or PDF) to the assignment submission folder in Gradescope.

3. The files must be uploaded to Gradescope by the due date.

4. The Pseudocode must be complete and following the standards listed on the FYE website.

Part 2: Source Code:

1. Review the assignment submission requirements and grading guidelines on the FYE website.

2. Upload the source code files to the assignment submission folder in Gradescope.

3. The files must be uploaded to Gradescope by the due date.

Appendix

Random numbers: for most languages, you have a special function you can call to get a random number. Often, this returns a value between 0.0-1.0 that you then have to multiply by the range you want. For example, if you multiply a number between 0.0-1.0 by 30, you get a result between 0.0-30.0. However, because Java and C# are Object-Oriented Programming languages, there are special objects you have work with, each of which have multiple (useful) methods.

C++ is funky. You call rand() and get a large number between 0-RAND_MAX, but every time you run the program, you get the same number! So, we have to “seed” the random number generator with something that is different each time we run our program (in this case, the time since January 1st, 1970). To get a number between 0.1 and 1.0, you have to divide that number by RAND_MAX.

In Java, it looks like (import java.util.Random;) :

Random generator = new Random();

float randNum = generator.nextFloat();

int randNum2 = generator.nextInt(10);

System.out.println (randNum + " " + randNum2);

While in C#, it looks like:

Random generator = new Random();

int randNum = generator.Next(1, 10); // 1-10

float randNum2 = (float)generator.NextDouble()*30; // 0.0-30.0

Console.WriteLine(randNum+" "+randNum2);

In C++ (and EVERYONE SHOULD READ THIS):

#include

#include

#include

using namespace std;

int main() {

// Here, we'll get the same number each time we run the program

// A big number between 0 and RAND_MAX. You should seed it first!!!

// Note: this is just to show you get the same number each time.

int number = rand();

cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download