Assignment #3

Due: Tues, June 13

Objective

This assignment will involve practice with control structures, including both selection and repetition statements (loops).

Task

Write the following programs, each in a separate file. Filenames should be: (Note that the filenames are all lowercase)

Exercise 1

Filename: gas.c

Write a program that computes miles per gallon, and repeats as many times as the user wishes. It should do the following:

  1. Ask the user to enter the gallons used (use a variable of type double).
  2. Ask the user to enter the miles driven (also use type double).
  3. Compute the miles per gallon, and print it, to 4 decimal places.

Sample Runs

(user input is underlined, to distinguish it from output)

Sample run 1

  Enter the gallons used: 13.5
  Enter the miles driven: 200

  The miles/gallon for this tank was 14.8148

Sample run 2

  Enter the gallons used: -4

  Must enter a positive value.  Try again. 
  Enter the gallons used: 0

  Must enter a positive value.  Try again.
  Enter the gallons used: 12.3
  Enter the miles driven: -3

  Must enter a positive value.  Try again.
  Enter the miles driven: -5

  Must enter a positive value.  Try again.
  Enter the miles driven: 0

  Must enter a positive value.  Try again.
  Enter the miles driven: 250

  The miles/gallon for this tank was 20.3252

Exercise 2

Filename: sum.c

Prompt the user and let them enter two integers (we'll call them x and y, for the purposes of this writeup). Compute and print the sum of the ingeters in the range x through y (including the endpoints). Output should look like the sample runs below, and print the addition details from the smallest to the largest number.

(Note: The user input will not necessarily have the lowest number first -- see Sample Run 2. You must take this into account).

Sample Runs

(user input is underlined, to distinguish it from output)

Sample Run 1

  Input two integers: 10 20

  Sum of values from 10 through 20 is:
  10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20
  = 165

Sample run 2

  Input two integers: 9 -4

  Sum of values from -4 through 9 is:
  -4 + -3 + -2 + -1 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
  = 35

Sample run 3

  Input two integers: 7 7

  Sum of values from 7 through 7 is:
  7
  = 7

Exercise 3

Filename: numbers.c

Write a program that will allow the user to enter a set of integers (as many as they want), then prints out a summary of information, as follows:

Sample Runs

(user input is underlined, to distinguish it from output)

Sample Run 1

  Input integer (0 to stop): 12
  Input integer (0 to stop): 4
  Input integer (0 to stop): -1
  Input integer (0 to stop): -5
  Input integer (0 to stop): 18
  Input integer (0 to stop): 0

  # of positives = 3
  # of negatives = 2
  Sum = 28
  Average = 5.60

Sample Run 2

  Input integer (0 to stop): 4
  Input integer (0 to stop): 8
  Input integer (0 to stop): 24
  Input integer (0 to stop): 94
  Input integer (0 to stop): -1
  Input integer (0 to stop): 43
  Input integer (0 to stop): 13
  Input integer (0 to stop): 0

  # of positives = 6
  # of negatives = 1
  Sum = 185
  Average = 26.43

Requirements for all programs


Submitting:

Submit only your source code files through the web submission page. This will consist of the following files:

  gas.c
  sum.c
  numbers.c