{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Practice 3A

# C By Discovery (4th Edition)

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

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 ++; } } }

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

View Full Document
2. Write a C function void print_spiral(int **A, int nr, int nc) that prints a two-dimensional array A with nr rows and nc columns in spiral order in a single line. For example, the 2D array below
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern