Title image Spring 2018

Due: Wednesday 7 March 2018

Part I: C Semantics

The purpose of this part is to understand function handling in C. The library function qsort is good practice for this purpose.

  1. Given an array of random integers, sort the array in a way that the even numbers apper first and the odd numbers apper 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. Read and understand the program. Implement your comparator function. Compile and run the program. The output of the program should be as follows.

    	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)

    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?

    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 explain what is happening when you go from 12! to 13! to 14! to 15!.

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 languages.

Tasks

For your A-list language, complete tasks 1, 2 and 3. Complete tasks 1 and 2 for your B/C-list language. If task 3 doesn't make sense for your B/C-list language, then do the optional task.

  1. What control flow statements does the language provide? Write a program that uses all these control flow statements. Name the source file task1.xx. For each of the statements, in your wiki report compare it with C if C also has a similar control structure. Briefly describe the differences if there are any.
  2. Function Type: write a program to show how/if functions treated in the language as a data type. Name the program task2.xx. Does your language have functions? How do you define a function? 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 wiki report and use example code in your explanations.
  3. Implement a general sort algorithm (e.g., quick sort, merge sort, buble 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.
  4. [Optional] Demonstrate how you can manipulate data, in particular the ordering of data, in your language.

Extensions

  1. A perfectly acceptable extension for any assignment going forward is to do a third language.
  2. Choose other aspects of the language, like polymorphism, 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. Make a compilable and runnable haiku in the language that helps to demonstrate the meaning of a statement in the language.
  5. The haiku Python def shows how the def function adds a new symbol to the computer's state which can subsequently be called.

  6. Make your comparator function for qsort as efficient as possible. 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.

Submission

The submission of this project has three components:

  1. Code: Your qsort, general sort algorithms, and source code for other tasks and extensions should be submitted to the fileserver. Please make the filename of your source code reflect the tasks or extensions.Note that the quality of your comments counts toward your grade.
  2. README: Submit a README file to the fileserver for your C code and the code for your selected languages. The README file can be a .txt file. No matter what, it should be readable from the terminal (no Word files). It should be well-organized such that readers can easily understand the usage and the outputs of the C code and the code for your selected languages. In addition, any known bugs should be in the README file. Follow the format and content instructions from project 1.
  3. Wiki Report: The non-C language pages for this project should have the following elements.
    • Title of the project and your name. Include your partner's name if you worked with someone for the non-C tasks.
    • A section for each task of part II. Each section should contain appropriate snippets of your sample programs, the outputs, and your explanations.
    • If you complete extensions in your selected languages, please include a section for each extension, providing sample programs, outputs, and explanations. Be sure to indicate credit, if necessary.
    To check whether you've made your write-up well organized, please follow the Organization part in the Wiki section on this writing page.

Please note that it is your responsibility to explicitly indicate the extensions you have undertaken. If you complete extensions for the C tasks, please indicate that explicitly in the code and README file. If you complete an extension in a selected language, please indicate this explicitly in your wiki report. Along with each extension, indicate if you completed it with a partner.