Projects for CMPSC 201C , Fall 2012

Project 9 : Matrix manipulation, Due Monday, Dec. 17, 11:59 pm

Write a program which reads the coefficients of a matrix consisting of N rows and N+1 columns from a file on disk. This file has to have the following format :

1. row : just the number N itself
2. row : the N+1 coefficients of the first row of the matrix
3. row : the N+1 coefficients of the 2nd row of the matrix
(N+1). row : the N+1 coefficients of the N'th row of the matrix.

The coefficients of the matrix are of data type "double".

The name of the input file is arbitrary but is at most 15 characters long.

Your program has to work for any number N between 2 and 20 and stop with an error message if the file contains a number outside this range.

After reading in all the data using a 2-D array your program is to do the following :

Multiplying each coefficient of the 2nd row with the same value and adding the results to the corresponding coefficient of the first row and making the resulting coefficients become the coefficients of the new second row replacing the old. Choose the multiplier such that the 1. coefficient in the new 2nd row becomes zero !! Repeat this for the 3rd etc. up to and including the N'th row.

Once this accomplished we will expand this to all the elements below the major diagonal and then maybe even attempt the backward substitution step. See you Friday, Dec 14, in class.

The final answer, which your program prints to the screen, must be a matrix consisting of N rows and N+1 columns with all elements below the major diagonal zero.

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

New C++ skills : multidimensional arrays

Project 8 : 1-D array , Due Fri. Dec. 7, 2012 (11:59 pm, no grace period)

Write a program which reads an arbitrary (≤21) number of values ( data type double) from a file on disk and stores these values in an 1-dimensional array. Subsequently the program will order these values (most positive first) and write the ordered values to a second file on disk.
Your program must ask the user for the names for the input and output files. The length of the file names is restricted to 19 characters (no blanks).
Also, have your program print out the minimum, maximum, and average of the numbers of the input file.

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

New C++ skills : 1-D arrays

Project 7 : Numerical Integration, Due Mon. Nov. 12

Write a program which numerically integrates a definite integral using Simpson's rule. The mathematical background for numerical integration is described in Chapter 10.4 of the text containing a paragraph on Simpson's rule and at Mathematics for trapezoidal and Simpson's rule.

  1. Your program will request from the user the value of the upper and lower integration limits.

  2. Your program will also request from the user the number of integration intervals, n.
    Note, a requirement for the Simpson rule to work is that the number of intervals, n, is even. Your program must verify the user's input and reject it if the user's input is an odd number (or alternatively increases the user's input by 1 and informing the user of that).

  3. After the user has provided his/her input your program determines an approximation to the value of the definite integral for the number of integration intervals prescribed.

  4. In order to adapt your program quickly to find solutions for a new integrand embed the equation for the integrand in a function called "integrand".

  5. Use your program first on a function ( 5.1·sin(2.3 x) ) for which you know the analytical solution for given integration limits ( 0.2 and 1.4 ). Let your program "solve" this case at three different numbers of intervals ( n = 10, 20, 40 ) and judge the results critically. Theory predicts that the magnitude of the difference between the analytical solution and the values obtained by your program should be approximately proportional to 1/n4; for Simpson's rule. You will see here that the differences are so small that the usual output with 6 significant digits is not accurate enough. Force your program to print out more digits, for more details on that click on How to Format a programs Output.

  6. Document your results carefully in the preamble of your soure code.

  7. Now use your program to solve the integral         and document your results carefully in the preamble of your soure code.

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

Project 6 : Root Finding , Due Mon. Oct. 29, 2012, 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 the program must ask again for these if f(x) is not greater than zero at one boundary and less than zero at the other boundary.
  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 : sine-function , Due Mon. Oct. 8, 2012, 11:55pm

Create a program which calculates the value of sin(x) for any value of x between 0 and 2*π (inclusive). Your program must terminate in cases of illegal input value : <0 , > 2*π , and accidental illegal character and tell the user why exactly it terminated. Use a while loop to allow for repeated user input.

sin(x) = x/1! - x3/3! + x5/5! - ....

The result must be accurate to within at least 6 decimal digits behind the decimal point.

In addition to printing out the result your program must also print out the number of terms included in your calculations.

Important : To insure efficiency of your program I will inspect your code in detail to verify that your program needs to perform a single multiplication plus a single division plus a single subtraction for each term in above series.

Note : see Value of π

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

Project 4 : Factorial , Due Mon. Oct. 1, 2012, 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 must 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.

New C++ skills : For- and while-loop
Math skills : Algebra

Project 3 : Speed of Car, Due Fri. Sept 21, 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 its user :

  1. The distance travelled.

  2. The time needed to travel above distance.

  3. The miles per gallon the user's 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 one (1) gallon of gasoline produces 19.4 pounds of CO2 ,

  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 Fri. Sep. 12, 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 Fri Sept. 7, 11:59pm
Modify (get to work) the program hello.cpp we started 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.

Send a Note to Zig

Zig Herzog © 2014
Last revised: 08/23/13