2024 Fall A1
2024 Fall A1
Assignment 1
Due Date: September 23, 2024, 6:00 pm – late submissions will not be accepted
Total points: 100
Instructions
All source files (Makefile, .c, .sh, ...) should be bundled in a single tarball (named
‘a1.tar’)
Verify that your tar file is not corrupted (can happen sometimes when updating the contents
of an existing tarball in an incorrect manner) by extracting it (using ‘tar -xvf a1.tar’) in a
different directory and checking its contents before submitting. Do not submit binaries;
i.e. no ‘.o’ or executable object files of any kind. You should always run ‘make clean’
before running ‘tar’ to clean all binaries.
Your code must include comments describing what it does, and how. Code with syntax
errors will receive no credit.
You should always develop your solutions incrementally on Git and show your
progress as you are developing your solutions; i.e. you should commit every single
complete change you make as you work on your solution. You must submit a git.log which
is the output of git log showing your incremental progress.
To submit your work, you should upload your tar file to Canvas.
1
Question 1 [60 points]
In this question, you are to write a C program that computes an approximation of π using
the Leibniz formula:
1 1 1 1 1
π = 4( − + − + − ...)
1 3 5 7 9
The above formula is what is known as an “infinite sum” formula. The idea is that we can
obtain the exact value, if add up an infinite number of terms. A realistic implementation
would only use a finite number of terms. The accuracy of the computed value depends on
the number of terms being added.
Your program should (eventually) start by reading an integer n from stdin using scanf().
You can then use the appropriate looping structure to compute the sum of the first n terms
of the above formula. To maximize accuracy, we will use a variable of type double to store
the sum. Once the sum is computed you must then display the approximate value of π using
printf() and the appropriate format characters.
To do this we will break this down into multiple stages so we can practice good design, and
git commit habits. It is important to submit each stage, even if you can develop the entire
solution already, as the assignment will assess your incremental work.
Create a skeleton
Create a file called pi.c this should contain a main function which reads the number of terms
to compute (it does not need to do anything with this yet). It should also compute the first
two terms (it does not need to use a looping structure, but should use variables to do the
computation). Lastly, have the program use printf() to output its (poor) approximation
of π.
As per “standard programming practice” discussed in class, you must also write a Makefile
to build your pi.c program into an executable object file named pi.
Once your pi.c can be built without errors by your Makefile: Create a copy of your pi.c
called pi-skeleton.c then git add all files created so far. Lastly, git commit your work.1
A git push wouldn’t be a bad idea either.
1
at this stage you my not know how to approach the next section, but you should be able to start on
some parts of Question 2, which is a good idea
2
Improve your skeleton
Continue to develop your pi towards the specification above by adding an appropriate loop.
You should also ensure your types are appropriate, and that your printf() output is correct.
As you complete improvements you should make incremental git commits which document
what you changed and why.
You have finished this stage when your pi program works as specified.
2
‘hardcoding’ is where something that should be a parameter is fixed in the program code
3
Files to hand in
All source files must be on git. The following files are to be packaged into a tarball a1.tar
and submitted to canvas:
- Makefile
- git.log
- pi-skeleton.c
- pi.c
- pi-test.sh
You can generate the git.log file with the following command:
Your tarball should be flat (contain no subdirectories), and should not contain any files not
listed above. Remember the command to generate a tarball will be like the following:
$- make
$ test -s git.log && echo “PASS” || echo “FAIL”
$ test -s pi.c && echo “PASS” || echo “FAIL”
$ test -s pi-test.sh && echo “PASS” || echo “FAIL”
All files, except those explicitally exempt, or used as command input, must include Name,
NSID, Student Number, and Course & section number at the top (i.e. before any other
content). These must have the following formats (Remove angle brackets and replace with
3
Where a command does not explicitly print a PASS/FAIL, the return value should be zero and there
should be no errors printed to the terminal.
4
your information). Note: due to how this document is formatted, you should not copy and
paste these blocks as the resulting text may lose formatting.
$• Bash
# !/ bin / bash
# Name : <Last , First >
# NSID : < abc123 >
# Student Number : <##### >
# Course : CMPT214 - <## > 2024
• C
/*
Name : <Last , First >
NSID : <abc123 >
Student Number : <##### >
Course : CMPT214 - <## > 2024
*/
• Makefile
# Name : <Last , First >
# NSID : < abc123 >
# Student Number : <##### >
# Course : CMPT214 - <## > 2024