Last Updated: 2015-06-03 Wed 13:57

CS 222 HW 1: Brush-up on Basics

CODE DISTRIBUTION

CHANGELOG:

Wed Jun 3 13:56:20 EDT 2015
Fixed small problem with rendering the break even trips equation.

Table of Contents

1 Grading

Grading for this HW will be divided into two distinct parts

  • Part of your grade will be based on passing automated test cases
  • Part of your grad will be based on a manual inspection of your code and analysis documents by the teaching staff to determine quality and efficiency.

1.1 Automated Tests (50%)   grading

  • A testing script or Makefile will be provided to detect logic errors in your code
  • Tests may not be available on initial release of the HW but will be posted at a later time.
  • Tests may be expanded as the HW deadline approaches.
  • It is your responsibility to get and use the freshest set of tests available.
  • Tests will be provided in source form so that you can discern what tests are doing and where you are failing. This may require you to study the testing code carefully.
  • It is up to you to run the tests to determine whether you are passing or not.
  • Most of the credit will be divide evenly among the tests; e.g. 50% / 25 tests = 2% per test. However, the teaching staff reserves the right to adjust the weight of test cases after the fact if deemed necessary.
  • Code that does not compile and run tests according to the specified command line invocation may lose all automated testing credit. Graders will usually try to fix small compilation errors such as bad directory structures or improper use of packages. Such corrections typically result in a loss of 5-10% credit on automated testing. However, if more than a small amount of error to fix problems seems required, no credit will be given.

1.2 Manual Inspection (50%)

  • Teaching staff (GTAs and professors) will manually inspect your code and analysis documents looking for a specific set of features.
  • Most of the time the requirements for credit will be posted along with the assignment though these may be revised and refined as the the HW deadline approaches. The specific sections pertaining to grading are marked with grading in this document.
  • Credit will be awarded for good coding style which includes good indentation, sensible variable names, comments explaining what is happening, and use of any auxiliary functions that are not part of the specification.
  • Some credit will be assigned for designing your program according to the given specification, for instance using a designated algorithm, structuring your program in a certain fashion, or utilizing a required programming element.
  • Some credit will be awarded for turning in any analysis documents that are required by the HW specification. These typically involve analyzing how fast a method should run or how much memory a method requires and are reported in a text document submitted with your code.

2 HW Setup and Submission

2.1 HW Distribution

Most programming assignments will have some code that is provided and should used to complete the assignment. A link to a zipped archive containing provided code is at the top of each assignment (including this one).

Download it and extract all its contents; by default it will create a directory (folder) named distrib-hw1.

If you are unfamiliar with how to zip and unzip things, see the sections on this in the Setup Tutorial.

2.2 HW Directory

Net ID: The first part of your GMU email address and the username you employ to gain access to mason systems like PatriotWeb. My mason ID is ckauffm2 because my official GMU email address is ckauffm2@gmu.edu.

Rename the distrib-hw1 directory to NetID-hw1 where NetID is your mason Net ID. I would rename it to ckauffm2-hw1. This is your HW directory. Everything concerning your assignment will go in this directory.

When finished with your project, your HW directory will have at a minimum the following structure.

lila [hw1]% unzip -l ckauffm2-hw1.zip 
Archive:  ckauffm2-hw1.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2014-06-05 17:50   ckauffm2-hw1/
       24  2013-06-12 18:13   ckauffm2-hw1/ID.txt
      613  2013-05-28 14:58   ckauffm2-hw1/p1.c
      663  2013-05-28 17:45   ckauffm2-hw1/p2.c
      519  2014-06-03 11:58   ckauffm2-hw1/p3.c
     1386  2014-06-03 10:50   ckauffm2-hw1/p4.c
      367  2014-06-05 17:50   ckauffm2-hw1/p2-description.txt
     5866  2014-06-05 17:37   ckauffm2-hw1/hw1-test.sh

Additional files may be present but they will not count for grading.

2.3 ID.txt

Create a text file in your HW directory called ID.txt which has identifying information in it. My ID.txt looks like.

Chris Kauffman
ckauffm2
G007123456

It contains my full name, my mason ID, and my G# in it. The presence of a correct ID.txt helps immensely when grading lots of assignments.

2.4 Penalties

Failure to submit your homework in an appropriately named directory and with the ID.txt file in it with correct information will result in the loss of up to 10% credit.

2.5 Submission: Blackboard

Do not e-mail the professor or TAs your code.

Create a zip file of your HW directory and submit it to the course blackboard page. See the Setup Tutorial for instructions on creating zip files if you are unfamiliar.

On Blackboard

  • Click on the Assignments section
  • Click on the HW 1 link
  • Scroll down to "Attach a File"
  • Click "Browse My Computer"
  • Select you Zip file

You can resubmit to blackboard as many times as you like up to the deadline.

3 Problem 1: Debugging

Files to submit

p1.c
This is provided in the homework pack; correct it and submit a working version.

The code in p1.c code is intended to use the quadratic formula to solve quadratic equations. However, it is full of syntax errors. Use your developing debugging skills to get it in working order. Note that it uses the sqrt() function which is in the C standard math library. Remember when compiling to include the math library with -lm: do

lila [hw1]% gcc -o p1 p1.c -lm

which creates the program named p1 by compiling the source file p1.c and including the m (math) library.

An example terminal session of the correctly running program is below.

lila [ckauffm2-hw1]% gcc p1.c -lm
lila [ckauffm2-hw1]% ./a.out
a x^2 + b x + c root finder
Input a: 1.2
Input b: 8
Input c: -23.87
Roots at x = 2.234680 and x = -8.901347
lila [ckauffm2-hw1]% ./a.out
a x^2 + b x + c root finder
Input a: -4.2
Input b: 9.9
Input c: -32.32
Discriminant < 0: the roots are imaginary

4 Problem 2: Integer vs Real Division

Files to submit

p2.c
source code for problem 2, create this file
p2-description.txt
description of what happens on certain inputs, create this file

Write all your code in p2.c. This problem compares integer division and floating point division in C. It also tests your ability to read floating point data from users.

Write a C program which reads two real numbers as doubles. Use a single call to scanf to read both doubles. Call them xdoub and ydoub. Create two integers called xint and yint and assign the integers the double values. This will look like.

xint = xdoub;
yint = ydoub;

Now compute 6 quantities and assign them to variables.

  • 3 int variables which are assigned the result of
    • xint / yint
    • xdoub / ydoub
    • xint / ydoub
  • 3 double variables which are assigned the results of the same expressions above
    • xint / yint
    • xdoub / ydoub
    • xint / ydoub

Print all 6 variables on the same line using a single call to printf.

Several example runs are shown here.

lila [ckauffm2-hw1]% gcc p2.c
lila [ckauffm2-hw1]% ./a.out
xdoub and ydoub are: 5.5 2.3
2 2 2 2.000000 2.391304 2.173913

lila [ckauffm2-hw1]% ./a.out
xdoub and ydoub are: 42.1 -14.2
-3 -2 -2 -3.000000 -2.964789 -2.957746

lila [ckauffm2-hw1]% ./a.out
xdoub and ydoub are: 9.09 3.98
3 2 2 3.000000 2.283920 2.261307

Strange things seem to be happening with division. Report what happens when your program is run when input ydoub (second double) is in the range -1 < ydoub < 1. Do some research and explain why the program is behaving the way it does. The specific behavior can depend on which computing platform you are running (Windows/Mac/Linux) so mention which platform you are employing. Put your 4-5 sentence answer in the file p2-description.txt and submit it with your code.

4.1 Problem 2 Manual Inspection Criteria (10%)   grading

  • A single call to scanf() is used to read both input doubles
  • A single call to printf() is used to print all outputs
  • The results running the program with the second input in the range -1 to +1 are reported for several cases. A general pattern is described for inputs in this range.
  • There is an explanation of why the program may encounter problems executing for inputs in the range -1 to +1 is given.
  • The computing platform you are running (Windows/Mac/Linux) is described.

5 Problem 3: Division and Coins

Files to submit

p3.c
source code for problem 3, create this file

The United States monetary system currently issues coins with the following values

Type Value (cents)
Dollar coin 100
Quarter 25
Dime 10
Nickel 5
Penny 1

For example, 76 cents can be gotten using

  • 3 quarters, 1 penny (4 coins)
  • 2 quarters, 2 dimes, 1 nickel, 1 penny (6 coins)
  • 7 dimes, 1 nickel, 1 penny (9 coins)
  • 1 dime and 66 pennies (67 coins)

An interesting property of the set of coin values chosen by the Treasury Department is that one can calculate the minimum number of coins required to equal a particular cent amount relatively easily.

Minimum Coin Algorithm

  • Starting with the largest coin value.
  • Calculate how many coins can be used before exceeding the desired amount.
  • Use that many coins and subtract off that amount from the total.
  • Repeat this process for the next lower valued coin.
  • Stop when one reaches pennies.

Implement this process for the 5 coin values in the above table. Write your code in p3.c. You should

  • Prompt the user for a total in cents
  • Read that as an integer
  • Compute the minimum number of coins to total the desired amount of cents
  • Output the number of each coin type.

Hint: integer division and perhaps the modulo % operator (remainder) may be useful here. No looping is required so don't use any.

Some sample runs are below:

lila [ckauffm2-hw1]% gcc p3.c
Input # of cents: 17
0 dollar coins
0 quarters
1 dimes
1 nickels
2 pennies
lila [ckauffm2-hw1]% ./a.out
Input # of cents: 24
0 dollar coins
0 quarters
2 dimes
0 nickels
4 pennies
lila [ckauffm2-hw1]% ./a.out
Input # of cents: 31
0 dollar coins
1 quarters
0 dimes
1 nickels
1 pennies

5.1 Problem 3 Manual Inspection Criteria (10%)   grading

  • No looping or conditionals are required to solve this problem so don't use any
  • Print a sensible prompt for the number of cents like the one listed above
  • Solutions should follow the Minimum Coin Algorithm relatively closely and it should be obvious how the code implements the algorithm.

6 Problem 4: Plane Manufacturing

Files to submit

p4.c
source code for problem 4, create this

An airline company is considering manufacturing a new plane to fly a specific route. Before committing to the manufacturing though, they want to study the business viability associated with the new plane. The particular model of plane can be customized so that that the passenger area has any rectangular shape. There are some restrictions though.

  • The passenger area has any length and width (specified in feet)
  • There must be a 1.5 foot wide aisle in the plane to allow passengers to board. There only needs to be one aisle.
  • The passengers are arranged in rows across the width of the plane
  • Each passenger must be allocated a 3 foot long (length) by 2 foot wide (width) area.
  • Passengers cannot fit in a fractional space, there must be the minimum dimensions above

The costs to build the plane are as follows.

  • $10,000.00 construction overhead to build any plane
  • $1,000.00 construction cost per square foot of the passenger area

Once the plane is built each trip it takes on the specified route has the following properties

  • The price of the tickets for each passenger can be specified by the airline. Due to high demand for the route, the plane always fills up and all passengers pay the specified ticket price.
  • Each trip requires plane maintenance of $2,000.00 per trip.
  • Each trip requires fuel which costs $100.0 per square foot of the passenger area.

The company is interested in exploring how many times they will have to fly the plane before breaking even. This computation is along the lines of

\begin{equation} \text{# trips to break even} = ceiling\left( \frac{\text{Cost to build the plane}} {\text{(Gross sales of tickets per trip)}- \text{(Cost to fly plane per trip)}} \right) \end{equation}

Note the ceiling function which means "round up": if the result is a fraction, a partial trip, one whole trip must be made to reach profitability.

Write a program which calculates the number of trips to break even and displays it along with other information about planes with a given set of parameters.

Input to your program will be as follows; all are real numbers

  • Length and width of the plane in feet
  • Ticket price in dollars

Each of the following output items should be calculated and printed on its own line.

  • Plane area in square feet. Use 1 digit of accuracy beyond the decimal point
  • Passenger capacity which is the number of passengers that can ride per trip. This is an integer.
  • Cost to build the plane in dollars; print with a $ sign and show cents (2 places beyond the decimal)
  • Per Trip Gross based on the number of passengers and ticket sales (in dollars, format as above)
  • Per Trip Cost based on the maintenance overhead and fuel costs per unit area (in dollars, format as above)
  • Per trip profit (in dollars, format as above)
  • The number of trips to break even, an integer

Print two prompts, the first for the length and width of the plane and the second for the price of tickets. Make sure that you print a newline at the end of each prompt line. This will ensure that you pass all the automated tests.

Print nice messages describing the output. You must print the output numbers in the order above at the end of each line. Doing so will ensure that you pass the automated tests. See the exmaples below for more details.

If the parameters given indicate that each plane trip runs at a loss (negative profit), the number of break even trips should be negative. You are not required to take any special action in your program when this happens.

You are not required to handle any bad input including negative length/width/ticket price.

6.1 Examples

The following examples use my solution. You do not need use the exact same prompts or descriptions but you must print the output in the specified order with numbers at the end of each line.

lila [ckauffm2-hw1]% gcc p4.c -lm
lila [ckauffm2-hw1]% ./a.out 
Enter length and width of plane in feet
100 20
Enter ticket price for customers in dollars
250
Plane area: 2000.0
Passenger capacity: 297
Build Cost: $2010000.00
Per Trip Gross: $74250.00
Per Trip Cost: $202000.00
Per trip profit: $-127750.00
# of trips to break even: -15

lila [ckauffm2-hw1]% ./a.out 
Enter length and width of plane in feet
200 7.5
Enter ticket price for customers in dollars
500
Plane area: 1500.0
Passenger capacity: 198
Build Cost: $1510000.00
Per Trip Gross: $99000.00
Per Trip Cost: $152000.00
Per trip profit: $-53000.00
# of trips to break even: -28

lila [ckauffm2-hw1]% ./a.out 
Enter length and width of plane in feet
200 7.5
Enter ticket price for customers in dollars
1200
Plane area: 1500.0
Passenger capacity: 198
Build Cost: $1510000.00
Per Trip Gross: $237600.00
Per Trip Cost: $152000.00
Per trip profit: $85600.00
# of trips to break even: 18

lila [ckauffm2-hw1]% ./a.out 
Enter length and width of plane in feet
12 11.5
Enter ticket price for customers in dollars
600
Plane area: 138.0
Passenger capacity: 20
Build Cost: $148000.00
Per Trip Gross: $12000.00
Per Trip Cost: $15800.00
Per trip profit: $-3800.00
# of trips to break even: -38

lila [ckauffm2-hw1]% ./a.out 
Enter length and width of plane in feet
12 11.5
Enter ticket price for customers in dollars
800
Plane area: 138.0
Passenger capacity: 20
Build Cost: $148000.00
Per Trip Gross: $16000.00
Per Trip Cost: $15800.00
Per trip profit: $200.00
# of trips to break even: 740

lila [ckauffm2-hw1]% ./a.out 
Enter length and width of plane in feet
12 11.5
Enter ticket price for customers in dollars
1000
Plane area: 138.0
Passenger capacity: 20
Build Cost: $148000.00
Per Trip Gross: $20000.00
Per Trip Cost: $15800.00
Per trip profit: $4200.00
# of trips to break even: 36

lila [ckauffm2-hw1]% ./a.out 
Enter length and width of plane in feet
13 11.5
Enter ticket price for customers in dollars
800
Plane area: 149.5
Passenger capacity: 20
Build Cost: $159500.00
Per Trip Gross: $16000.00
Per Trip Cost: $16950.00
Per trip profit: $-950.00
# of trips to break even: -167

6.2 Hints

  • The C math library includes a floor and ceil function to that respectively round down and up. These functions return take double arguments and return doubles that are rounded and can be caste to ints.
  • When calculating the passenger capacity that can fit in the plane, consider calculating the number of rows and columns of passengers that will fit in the planes length and width. This will involve rounding down. Don't forget to account for the aisle width in the columns calculation.
  • Break the numeric computations into several steps as you are required to print many of these intermediate results (per trip gross, per trip cost, etc.) anyway.
  • Figure out how to format floating point numbers using printf to different places after the decimal.

6.3 Problem 4 Manual Inspection Criteria (10%)   grading

  • No loops or conditions are needed to complete the problem so don't use any.
  • Use several steps to complete the numeric computations required for the problem. Make sure your steps are clear by using good variable names and possibly a few explicit comments
  • Use math library functions effectively to get the rounding correct.
  • Use a set of globally defined symbolic constants to represent the fixed parameters such as the aisle width and base cost to manufacture a plane. Use either #define or a global const to set up names like PASSENGER_WIDTH which make your code more readable and allow it to change if the constants change. Do not include mysterious constants such as 1.5 in your code computations.

7 Overall Manual Inspection Criteria

7.1 Correct Setup (10%)   grading

Correctly setting up the directory structure for each project greatly eases the task of grading lots of projects. Graders get cranky when strange directory structures or missing files are around and you do not want cranky graders. The following will be checked for setup on this project.

  • The Setup instructions were followed closely
  • The HW Directory is named according to the specification
  • There is an Identity Text File present with the required information in it
  • Code can be compiled and tests run from the command line

7.2 Coding Style and Readability (10%)   grading

Reading code is made much easier if it is styled reasonably well. On this assignment, you will get credit for properly doing the following.

  • Indent and {bracketing} code uniformly throughout the program
  • Include comments above each method which describe its purpose and any limitations. Some information can be transcribed from the project specification.
  • Complex parts of code are additionally commented to describe what is happening in them. Some methods are required to have these (see below) while others may need them if your approach is unusual.

On subsequent assignments, you may be penalized for dirty-looking code so get in good habits now while there are rewards for it.

8 Testing Your Code

A script called hw1-test.sh is provided in the homework pack. This script is used to automatically check the programs on some of inputs given above.

Here is a sample run

lila [ckauffm2-hw1]% ./hw1-test.sh
Compiling p1.c

P1 Test 1
Passed

P1 Test 2
Passed

Compiling p2.c

P2 Test 1
FAILED
input: 5.5 2.3
pattern: 2 2 2 2.000000 2.391304 2.173913
output: xdoub and ydoub are: 10 2 2 2.000000 2.391304 2.173913

P2 Test 2
FAILED
input: 42.1 -14.2
pattern: -3 -2 -2 -3.000000 -2.964789 -2.957746
output: xdoub and ydoub are: -588 -2 -2 -3.000000 -2.964789 -2.957746

Compiling p3.c
p3.c: In function ‘main’:
p3.c:16:3: error: unknown type name ‘nt’
Failed to compile p3.c

MISSING: p4.c

------------------
Failures: 3
Tests Passed: 2 / 12 = 8.33 / 50

Each test, compile fail, or missing code increases the number of failures.

  • All is well for Problem 1
  • Problem 2 failed on both of the tests likely meaning there is a problem in the code
  • Problem 3 did not compile correctly
  • Problem 4 does not have code

Success on all fronts looks like this:

lila [ckauffm2-hw1]% hw1-test.sh
Compiling p1.c

P1 Test 1
Passed

P1 Test 2
Passed

Compiling p2.c

P2 Test 1
Passed

P2 Test 2
Passed

Compiling p3.c

P3 Test 1
Passed

P3 Test 2
Passed

P3 Test 3
Passed

P3 Test 4
Passed

Compiling p4.c

P4 Test 1
Passed

P4 Test 2
Passed

P4 Test 3
Passed

P4 Test 4
Passed


------------------
Failures: 0
All tests passed
Tests Passed: 12 / 12 = 50.00 / 50

This script is there to provide you some clues but may not test all the functionality that is required for full credit. If gaps are identified in your program during manual inspection, you may lose credit. As was once eloquently put by a godfather of computer science…

Testing can only prove the presence of bugs, not their absence.

– Edsger Dijkstra


Author: Chris Kauffman (kauffman@cs.gmu.edu)
Date: 2015-06-03 Wed 13:57