quiz4 - * returns a pointer to the allocated storage; * /...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming Using C Quiz 3 Name: 1. [50 p ] Consider the following declaration: typedef struct { enum {RECTANGLE, CIRCLE} shape_kind; struct point center; union { struct { double height, width; } rectangle; struct { double radius; } circle; } u; } Shape; Shape s, *p; (a) [5 p ] Write an appropriate declaration for point, which represents the coordinates of a point in 2D. Indicate which of the following statements are legal, and show how to repair the ones that aren’t (b) [5 p ] s.shape kind = RECTANGLE (c) [5 p ] printf(“%s”, s.shape kind); (d) [5 p ] s.center.x = 10; (e) [5 p ] s.height = 25; (f) [5 p ] s.u.circle = 5; (g) [5 p ] p = malloc(sizeof(union u)); (h) [5 p ] p.shape kind = CIRCLE; (i) [5 p ] p.center = (*s).center; (j) [5 p ] free(p); p = &s; 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2. [50 p ] Write a program that maintains a sorted, dynamically allocated array of integers. The program should use the following functions: /* allocates storage for n integers
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: * returns a pointer to the allocated storage; * / int * create_array(int n); /* destroys the array passed as argument * by releasing the memory occupied by it * / void release_array(int *a); /* Inserts new_element in array a at the proper position. * a is assumed to be sorted * n is the number of elements already inserted in a * After the insertion, a should be still sorted * All elements following new_element are shifted right * one position from their initial position * The function assumes that the storage allocated for a * is large enough to accomodate a new element */ int insert(int *a, int n, int new_element); The main function should create a dynamic array, insert 3 integers of your choice, print the second one in the array and release the array. 2...
View Full Document

Page1 / 2

quiz4 - * returns a pointer to the allocated storage; * /...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online