UMBC CS 201, Spring 02
 UMBC CMSC 201 Spring '02 CSEE | 201 | 201 S'02 | lectures | news | help

 CMSC 201 Programming Project Five Approximating PI Using a Series Out: Sunday 4/28/02 Due: Before Midnight, Tuesday 5/14/02 The design document for this project, design5.txt is due: Before Midnight, Sunday 5/5/02

## The Objective

The objective of this assignment is to give you experience working with linked lists. You'll be using command-line arguments, dynamic memory allocation, passing by reference, and be working with the fraction ADT. You will also continue to practice using good program design and implementation techniques.

## The Background

A series is a sum of terms that are related by some general formula. We can approximate pi by using the following formula :

pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 . . .

so

pi = 4(1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 . . .)

This is an infinite series. The more terms we use, the closer we get to the real value of pi.

Design and code a project that uses a command-line argument to get the number of terms the user would like to sum to approximate pi. Each term of the series is to be stored as a FRACTION (see Lecture 22) within a NODE of a linked list (See Lecture 23). So for each term you will create a node, store the appropriate fraction in it and insert it into your linked list.

After building the linked list, you should traverse the list and print out all of the fractions that are in it. Since you are interested in the sum of the terms, as you insert each node, you'll need to add that fraction to the sum. It is possible to keep this sum as either a FRACTION or a double. You should calculate the sum in both forms (both FRACTION and double) if possible. There is a problem with storing the sum as a FRACTION when the user chooses to have more than 12 terms in his/her partial series. See the sample output below to determine what that problem is. The file header comment of your proj5.c file MUST explain the problem. If the user wants to use a partial series of more than 12 terms, calculate the sum only as a double.

Your project should print out the fractions that are being stored in your linked list as they would be printed by the function PrintFraction(). Although this is sufficient to tell what is in the list, we would like to see the series printed out in a more appopriate form. You must have a different function that will print out the terms of the series in a nicer form as shown in the sample run. You must also print out the sum either as just a double, or in both forms when possible, and the approximation of pi.

## Sample Run

Here is a chart of some approximations

# of terms Approx of pi  # of terms Approx of pi
10 3.041840   11 3.232316
100 3.131593   101 3.151493
1000 3.140593   1001 3.142592
10000 3.141493   10001 3.141693
25000 3.141553   25001 3.141633
50000 3.141573   50001 3.141613

## Further Specifications

• You must use separate compilation for this project. It would be appropriate to have many .c and .h files for this project. I would suggest proj5.c, list.c & .h, fraction.c & .h, and proj5aux.c & .h
• You must use a linked-list for this project.
You may NOT use an array implementation. If you choose to do so your project will earn a 0.
• You may use the linked list code given in lecture 23. Some of the functions will need minor modification, but some will work as they are (if you choose your data structures carefully when designing).
• You may use the fraction code given in lecture 22.
• For terms of 12 or less, you must calculate the sum as both a fraction and as a double and display each. For terms of more than 12, do NOT calculate the sum as a fraction. Only calculate the sum as a double. You must have an explanation of why this is necessary in your file header comment of the proj5.c file.
• You MUST use dynamic memory allocation (malloc) to allocate the memory for the nodes of the linked lists. Of course, you must check to see that the space was actually given to you.
• You must destroy the list after you are finished using it, so you must write your own DestroyList() function and add it to the linked list code. This function should free all of the nodes of a linked list that is passed to it.
• If the program is run with the wrong number of command line arguments, an appropriate message must be printed to the screen informing the user how to correctly run the program and then the program must exit.

## Submitting the Program

Here is a sample submission command. Since you may have different file names you'll have to adjust it to fit your needs. As always, be sure to submit all of the files necessary for your project to compile.
Note that the project name starts with uppercase 'P' as always.

submit cs201 Proj5 proj5.c list.c list.h fraction.c fraction.h proj5aux.c proj5aux.h

To verify that your project was submitted, you can execute the following command at the Unix prompt. It will show the file that you submitted in a format similar to the Unix 'ls' command.

submitls cs201 Proj5

CSEE | 201 | 201 S'02 | lectures | news | help

Sunday, 28-Apr-2002 14:15:13 EDT