Projects for CMPSC 201C , Fall 2009


Project 9 : Writing and reading text files, Due Fri. Nov. 13, 2009

Create program which reads from a file on your disk called "input.dat" a sequence of up to 20 double precision numbers ( positive , zero , or negative ) into an array. Your program must deal with the possibility that your data file may contain more or less than 20 numbers.
Subsequently, your program must write these numbers into a file called "sorted.dat".
You may use the Unix program "diff" to check whether both files have the same content :
diff input.dat output.dat

Also, please insert comments near the top of your .cpp files describing the essential parts needed to have your program read/write a disk file and what rules you have to obey by.

The name of the file containing your source code must be files.cpp

New C++ skills : Reading/writing to disk


Project 8 : 1-D array , Due Wed. Nov. 4, 2009

Write a small program which initializes an 1-dimensional array with 10 integer numbers, prints out these numbers (one number per line ), calculates and prints out the sum and the average of these numbers.

The name of the file containing your source code must be array1D.cpp

New C++ skills : 1-D arrays


Project 7 : FluidFriction , Due Wed. Oct. 28, 2009

Create a program which utilizes the Newton_Raphson method to find roots of prescibed mathematical functions. The program must determine the value of those mathematical functions and their derivatives for arbitrary value of the independent variable inside a program function newton(......) with the value of these two quantites passed_by_reference back to the calling function "main" which then prints out the result.

The problem to be solved occurs in fluid mechanics. The value of a quantity called Reynolds number (Re in the equation below) is specified by the user of your program ( range from 1000 to 107 , your program must verify that the user does not specify a value outside this range ). Re is directly propertional to the mean velocity of a fluid streaming through a circular pipe. To be determined by your program is the coefficent of resistance λ which is a measure of the pressure drop per unit length of the pipe. λ and Re are related to each other by :

1/√λ = 2 log10 ( Re √λ ) - 0.8

which, unfortunately, cannot be solved explicitely for λ and hence an iterative method has to be employed.
Your program must utilize the Newton_Raphson method ( text chapter 10.1 ) utilizing a user-defined function with output parameters ( text chapter 5.3 ) to provide f(x) and f'(x) as defined in the text, chapter 10.1.

The Newton-Raphson method has the advantage of converging very fast needing only a single initial estimate.
BUT : there is no guarantee that it converges for every initial estimate !!!!!! Here choose
&lambda < 0.1
as an initial estimate.

The name of the file containing your source code must be newton.cpp

New C++ skills : functions returning values via argments
Math skills : Newton-Raphson method to find roots of functions


Project 6 : Root Finding , Due Mon. Oct. 12, 2009, 11:55pm

Create a program which finds the roots of the funtion :

f(x) = a*sin( b* x ) + c*cos( d*x )

using the bisection method.
  1. The values for the constant "a","b","c", and "d" are to be provided by the user on the keyboard and are of type double.
  2. The user must also specify the two boundaries of the starting interval and reject it ( ask the user again ) if f(x) has the same sign ( + or - ) at both boundaries.
  3. The user must also be able to specify a termination criteria. For this project the iteration should terminate when the size of the interval containing the root falls below the value specified by the user.
  4. The program must contain a user-defined function which returns the value of f(x) with arguments for a,b,c,d, and x.
  5. The name of the file containing your source code must be bisection.cpp

New C++ skills : Using library and user-defined functions
Math skills : Rootfinding by bisection and regular falsi method.


Project 5 : Angle , Due Fri. Oct. 2, 2008, 11:55pm

Write a program which calculates the angle between two vectors utilizing the fact that the dot-product between two vectors can be determined in two different ways if the the x, y, and z-component are given in cartesian coordinates.

dotproduct = x1*x2 + y1*y2 + z1*z2

or

dotproduct = length of 1.vector times length of 2nd vector times cosine of the angle between them.

Mandatory features

  1. The values of the 6 vector components are user input ( cin ) .
  2. Once done with the calculation of an angle the user should be asked to provide another pair of vectors.
  3. If the length of the first vector is zero the program must terminate.
  4. If the length of the second vector is zero ( but not of the first ) the program should report a message ( like "Cannot find angle if second vector is equal to zero" ) and requests again the 3 components of the second vector.
  5. The name of the file containing the source code shall be angle.cpp

    The name of the file containing your source code must be angle.cpp


Project 4 : Factorial , Due Wed. Sep. 23, 2009, 11:55pm

Create a program which for an integer number n, to be provided by the user, determines the value of n!( n factorial ). Design the program such that after calculating n! and informing the user of the result, it will ask the user for another value for n until the user responds by entering a value of 0 (zero). At that time the program will terminate with a nice message to the user. Of course, you have to inform the user of that possibility of entering zero. In addition to this you have to safe-guard your program by rejecting negative values for n. ALSO, use a first version of this program to explore at what value of n an overflow condition occurs and amend your program to prevent that from happening (and of course telling the user about it).
MANDATORY : You must have a while- and a for-loop in your program.

The name of the file containing your source code must be factorial.cpp


Project 3 : Speed of Car, Due Wed. Sept 16, 11:59pm

Write a program which computes the average speed of a car and the carbon foot print for the indicated travel. Floating point numbers (data type "double") are required.

The program must solicit from the user :

  1. The distance travelled.

  2. The time needed to travel above distance.

  3. The miles per gallon your car gets at 55 mph.

Your program must provide the following feedback based on the values provided by the user :

  1. Reflect the input the user provided

  2. The average speed

  3. Print a warning if the speed was above 55 mph or a compliment (for saving gas) if the average speed was below 55 mph

  4. Determine the amount of gas ( gallons ) the car used for the trip. Here your program must take into account that the mileage a car gets depends on its speed. Use the formula :

    mpg = mpg55 * ( 55 / speed )2

    Here "speed" = actual velocity of car
    "mpg55" = miles per gallon at 55 mph
    "mpg" = miles per gallon at "speed"



  5. Calculate the numbers of pounds of CO2 the car put into the air during this travel (According to the EPA standard 1 gallon of gasoline produces 19.4 pounds of CO2 ,
    http://www.epa.gov/OMS/climate/420f05001.htm#calculating.

  6. Print a nice good bye message

The name of the file containing your source code must be car.cpp

New C++ skills : Getting input from keyboard, outputting to monitor, mathematical operators for floating point numbers.
Math skills : Algebra


Project 2 : Program intParen.cpp, Due Wed. Sep. 9, 11:59pm
Write a program ( file name intParen.cpp ) which solicits from the user four (4) integer numbers ( I use the variable names i, j, k, and m to store the values the user provides ) and performs the following actions :
  1. Verifies that the use of parenthesis in C++ is identical to its use in mathematics by performing the following operations which all should give the same answer.
    • (i+j)*(k+m)
    • i*k + i*m + j*k + j*m
    • i*(k+m) + j*(k+m)
    • (i+j)*k + (i+j)*m
    Have your program print out the results for each of the four cases and inform the user ( using cout ) that the four results should be ( should not be ) identical.
  2. Have your program investigate whether the two statements
    • i = i + 3 ;
    • i += 3 ;
    change "i" by the same amount. For each of these two statements have your program print out the value of "i" before and after the operation. Have the program inform (cout) the user whether the two results should have identical outcome or not.
  3. Finally, add the following assignments in your program :
    • i = 7 ;
    • j = -11;
    • k = 3 ;
    • m = -5;
    and have your program calculate and print out the result of the following operations :
    • i/k
    • i/m
    • j/k
    • j/m
    • Observe the results your program produces and add a "cout" statement to your program which prints out your conclusions.


Project 1 : Program hello.cpp, Due Mon. Aug. 31, 11:59pm
Modify the program hello.cpp we wrote in class :


The following points pertain to each and every project and should be read carefully :

  1. Each file containing source code of a project must have the following informative block at the top of the file with the dots replaced by the proper information : :
    // Created by : .....
    // Access account ID : .....
    // Date of Creation : .....
    // Purpose of Program : ....
    Usually, the Purpose section will contain more than 1 line.



  2. Each of your user-defined functions must contain a blurb at the top of its definition outlining the purpose of the function and an explanation of the formal arguments.

  3. Each variable you declare either in the main program or in a function must be commented on concerning its purpose and possible restrictions. This is usually done at the point where a variable is declared.

  4. Clearly document in your program where the user input section, the user data validation section and the computational section start and end.

  5. Be aware of that I will re-compile your program and run test cases with it on my computer including checking the numerical results it produces.


Zig Herzog; hgn@psu.edu
Last revised: 10/06/09