Programming Homework Help

Create a Program Code C Programming Task

 

Background

1. Dynamic memory allocation

In this assignment, you will use malloc function to allocate dynamic function.

void * malloc(size_t size) allocates the requested memory, and returns a pointer to it, or returns NULL if the allocation fails.

You should always deallocate the memory with the void free(void *ptr) function when you are done using the memory.

  • For example, int * arr = (int *) malloc(10 * sizeof(int)); allocates the memory for 10 integers, and free(arr) releases the allocated memory.

You will use the valgrind tool to debug memory, and detect memory leak. Check this page for the various types of errors that you may have.

  • For example, to use a valgrind to check a program hw4 for memory errors with input file filename, run the following command:

> valgrind --tool=memcheck --leak-check=full ./hw4 filename

2. QSort

QSort is a generic function in C for sorting arrays. In this assignment, you will not need to implement the qsort function. Instead, you will need to read the qsort function and understand how to use this qsort function. The QSort algorithm is based on recusion. You will learn the algorithm later when we discuss recussion in class.

What do you need to do?

You need to complete the following functions:

  • int main(int argc, char * * argv) in main.c, this file contains your main function, which gets the names of the input and output files from the command line (argv[1] and argv[2]), allocate memory, and calls qsort.
  • int countInt(char* filename) in hw4.c
  • bool readInt(char* filename, int * intArr, int size) in hw4.c
  • bool writeInt(char* filename, int * intArr, int size) in hw4.c
  • int compareInt(const void *p1, const void *p2) in hw4.c