Question
LOOK AT BOLD FUNCTIONS Finish the main() function, readNumbers() function, and the writeNumbers() function as described in the comments. There are also 3 input files
LOOK AT BOLD FUNCTIONS Finish the main() function, readNumbers() function, and the writeNumbers() function as described in the comments. There are also 3 input files that you can use to test your code. Input 1, input 2, and input 3. READ DIRECTIONS IN THE ATTACHMENTS. NEEP HELP WITH THIS. The comments are in bubble.c. Thanks
/* bubble.c
This program reads in a list of numbers, prints it to stdout, uses bubble sort to sort it, prints it again, and then writes it to the specified output file. */
#include
// Function prototypes void printArray(int array[], int size); int readNumbers(int array[], char* fname); void writeNumbers(int array[], int size, char* fname); void bubbleSort(int array[], int size, bool isIncreasing); void sort(int* first, int* second, bool isIncreasing); void swap(int* first, int* second);
int main(int argc, char* argv[]) { const int MAX_NUMBERS = 100;
char * inFileName; char * outFileName; int numbers[MAX_NUMBERS]; int count; int exitValue = 1;
if(argc != 3){ puts("Error: this program requires three command line arguments"); puts("Example: ./a.out input1.txt output.txt"); } else{ //set inFileName to argv[1], the second command line argument
count = readNumbers(numbers, inFileName);
if (count 0) { //set outFileName to argv[2], the third command line argument
printf(" "); printArray(numbers, count); bubbleSort(numbers, count, true);
printArray(numbers, count); printf(" "); writeNumbers(numbers, count, outFileName);
exitValue = 0; } }
return exitValue; }
/* readNumbers() This function tries to open the file whose name is contained in the string variable 'fname' and read its contents into the array 'array'.
preconditions: - 'array' is an integer array large enough to hold all of the elements in the file and 'fname' represents a valid filename
postconditions: - If 'fname' can be opened then its contents are read into the array 'array', the function returns the number of elements read. If 'fname' is invalid, then -1 is returned. */ int readNumbers(int array[], char* fname) { int numberRead = 0; // Declare local variables and FILE pointer
// Use fopen to try to open the file for reading
// Test to see if the file was opened correctly
// Now read until !feof(...
// Close the file pointer
// Return the number of items read return numberRead; }
/* writeNumbers() This function writes the first 'size' numbers from array 'array' to a file whose name is contained in 'fname'.
preconditions: - 'array' is an array of integers equal to or larger than size and 'fname' holds a valid filename
postconditions: - If a file can be created then 'size' elements from a are written to that file and it is closed. If the file cannot be created then an error message "The output file could not be created." is printed to stdout. */ void writeNumbers(int array[], int size, char* fname) { // Declare local variables and FILE pointer
// Use fopen to try to open the file for writing
// Test to see if the file was opened correctly
// Use fprintf to write to the file
// close the file
}
/* This function prints the first 'size' elements from the array 'array' to stdout using printf
preconditions: - array' is a valid array with >= 'size' elements
postconditions: - Contents of 'array' are printed to standard output */ void printArray(int array[], int size) { if (size > 0) { int i;
printf("%d", array[0]);
for (i = 1; i
/* bubbleSort() This function implements the bubble sort algorithm
preconditions: - 'array' is an array with at least 'size' elements
postconditions: - If isIncreasing is true, then the array will be sorted into non-decreasing order. If it is false, then the array will be sorted into non-increasing order */ void bubbleSort(int array[], int size, bool isIncreasing) { if (size >= 2) { int i;
for (i = 0; i
for (j = 0; j
/* sort() Sort will interchange the values pointed to by 'first' and 'second' if they aren't in the correct order as specified by 'dir'
preconditions: - integer pointers 'first' and 'second' are non-NULL
postconditions: - If isIncreasing is true then 'second' will be >= 'first' - If isIncreasing is false then 'second' will be *second) ) ) { swap(first, second); } }
/* swap() Swaps the values pointed to by the parameters 'first' and 'second'
preconditions: - Neither 'first' nor 'second' are NULL
postconditions: - The variable pointed to by 'first' has the initial value of the variable pointed to by 'second', and vice versa. */ void swap(int* first, int* second) { int holder;
holder = *first; *first = *second; *second = holder; }
INPUT 1: 134 51 209 74 137 159 4 125 11 158 201 181 119 186 117 21 INPUT 2: 134 51 209 74 137 159 169 125 123 158 201 181 119 186 117 21 11 145 143 231 176 187 173 248 32 115 131 18 14 227 12 147 12 253 252 180 146 188 39 35 80 6 216 198 191 67 250 202 243 127 167 153 49 74 135 80 189 32 129 234 259 141 116 5 128 134 217 40 88 255 75 167 261 57 132 186 123 116 154 132 10 88 19 58 161 185 169 116 216 65 85 241 205 232 246 INPUT 3: Empty
OUTPUT IS SUPPOSED TO LOOK LIKE:
Create a directory for this lab assignment on one of our Unix machines, and download all the following files from Canvas to that directory: bubble. c, input1 .txt, input2.txt, input 3. txt The bubble.c file contains a program with three incomplete functions. The sorting part is already written. Finish the main function, readNumbers function, and the writeNumbers function as described in the comments. There are also 3 input files that you can use to test your code l. The input.txt contains 16 integers. When you run your program, you should see those same 16 integers but in ascending order, and those numbers should also be written to an output file. Check the output file to make sure they are there and in ascending order a .out inputl.txt output.txt 134, 51, 209, 74, 137, 159, 4, 125, 11, 158, 201, 181, 119, 186, 117, 21 4, 11, 21, 51, 74, 117, 119, 125, 134, 137, 158, 159, 181, 186, 201, 209 2. The input2.txt file contains 95 integers. Test your program with that file to make sure it works with a larger collection of numbers. Also, check the output file to make sure they were all written to the file in ascending order. /a out input2. txt output.txt 134 51 209 74, 137 159 169 125 123 158 201 181 119 186 117, 21 11 145 143 231 176 187, 173 248 32, 115 131 18 14 227 12 147, 12 253 252 180 146 188 39 35 80, 6, 216 198 191 67, 250 202 243 127 167, 153 49 74, 135 80 189 32 129 234 259 141 116 5, 128 134 217, 40 88 255 75 167, 261 57, 132 186 123 116 154 132 10 88, 19 58 161 185, 169 116, 216 65 85, 241 205 232, 246 5, 6, 10 11, 12 12 14, 18, 19, 21 32, 32, 35 39 40 49 51 57, 58 65 67, 74, 74, 75 80, 80, 85 88, 88 115 116 116 116 117, 119 123 123 125 127 128 129 131 132 132 134 134 135 137 141 143 145 146 147, 153 154 158 159 161 167, 167 169 169 173 176 180 181 185 186, 186 187 188 189 191 198 201 202 205 209 216 216 217, 227, 231, 232 234, 241, 243 246 248 250 252, 253 255 259 261
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started