Question: The language is in C and codes with explanations for the complex parts would be appreciated. This assignment asks you to write a program that
The language is in C and codes with explanations for the complex parts would be appreciated.
This assignment asks you to write a program that uses qsort (a function provided by C) to sort an array of integers in the ascending order.
You will learn to
Count the number of integers in a file
Allocate memory (an array) to store the integers
Read integers from the file and store them in the array
Call qsort function to sort the integers
Write the sorted array to another file
Release memory
Check memory error using valgrin
Warning: This assignment is much much harder than all previous assignments combined. You must start early.
Need to complete the following functions:
int main(int argc, char * * argv)
int countInt(char* filename)
bool readInt(char* filename, int * intArr, int size)
bool writeInt(char* filename, int * intArr, int size)
int compareInt(const void *p1, const void *p2)
| #include | |
| #include | |
| #include | |
| #include | |
| #include "hw07.h" | |
| #ifdef TEST_MAIN | |
| int main(int argc, char * * argv) | |
| { | |
| // argv[1]: name of input file | |
| // argv[2]: name of output file | |
| // if argc is not 3, return EXIT_FAILURE | |
| if (argc != 3){ | |
| return EXIT_FAILURE; | |
| } | |
| // count the number of integers in the file | |
| int numElem = 0; | |
| numElem = countInt(argv[1]); | |
| if (numElem == -1) // fopen fails | |
| { | |
| return EXIT_FAILURE; | |
| } | |
| // allocate memory for the integers in the file | |
| // 1. create a pointer variable | |
| // 2. allocate memory | |
| // 3. check whether allocation succeed | |
| // if allocation fails, return EXIT_FAILURE | |
| int * intArr = (int *)malloc(numElem * sizeof(int)); | |
| if (intArr == NULL){ | |
| return EXIT_FAILURE; | |
| } | |
| bool rtv = readInt(argv[1], intArr, numElem); | |
| if (rtv == false) //if read fails, return EXIT_FAILURE | |
| { | |
| free(intArr); | |
| return EXIT_FAILURE; | |
| } | |
| // call qsort using the comparison function you write | |
| qsort(intArr, numElem, sizeof(int), compareInt); | |
| // write the sorted array to a file whose name is argv[2] | |
| rtv = writeInt(argv[2], intArr, numElem); | |
| if (rtv == false) // read fail | |
| { | |
| // release memory | |
| free(intArr); | |
| return EXIT_FAILURE; | |
| } | |
| // everything is ok, release memory, return EXIT_SUCCESS | |
| free(intArr); | |
| return EXIT_SUCCESS; | |
| } | |
| #endif |
| #include | |
| #include | |
| #include | |
| #include | |
| #include "hw08.h" | |
| #ifdef TEST_COUNTVECTOR | |
| int countVector(char * filename) | |
| { | |
| // count the number of vectors in the file and return the number | |
| // The input is a binary file. You must use fread. | |
| // You must not use fscanf(, "%d", ) | |
| // | |
| // If fopen fails, return -1 | |
| // | |
| // | |
| // For the mode of fopen, you may use "r" without b | |
| // | |
| } | |
| #endif | |
| #ifdef TEST_READVECTOR | |
| bool readVector(char* filename, Vector * vecArr, int size) | |
| { | |
| // if fopen fails, return false | |
| // read Vectors from the file. | |
| // | |
| // | |
| // if the number of integers is different from size (too | |
| // few or too many) return false | |
| // | |
| // if everything is fine, fclose and return true | |
| } | |
| #endif | |
| #ifdef TEST_COMPAREVECTOR | |
| int compareVector(const void *p1, const void *p2) | |
| { | |
| // compare the x attribute first | |
| // If the first vector's x is less than the second vector's x | |
| // return -1 | |
| // If the first vector's x is greater than the second vector's x | |
| // return 1 | |
| // If the two vectors' x is the same, compare the y attribute | |
| // | |
| // If the first vector's y is less than the second vector's y | |
| // return -1 | |
| // If the first vector's y is greater than the second vector's y | |
| // return 1 | |
| // If the two vectors' y is the same, compare the z attribute | |
| // | |
| // If the first vector's z is less than the second vector's z | |
| // return -1 | |
| // If the first vector's z is greater than the second vector's z | |
| // return 1 | |
| // If the two vectors' x, y, z are the same (pairwise), return 0 | |
| } | |
| #endif | |
| #ifdef TEST_WRITEVECTOR | |
| bool writeVector(char* filename, Vector * vecArr, int size) | |
| { | |
| // if fopen fails, return false | |
| // write the array to file using fwrite | |
| // need to check how many have been written | |
| // if not all are written, fclose and return false | |
| // | |
| // fclose and return true | |
| } | |
| #endif | |
| // This function is provided to you. No need to change | |
| void printVector(Vector * vecArr, int size) | |
| { | |
| int ind = 0; | |
| for (ind = 0; ind < size; ind ++) | |
| { | |
| printf("%6d %6d %6d ", | |
| vecArr[ind].x, vecArr[ind].y, vecArr[ind].z); | |
| } | |
| } |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
