CSci 1113: Introduction to C/C++

Programming for Scientists and Engineers

Homework 2

Spring 2016

Due Date: Friday, Feb. 19, 2016 before 6:00pm.

Instructions: This is an individual homework assignment. There are two problems worth

20 points each. Solve each problem below by yourself (unlike the labs, where you work

collaboratively), and submit each solution as a separate C++ source code file. Here are a

few more important details:

• You submit the correct file(s) through Moodle by the due deadline.

• You should follow the example input and output formats given in each problem description.

(match the input and output format exactly)

• Regardless of how or where you develop your solutions, your programs compile

and execute on cselabs computers running the Linux operating system.

Problem A: Bet simulation (20 points)

Suppose a suspicious person on the street offered you two games to play:

1. Flip a dozen (12) pennies and you get one dollar for every head.

2. Roll two dice (6 sided) and get you the sum of both in dollars.

Which game is better? Pretend you don't know the answer (even if you do) and write a computer

program to simulate both games. Run each game 1,000,000 times and calculate your average income

per game.

(Reminder: this line should be run once and only once in your code):

srand(time(0)); // you may need to say: srand(time(NULL));

Example 1 (user input is underlined):

Average income from penny game: 5.99943

Average income from dice game: 6.99914

Example 2 (user input is underlined):

Average income from penny game: 6.00277

Average income from dice game: 6.999

Example 3 (user input is underlined):

Average income from penny game: 6.00017

Average income from dice game: 7.0007

When you are done, name the source code file <username>_2A.cpp. Here you replace

<username> with your U of M email address; for example, if your email address is

smithx1234@umn.edu, your file should be named smithx1234_2A.cpp. Then submit your

program using the HW2A submission link in Moodle.

Problem B: Better integer calculator (20 points)

This problem will improve the integer calculator we did in lab 2. Allow as many operations (still just

+, -, * and / but no parenthesis) until a '#' character is reached instead of an operation. Ignore order of

operations as well, and always assume the left operations are done first. For eample: 2+3/5 = 5/5 = 1.

You should then show the result (you do not need to show the original equation). As this is an integer

calculator you may assume they enter only integers and valid operations (or the stopping character).

Example 1 (user input is underlined):

Enter an equation: 2+3*2#

10

Example 2 (user input is underlined):

Enter an equation: 2+3*2/5000+94#

5

Example 3 (user input is underlined):

Enter an equation: 2/3#

0

Example 4 (user input is underlined):

Enter an equation: 7#

7

(5 points extra credit)

You can also attempt it to detect an error if you enter just a '#'. This is more difficult, so I am moving it

from a requirement to extra credit.

Example 5 (user input is underlined):

Enter an equation: #

Error

When you are done, name the source code file <username>_2B.cpp. Here you replace <username>

with your U of M email address; for example, if your email address is smithx1234@umn.edu your file

should be named smithx1234_2B.cpp. Remember to follow this naming convention diligently. Then

submit your file using the HW2B link on Moodle.

代写 CSci 1113: Introduction to C/C++

代写 CSci 1113: Introduction to C/C++