Question: Codes in C would be much appreciated as I am having trouble writing the codes. Thanks! int main(int argc, char * * argv) int countVector(char*
Codes in C would be much appreciated as I am having trouble writing the codes. Thanks!
int main(int argc, char * * argv)
int countVector(char* filename)
bool readVector(char* filename, Vector * vecArr, int size)
bool writeVector(char* filename, Vector * vecArr, int size)
int compareVector(const void *p1, const void *p2)
Structure
The structure looks like
#typedef struct { int x; int y; int z; } Vector; It contains 3 integer variables, x, y, and z.
| #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); | |
| } | |
| } |
| #include | |
| #include | |
| #include | |
| #include | |
| #include "hw08.h" | |
| #ifdef TEST_MAIN | |
| int main(int argc, char * * argv) | |
| { | |
| // argv[1]: name of input file (binary) | |
| // argv[2]: name of output file (binary) | |
| // check whether there are three arguments. | |
| // If not, return EXIT_FAILURE. DO NOT print anything | |
| // use argv[1] as the input to countVector, save the result | |
| // if the number of vector is 0 or negative, return EXIT_FAILURE | |
| // otherwise, allocate memory for an array of vectors | |
| // read the vectors from the file whose name is argv[1]. save the | |
| // results in the allocated array | |
| // if reading fails, release memory and return EXIT_FAILURE | |
| #ifdef DEBUG | |
| printVector(vecArr, numElem); | |
| #endif | |
| #ifdef DEBUG | |
| printf(" "); | |
| printVector(vecArr, numElem); | |
| #endif | |
| // write the sorted array to the file whose name is argv[2] | |
| // if writing fails, release memory and return EXIT_FAILURE | |
| // release memory, return EXIT_SUCCESS | |
| } | |
| #endif |
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
