CS代考 4CCE1PHC

4CCE1PHC
1 Introduction
Arrays & Pointers
October 11, 2021
This lab introduces you to arrays and pointers as important consructs to handle data in C. It will also give you some experience in writing larger programs in C, using functions to structure the program into modular units.
At the end of the lab, you should be able to:
• Declare, define and call functions which pass arguments both by value and reference. • Use pointers and arrays to manage and manipulate data.
• Use functions to organise complex code.
• Write programs consisting of multiple files and use header files to enable code reuse.
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.
Answer the following questions:
1. Write down how you would initialise a one-dimensional array with the first ten elements of the Fibonacci sequence.
An example way to do this is int f[] = {1,1,2,3,5,8,13,21,34,55}; (If you don’t know what the Fibonacci sequence is, look it up!)
2. Write down how you would access the first, last and 200th element of a one-dimensional array a containing 400 elements.
Code to access (i) first element: a[0] (remember indices start from zero in C), (ii) last element: a[399] (as indices start from zero, the 400th element is at position 399), and (iii) 200th element: a[199].
Dr Matthew Howard 1 Deparment of Engineering © 2020 King’s College London

4CCE1PHC
1 void change_first(int x[]) 2{
3 x[0] = 42;
4 return;
5}
Listing 1: Sample function to alter the contents of an integer array using pass by reference.
3.
Write down the equations needed to compute the mean and the standard deviation of a set of numbers.
The equation for the mean of a set of N numbers x1, x2, …, xN is
N
μ = 􏰃 xn.
n=1
The equation for the standard deviation is
􏰆􏰅􏰅 1 􏰃N
σ = 􏰄N − 1 (xn − μ)2 n=1
where μ is the mean.
Write down a sample function in which an integer array is passed as an argument to a function by reference so that the function is able to update the contents of the array and return these changes to the calling function.See sample code in Listing 1. The integer array is x and should be declared in the calling function. After the function is called, the first element of x will be assigned the value 42. This value will be retained in x even when the function change_first() exits.
Laboratory Work Working with Arrays
Create a new file array_1d.c and open it with a text editor. Write a C-program containing (i) a function array_printf() that takes an integer array as an argument and prints out the value stored in each element to stdout and (ii) a main() function declares a local integer array named a, initialises it with the numbers 21, 6, 28, 36, 15, 1, 3, 10, 45, 55 and calls array_printf() with a as an argument. Use the const keyword in your defintion of array_printf() to declare that the function will not modify the contents of the array. Test the program and verify that it works as expected. A sample solution is given in Listing 2.
Add (i) a function array_max() that takes an array as an argument and returns the maxiumum of the values in the array and (ii) a function array_min_index() that takes an array as an argument and returns the index of the minimum value in the array. Modify main() so that it calls array_max() and array_min_index() and prints the results to stdout. A sample implementation of array_max() is given in Listing 3. This should be called in main() with a line:
int a_max = array_max(a,10);
A sample implementation of array_min_index() is given in Listing 4. This should be called in main() with a line:
int index = array_min_index(a,10);
4.
3 3.1
1.
2.
Dr Matthew Howard 2 Deparment of Engineering © 2020 King’s College London

4CCE1PHC
1 #include /* Include stdio.h library, so we
2 can use the function printf (). */
3 #define SUCCESS 0
4
5 /* Function takes the array, and its size as arguments. */
6 void array_printf(const int x[], int size)
7{
8 int 9
10 for 11
12 {
13
i; /* (i=0; i< size; i++) /* Declare an integer to be used in the for loop. */ Iterate through each element of the array. */ printf("%d␣",x[i]); /* Print the ith element followed by a space. */ Print a new line when finished. */ No return value needed.*/ Declare and initialise integer array. The size of the array is determined by the number of elements given to initialise it. */ Call function to print array contents. */ 14 15 } 16 printf("\n"); /* 17 18 19 return ; /* 20 } 21 22 int main(void) 23 { 24 int a[] = {21, 6, 28, 36, 15, 25 1, 3, 10, 45, 55}; /* 26 27 28 29 30 printf("a␣=␣"); 31 array_printf(a,10); /* 32 33 34 return SUCCESS; 35 } Listing 2: Sample implementation of array_printf(). Dr Matthew Howard 3 © 2020 Deparment of Engineering King’s College London 4CCE1PHC 1 /* Function takes the array, and its size as arguments. 2 * Its return value is the maximum element found. */ 3 int array_max(const int x[], int size) 4{ 5 int i; 6 7 int result=x[0]; 8 9 10 11 12 13 14 15 16 for (i=0; i< size; i++) 17 18 { 19 if (x[i]>result)
20 21 22
23 {
24 result = x[i];
25
26 }
27 }
28 return result;
29
30 }
/* Declare used in /* Declare
an integer to be the for loop. */ the variable
result that will contain the result of the search for the biggest element of x. Initialise it to the first element of x (if we’re lucky it be the element we’re
looking /* Iterate element
for !) */
through each
of the array. */
/* If the ith element is bigger than the current value stored in
result … */
/* … update result with this value. */
/* Return the result of the search. */
Dr Matthew Howard © 2020
4 Deparment of Engineering King’s College London
Listing 3: Sample implementation of array_max().

4CCE1PHC
1 /* Function takes the array, and its size as arguments.
2 * Its return value is the index of the miniumum element
3 * found. */
4 int array_min_index(const int x[], int 5{
6 int i;
7
8 int
9
result =0;
the the
for loop. */ variable will
result
10
11
12
13
14
15
16
17 for (i=0; i< size; i++) /* 18 19 { 20 if (x[i]
14 #include
15 16
17 /* Function prototype for array_printf() */
18 void array_printf(const int x[], int size);
19
20 /* Function prototype for array_max() */
21 int array_max(const int x[], int size);
22
23 /* Function prototype for
24 int array_min_index(const
25
26 /* Function prototype for
27 void array_mean_std(float
28
29
30 #endif
Dr Matthew Howard © 2020
8 Deparment of Engineering 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 22
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18
#include “array.h” /* Include the array.h library. By convention, non-standard libraries are included with
#define SUCCESS 0 int main(void)
{
int a[] = {21, 6,
1, 3, 10, 45, 55}; /*
printf(“a␣=␣”); array_printf(a,10); /*
return SUCCESS; }
quote marks instead of angle brackets.
*/
28, 36, 15,
Declare and initialise integer array. The size of the array is determined by the number of elements given to initialise it. */
Call function from library to print array contents. */
Listing 7: Sample application code array_app.c that uses the array.h library.
#include “array.h” /* Include the array.h library. By convention, non-standard libraries are included with
#define SUCCESS 0
int main(void) {
quote marks instead of angle brackets.
*/
/* Declare and initialise array a. */
/* Call functions from array.h library. */
/* Print results to stdout. */
return SUCCESS; }
Listing 1: Template code array_app.c for use in testing array.h library.
Dr Matthew Howard 9 © 2020
Deparment of Engineering King’s College London

4CCE1PHC
• Summationa+b=cora−b=c.
• Hadamard1 (i.e., element-wise) product a ◦ b = c. • Dot product a⊤b = c.
• Computation of the length |a|.
• Normalisation a/|a| = c.
4.2 Reverse and Sort
Extend your array.h library to include functions to reverse the order of the elements in a, and sort the elements into order of increasing value.
1 https://en.wikipedia.org/wiki/Hadamard_product_(matrices)
Dr Matthew Howard 10 Deparment of Engineering © 2020 King’s College London

4CCE1PHC
1 /* TO DO: Model answer for part 3 of the lab exercise. */
2 #include
3
4 #define N_VEC 10
5
6 /* add 2 vectors */
7 void vector_add(const int x1[], const int x2[], int y[])
8{
9 int i; /* counter */
10 for (i=0; i< N_VEC; ++i) 11 y[i] = x1[i] + x2[i]; 12 } 13 14 /* input vector */ 15 void InputVector(int x[]) 16 { 17 int i; /* counter */ 18 printf("Enter␣%d␣integers ,␣press␣return␣after␣each:␣\n",N_VEC); 19 for (i=0; i< N_VEC; ++i) 20 scanf("%d", &x[i]); 21 } 22 23 /* output vector */ 24 void OutputVector(const int x[]) 25 { 26 int i; /* counter */ 27 printf("[␣"); 28 for (i=0; i< N_VEC -1; ++i) 29 printf("␣%d,", x[i]); 30 printf("␣%d␣]␣\n", x[N_VEC -1]); 31 } 32 33 int main(void) 34 { 35 int x1[N_VEC], x2[N_VEC], x3[N_VEC]; 36 printf("Vector␣x1␣␣\n"); 37 InputVector(x1); 38 printf("Vector␣x2␣␣\n"); 39 InputVector(x2); 40 vector_add(x1,x2,x3); 41 printf("x1␣+␣x2␣=␣"); 42 OutputVector(x3); 43 } Listing 4: Sample vector library. Dr Matthew Howard 11 © 2020 Deparment of Engineering King’s College London