C By Discovery (4th Edition)

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

View Full Document Right Arrow Icon
CSE 1320 – Section 001 Midterm 3 – Final Exam – Practice A Name:_____________________________________________________Grade: ___________ [100pts] 1. Given two sets A and B of numbers implemented as two arrays of sorted unique integers (ascending order) with n and m elements, respectively, write a C function void union(int *A, int n, int *B, int m, int **U, int *s), which computes the union set U of sorted unique integers (ascending order) with s elements. Note that memory must be allocated for the set U. void union(int *A, int n, int *B, int m, int **U, int *s) { in ai, bi, numElts, i; ai = 0; bi = 0; numElts = 0; while (ai < n && bi < m) { if (A[ai] < B[bi]) { ai++; // take A[ai] } else if (A[ai] > B[bi]) { bi++; // take B[bi]; } else { ai++; // intersection happens bi++; } numElts++; } if (ai < n) { numElts += (n-ai); } else if (bi < m) { numElts += (m-bi) } *U = (int *) malloc(numElts*sizeof(int)); *s = numElts; ai = 0; bi = 0; numElts = 0; while (ai < n && bi < m) { if (A[ai] < B[bi]) { ai++; // take A[ai] (*U)[numElts] = A[ai]; } else if (A[ai] > B[bi]) { bi++; // take B[bi]; (*U)[numElts] = B[bi]; } else { ai++; // intersection happens bi++; (*U)[numElts] = A[ai]; } numElts++; } if (ai < n) { for (i=ai, i<n; i++) { (*U)[numElts] = A[ai]; numElts ++; } } else if (bi < m) { for (i=bi, i<m; i++) { (*U)[numElts] = B[bi]; numElts ++; } } }
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. Write a C function void print_spiral(int **A, int nr, int nc) that prints a two-dimensional array
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

Practice 3A - CSE 1320 Section 001 Midterm 3 Final Exam...

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

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