{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

C By Discovery (4th Edition)

Info icon This 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 ++; } } }
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern