[underline]Objectives[/underline][font=TimesNewRoman]Design and write functions that use multidimensional arrays as function arguments. Understand memory allocation and de-allocation using malloc and free.[/font]
[underline]Tutorial Assessment[/underline]The students are required to do [bold][underline]ALL[/underline][/bold] exercises. [font=TimesNewRoman]All assessed exercises need to be submitted in the Assessment section of this module under ICE. For [bold]Exercises 2, 3 and 4 [/bold]you should submit the source code for each exercise separately (no design document is needed).[/font]
[underline]Deadline[/underline]The deadline for submitting the exercises is: [bold][italic]Thursday 22 November 2007 at 4pm.[/italic][/bold]
[font=Times New Roman] [/font]
[underline]Exercise 1[/underline]
[font=TimesNewRoman]Analyse and explain in detail the following program.[/font]
[align=left][font=Times New Roman] [/font]
[/align][align=left][bold][font=Times New Roman]/* dyn_arr.c -- dynamically allocated array */[/font][/bold]
[/align][align=left][bold][font=Times New Roman]#include <stdio.h>[/font][/bold]
[/align][align=left][bold][font=Times New Roman]#include <stdlib.h> // for malloc(), free()[/font][/bold]
[/align][align=left][bold][font=Times New Roman]int main(void)[/font][/bold]
[/align][align=left][bold][font=Times New Roman]{[/font][/bold]
[/align][align=left][bold][font=Times New Roman]double * ptd;[/font][/bold]
[/align][align=left][bold][font=Times New Roman]int max;[/font][/bold]
[/align][align=left][bold][font=Times New Roman]int number;[/font][/bold]
[/align][align=left][bold][font=Times New Roman]int i = 0;[/font][/bold]
[/align][align=left][bold][font=Times New Roman] [/font][/bold]
[/align][align=left][bold][font=Times New Roman]puts("What is the number of type double entries?");[/font][/bold]
[/align][align=left][bold][font=Times New Roman]scanf("%d", &max);[/font][/bold]
[/align][align=left][bold][font=Times New Roman]ptd = (double *) malloc(max * sizeof (double));[/font][/bold]
[/align][align=left][bold][font=Times New Roman]if (ptd == NULL)[/font][/bold]
[/align][align=left][bold][font=Times New Roman]{[/font][/bold]
[/align][align=left][bold][font=Times New Roman]puts("Memory allocation failed. Goodbye.");[/font][/bold]
[/align][align=left][bold][font=Times New Roman]exit(EXIT_FAILURE);[/font][/bold]
[/align][align=left][bold][font=Times New Roman]}[/font][/bold]
[/align][align=left][bold][font=Times New Roman]/* ptd now points to an array of max elements */[/font][/bold]
[/align][align=left][bold][font=Times New Roman]puts("Enter the values (q to quit):");[/font][/bold]
[/align][align=left][bold][font=Times New Roman]while (i < max && scanf("%lf", &ptd[i]) == 1)[/font][/bold]
[/align][align=left][bold][font=Times New Roman]++i;[/font][/bold]
[/align][align=left][bold][font=Times New Roman]printf("Here are your %d entries:\n", number = i);[/font][/bold]
[/align][align=left][bold][font=Times New Roman]for (i = 0; i < number; i++)[/font][/bold]
[/align][align=left][bold][font=Times New Roman]{[/font][/bold]
[/align][align=left][bold][font=Times New Roman]printf("%7.2f ", ptd[i]);[/font][/bold]
[/align][align=left][bold][font=Times New Roman]if (i % 7 == 6)[/font][/bold]
[/align][align=left][bold][font=Times New Roman]putchar('\n');[/font][/bold]
[/align][align=left][bold][font=Times New Roman]}[/font][/bold]
[/align][align=left][bold][font=Times New Roman]if (i % 7 != 0)[/font][/bold]
[/align][align=left][bold][font=Times New Roman]putchar('\n');[/font][/bold]
[/align][align=left][bold][font=Times New Roman]puts("Done.");[/font][/bold]
[/align][align=left][bold][font=Times New Roman]free(ptd);[/font][/bold]
[/align][align=left][bold][font=Times New Roman]return 0;[/font][/bold]
[/align][align=left][bold][font=CourierNewPS-BoldMT]}[/bold][/font]
[/align][align=center][bold][font=Wingdings]F[/font][/bold][bold][underline]Assessed exercises: 2, 3 & 4[/underline][/bold][bold][underline][/underline][/bold]
[/align][underline]Exercise 2 (3 points)[/underline][font=TimesNewRoman]Write a program that declares a 3 by 5 two-dimensional array and initialises it with some values of your choice. The program should:[/font]
- [*][font=Times New Roman]print all the values,[/font][*][font=Times New Roman]double the values[/font][*][font=Times New Roman]print again all the values[/font]
[align=left][font=TimesNewRoman]Write a function that is responsible for displaying the values and a second function that doubles the values. The functions could take as arguments the [bold]array name [/bold]and the [bold]number of rows [/bold]as arguments.[/font]
[/align][underline]Exercise 3 (3 points)[/underline][align=left][font=TimesNewRoman]Write a function that returns the largest value in a two-dimensional array of [bold]int[/bold]. Test the function in a program.[/font]
[/align][underline]Exercise 4 (4 points)[/underline][font=TimesNewRoman]Write a function that returns the largest value in a one-dimensional array of int. Test the function in a program. [italic]Allow the user to specify[/italic] the size of the array at execution time. Allow the user to provide the array values during execution. Use [bold]malloc() [/bold]to allocate space for the array. Use the resulting pointer to call the function that is responsible for processing the array elements. Print the largest value. Free the memory when you finished.[/font]