Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CPT109 C programming and SW engineering 1

Lab Practice 6 – Pointers and Arrays

Use your preferred compiler to investigate the programming exercises below. This laboratory concerns programs for the use of arrays in function calls. It will develop your ability to use pointers.

Exercise 1

What is the value of *ptr and *(ptr+2) in each case below?

Example a

#include <stdio.h>

int main(){

int *ptr;

int arr2D[2][3] = {2,3,4,5,6,7};

ptr=arr2D[0];              /*what are the values of *ptr and *(ptr+2) here*/

return 0;

}

Example b

#include <stdio.h>

int main(){

int *ptr;

int arr2D[3][3] = {{2},{3,4,5},{6}};

ptr=arr2D[0];              /*what are the values of *ptr and *(ptr+2) here*/

return 0;

}

Note: a two dimensional array is initialized starting with the first row. The elements provided in the curly brackets (example a) are assigned in turn unless additional curly ‘{}’ brackets are used to indicate in which row these should be assigned. If you do not provide sufficient values in between the curly brackets, there elements will be set to 0.

Exercise 2

Consider the following two-dimensional array declared as follow:

float t[3][4];

Using pointers, write a program that initializes the array t using values of your choice and calculates the sum of the values of its elements.

Exercise 3

Write a function that returns the index of the largest value in an array of int. Test the function in a program with input data of your choice.

Exercise 4

Write a function that returns the difference between the largest and smallest elements in an array of type double. Test the function in a program with input data of your choice.

Exercise 5

Write a function that adds the elements of two one-dimensional arrays of the same length. The results should be stored in a third array. The function should take as arguments the name of the three arrays and the array dimensions.

Write a simple program that uses the function. The two arrays should be declared at the beginning of the program (inside the main( ) function).