4CCE1PHC
1 Introduction
Program Control and Flow
October 4, 2021
In this lab you will build a function plotter in stages. It will (i) reinforce what you learnt in the first lab, (ii) introduce you to writing your own functions with local variables, (iii) familiarise you with using conditions and loops to control the flow of your program, and (iv) introduce you to calculations using the math.h library.
At the end of the lab, you should be able to:
• Write a C program from scratch.
• Write your own functions.
• Use conditions and loops in the design of your programs. • Perform calculations with formatted output.
Before starting work you should read through this document and complete the preparatory tasks detailed in §2.
2 Preparation
Materials
Make sure you have everything you need to complete this lab.
Ensure that you have the following software installed on your computer: (i) a terminal program (e.g., xterm or similar on Linux/Mac OS, cygwin on Windows), (ii) a text editor (e.g., gedit or similar on Linux/Mac OS, notepad++ on Windows), (iii) the GNU compiler collection (i.e., gcc).
Self-study
Read §3 below, read up on any new material (in textbooks and/or online) that you need to, to accomplish the tasks. Remember to make notes of where you got the information, so that you can quickly go there again if something does not work as expected. For this lab, you will not receive skeleton code, so you should be prepared to open an editor and write a C-program from scratch, based on what you learnt in the first lab.
As a minumum, you should have notes about the following in before the start of the lab:
• How to declare and intialise int and float variables.
Example declaration of an integer variable: int a = 42;
Example declaration of a floating point variable: float x = 3.14;
• How to print these data types with a fixed character width, including what format specifier is needed. Example of printing an integer variable with a fixed width of two characters: printf(“%2dn”,a); Example of printing a floating point variable to two decimal places: printf(“%.2fn”,x);
Dr Matthew Howard 1 Deparment of Engineering © 2020 King’s College London
4CCE1PHC
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20 21
/* Demonstrates use of while loop. */
#include
int main() { int x=0,
n=0,
This variable is used to keep track of
the number of times the loop is repeated. */ N=360; /* Set the number of iterations at which
the loop should terminate. */ while(n
int main() { int x=0,
n,
this happens in the for loop). This variable is used to keep track of the number of times the loop is repeated. */
N=360; /* Set the number of iterations at which the loop should terminate. */
for(n=0;n
#include
#define PI
int main() int x=0,
n,
N=360;
so we can use the sin ()
function. */
3.141593 /* Define PI as a constant
(could use M_PI from math.h instead ). */
/* Declare x and initialise it to zero. */ /* Declare n. (No need to initialise, as
this happens in the for loop). This variable is used to keep track of the number of times the loop is repeated. */
/* Set the number of iterations at which the loop should terminate. */
float y; for(n=0;n
#include
#define PI
int main() int x=0,
n,
N=360;
so we can use the sin ()
function. */
3.141593 /* Define PI as a constant
(could use M_PI from math.h instead ). */
/* Declare x and initialise it to zero. */ /* Declare n. (No need to initialise, as
this happens in the for loop). This variable is used to keep track of the number of times the loop is repeated. */
/* Set the number of iterations at which the loop should terminate. */
float y; for(n=0;n
#include
so we can use the sin ()
function. */
#define PI 3.141593 /* Define PI as a constant
(could use M_PI from math.h instead ). */
1.0f /* Define FREQ as a constant. This means the code must be
recompiled for changes to
this value to take effect. */
#define FREQ
int main() { int x=0,
n,
this happens in the for loop). This variable is used to keep track of the number of times the loop is repeated. */
N=360; /* Set the number of iterations at which the loop should terminate. */
float y; /* Declare y. */ for(n=0;n
#include
so we can use the sin ()
function. */
#define PI 3.141593 /* Define PI as a constant
#define FREQ
(could use M_PI from math.h instead ). */
1.0f /* Define FREQ as a constant. This means the code must be
recompiled for changes to
this value to take effect. */
#define WIDTH 60 /* Define a constant width for plotting .*/
void plot(int x, float y){ int n;
printf(“%3d␣” ,x);
if(y==0) /* If y==0, then plot *
and a bunch of spaces after. */
{
printf(“*”); for(n=0;n
“———————————-|␣y\n” ); for(n=0;n
int main() { printf(“\x1B[31m␣red\n\x1B[0m”); printf(“\x1B[32m␣green\n\x1B[0m”); printf(“\x1B[33m␣yellow\n\x1B[0m”); printf(“\x1B[34m␣blue\n\x1B[0m”); printf(“\x1B[35m␣magenta\n\x1B[0m”); printf(“\x1B[36m␣cyan\n\x1B[0m”); printf(“\x1B[37m␣white\n\x1B[0m”);
4
4.1
3.
4.2
Hint: You can use the terminal colour codes to print in different colours. See the example code in Listing 7.
return 0; }
4.
5.
Listing 7: Sample code to print in colour.
argument, if it is between 0 and 1. Ensure that the function ends the line before it returns control to main().
Think: Could you implement the same function with a for, while or do, while loop? Which would be more efficient?It depends on how you implement it, but a do, while loop would avoid you having to have a separate case for the first iteration.
Modify your function plot() so that it prints the value of x on the left hand side of the plot, and the value of y on the right hand side. Add a line to main() to print a simple scale axis for y (e.g., 0.0 |—————-| 1.0).
Verify that the program performs as expected, including when you change the frequency of the sine function using FREQ.
Think: Debug your program and make sure it is easy to read, before storing it for later use.
Optional Additional Work
Improving Formatting
Modify your program to improve the formatting. For instance, you could
1. Try plotting some more interesting functions, e.g., summations of sine functions with different phase and frequency. You can add noise with the rand() function from the math.h library.
2. Modify your program so that it shows a tick-mark following the value of x at regular intervals (e.g., at x=0,10,20,…). Define a constant that can be used to change the spacing of the tick-marks. Hint: You can use an if…else condition together with the modulo-operator % for this.
Modify your program so that it plots the sine curve in colour. Using this, try plotting two functions together on the same plot in different colours (e.g., sin(x) in red and cos(x) in blue).
Homemade Sine Function
Implement and test a function to evaluate the sine of a number x. Hint: You can use a for loop and the series expansion of sine:
x3 x5 x7 sin(x)=x− 3! + 5! − 7! +···
Dr Matthew Howard 8 © 2020
(1)
Deparment of Engineering King’s College London
4CCE1PHC
Print your answer together with the one from the sin() function in math.h.
Think: What happens when a large number of iterations are used? What happens when you call your function with a large values of x?
Dr Matthew Howard 9 Deparment of Engineering © 2020 King’s College London