Due: Friday, March 20 2026, 11:59 pm

Part I: C Semantics

The purpose of this part is to understand the function handler in C, how to implement polymorphism in non-object-oriented languages such as C.

Tasks

  1. Given an array of random integers, sort the array in a way that the even numbers appear first and the odd numbers appear later. The even numbers should be sorted in descending order and the odd numbers should be sorted in ascending order. For example, given [3, 8, 2, 9, 1, 6], the output of your program should be [8, 6, 2, 1, 3, 9].

    Download the template of the program. Read and understand the program. Implement your comparator function. Compile and run the program. The output of the program should be as follows. Include a screenshot of your output in the README file and explain how you implement the comparator function.

    	The sorted array is: 12 10 8 6 4 2 0 1 3 5 7 9 11 13 
    
  2. Create a new file for this task. Write a function that takes in an integer argument and returns its factorial value as an integer.

    In your main function declare a variable calc as

    	int (*calc)(const int)
    
    1. Assign your factorial function to calc, then use calc to execute the function. Print out its return value. What does this demonstrate with respect to types and functions in C? Include your answer in your report.
    2. Update your function so that it can take in an integer command-line argument. Pass it in as N to the factorial function. Then experiment and in your report include a screenshot of the output and explain what is happening when you go from 12! to 13! to 14! to 15!.
  3. In this task, we'll update our Stack file from last week to work for arbitrary data types. To help you get started, we've created cstk2.h This updated header file is dedicated to this task, and you are not expected to use it for the above integer stack. Your job for this task is to implement the functions in the header file in a new cstk2.c file. You can use cstktest2.c to test your work. In order for it to work, you'll need to:

    1. Finish cstk2.c. Probably the hardest function will now be the toString method. For partial credit, you can assume that we'll only use strings of relatively small size, so allocating space for, say, 1024 characters will be sufficient for all tests. For full credit, ensure that you never allocate more than twice as much memory as necessary, while still maintaining a linear time toString method and detail your method in the report.
    2. Check out lines 15-23 in cstktest2.c; you'll see two functions: intToString and accountToString. Although each has a void * parameter, you need to write a method that casts the given parameter to the appropriate type, then converts that result into an appropriate string. For ints, you should do the obvious thing (10 --> "10"). For Accounts, an Account with the name "Max Bender" and balance 10 should be converted to "Max Bender: 10" (in general: "<name>: <balance>")
    3. At the bottom of cstktest2.c is a small test you can use to confirm that you removed any potential memory leaks in your toString method (which is not trivial, especially if done correctly)

    In your report, include a screenshot of your output of running cstktest2.c and draw the memory contents at mark 1 of this file.

Part II: Semantics of Selected Languages

In this part you'll explore both the syntax and semantics of certain types of statements in your chosen language.

Tasks

  1. What control flow statements does the language provide? Write a program that uses all these control flow statements. Name the source file to task4.xx. For each of the statements, compare it with C if C also has it. Briefly describe the differences if there are any on project 3 Google Sites page.

  2. Function Type: write a program to show how are functions treated in the language as a data type? Name the program to task5.xx. Can you assign a function to a variable? Can you pass a function to another function? If so, how do you then execute the function using the reference? Do you have to name a function? Answer these questions in your Google Sites report and use example code in your explanations.

  3. Implement a general sort algorithm (e.g., quick sort, merge sort, bubble sort, insert sort, ...) that can be used for any type and can be used to obtain any desired order (or as close as you can get). Write a test program for your implementation, and demonstrate your algorithm works with more than one type. (Note that you must implement your own sorting function - don't use built-in sort function.) Include a screenshot of your testing result on your Google Site.

Extensions

  1. A perfectly acceptable extension for any assignment going forward is to do a second language. You are encouraged to pick the second language from List B or C.
  2. Choose other aspects of the selected language, dynamic function definitions, lifetime, or aggregate data type storage and internal representation, and write example programs demonstrating those concepts.
  3. Explore the precedence and semantics of the C memory operators like & and *, or look at whether your languages have similar capabilities.
  4. The haiku Python def shows how the def function adds a new symbol to the computer's state which can subsequently be called.

    You can also choose to make a compilable and runnable haiku in the language that helps to demonstrate the concept of polymorphism. poly.py

  5. Make your comparator function for qsort as efficient as possible. For example, make it uses less operations and control flow statements. Use it to sort a large array. Observe the computation time, and compare it with the less-efficient version if possible. Explain why your implementation is efficient in the comments.

Please note that it is your responsibility to explicitly indicate the extensions you have undertaken. If you complete the extension for the C tasks, please indicate that explicitly in the code and report file. If you complete an extension in a selected language, please indicate this explicitly in your Google Sites report.

Submission

The submission of this project has two components:

  1. Create a folder named Project4_<username> Place the required files into it, zip it, and upload it to the Google Classroom.

    Required Files:

    1. Code: Create a fold for each language, including C and your selected language(s). Place all of your C code and selected language programs to corresponding folders. It should be clear which files correspond to which tasks. All these language folders should be placed into the Project4_<username> folder. Note that the quality of your comments counts toward your grade.
    2. README: We use markdown files for README files. Markdown files are files with the .md filename extension. Each project should have a README file along with all your source codes. The README file will contain:
      • Project identifier, your name, and the submission date.
      • The URL of your Google Sites report.
      • The directory layout for your hand-in folder. You can use the Unix tree command to generate this.
      • Your operating system and C compiler (e.g. use gcc --version).
      • For each task, include how to compile your program, how to run your program, known bugs if there is any, and the program output (a screenshot). The screenshot should include the actual output of running the program if the output is no more than 100 lines. Otherwise, give a brief version of the output.
      • Text and images reporting on the C part of the project.
      • A description of each extension you undertook (for the C part of the project).

      You can follow the instructions for the README file from Project 1.

  2. Please do NOT include: Executable files. On Windows, these have the .exe extension. On Mac and Linux, there is no e xtension. We have found that graders often have difficulty downloading submissions if they contain executable files.

  3. Google Sites Report: The report of this project should have the following elements.
    • Title of the project and your name
    • A section for each task of part II. Every section should contain the snapshot of your sample programs, the outputs, and your explanations.
    • If you complete extensions in your selected language, please include a section for each extension, providing sample programs, outputs, and explanations.

    To check whether you've made your report well organized, please follow the Organization part in the Google Sites section in this writing page. Remember to publish all your changes and include the homepage link of your CS333 Google Sites in the README file.

© 2026 Ying Li. Page last modified: .