int main (int argc, char ** argv) { if (argc != 5) { printf("\nInvalid number of arguments"); return EXIT_FAILURE; } //Assign inputs to variables char * mode = argv[1]; char * in_file = argv[2]; char * seq_file = argv[3]; char * out_file = argv[4]; int Size = 0; long * arr; double N_Comp = 0; double N_Move = 0; clock_t sort_t = 0; //keeps track of sort time clock_t io_sum = 0; //keeps track of total I/O time clock_t io = clock(); arr = Load_File(in_file, &Size); //Load the fille and store the contents in arr io_sum = clock() - io; if (*mode == 'i') { sort_t = clock(); Shell_Insertion_Sort(arr, Size, &N_Comp, &N_Move); sort_t = clock() - sort_t; } else if (*mode == 's') { sort_t = clock(); Shell_Selection_Sort(arr, Size, &N_Comp, &N_Move); sort_t = clock() - sort_t; } else { printf("\nInvalid argument for sort type"); return EXIT_FAILURE; } io = clock(); Print_Seq(seq_file, Size); Save_File(out_file, arr, Size); io_sum += clock() - io; free(arr); printf("Number of comparisons: %le\n", N_Comp); printf("Number of moves: %le\n", N_Move); printf("I/O Time: %le\n", ((double)io_sum) / CLOCKS_PER_SEC); printf("Sorting Time: %le\n", ((double)sort_t) / CLOCKS_PER_SEC); return EXIT_SUCCESS; }
int main (int argc, char ** argv) { if (argc != 5) { printf("There should be four arguments!\n"); printf("the sorting algorithm, input file, sequence file, and output file\n"); return EXIT_FAILURE; } int size = 0; int check = 0; double * n_comp = 0; double * n_move = 0; long * array = Fileload(argv[2], &size); if (array == NULL) { printf("File could not be opened\n"); return EXIT_FAILURE; } if (*argv[1] == 'i') { Shell_Insertion_Sort(array, size, n_comp, n_move); } if (*argv[1] == 's') { Shell_Selection_Sort(array, size, n_comp, n_move); } check = Filesave(argv[4], array, size); if (check == 0) { printf("File did not save properly\n"); free(array); return EXIT_FAILURE; } free(array); return EXIT_SUCCESS; }
int main(int argc, char * argv[]) { // Check to make sure the correct number of input arguments are provided if(argc != 5) { printf("Usage: ./proj1 <sorting_method> <input_file> <sequence_file> <output_file>\n"); return EXIT_FAILURE; } // Set these variables to the input arguments (Make things easier) char method = * argv[1]; char * input_file = argv[2]; char * sequence_file = argv[3]; char * output_file = argv[4]; // Used to store values used in this program int Size; long * values; clock_t t_sort, t_load, t_save, t_seq; // Load the file, keeping track of the time it takes to load the file t_load = clock(); values = Load_File(input_file, &Size); t_load = clock() - t_load; // Check to make sure the files was loaded correctly if(values == NULL) { printf("Error loading the input file\n"); return EXIT_FAILURE; } // Print the sequence to a file, keeping track of the time it takes to write to the file t_seq = clock(); int i = Print_Seq(sequence_file, Size); t_seq = clock() - t_seq; // Check to make sure the sequence was correctly output to the file if(i == 0) { printf("Failed to print sequence\n"); free(values); return EXIT_FAILURE; } // Used to keep track of the number of moves and comparisons in the sorting algorithms double N_Comp = 0.0, N_Move = 0.0; // Check for the sorting method chosen by the user // Keep track of the time it takes to sort the values as well t_sort = clock(); if(method == 'i') { Shell_Insertion_Sort(values, Size, &N_Comp, &N_Move); } else if(method == 's') { Shell_Selection_Sort(values, Size, &N_Comp, &N_Move); } else { printf("Sorting method must be either i (Insertion) or s (Selection) sort\n"); free(values); return EXIT_FAILURE; } t_sort = clock() - t_sort; // Save the sorted array to the output file, keeping track of th time it takes as well t_save = clock(); int returned = Save_File(output_file, values, Size); t_save = clock() - t_save; // Check to make sure that the file was correctly output to if (!returned) { printf("Failed to output values to file\n"); free(values); return EXIT_FAILURE; } else if(returned != Size) { printf("Number of values returned not equal to number of values specified\n"); free(values); return EXIT_FAILURE; } // Free the malloced values free(values); // Display all the required information to the screen Screen_Dump(N_Comp, N_Move, (t_save + t_load + t_seq) / CLOCKS_PER_SEC, t_sort); // SUCCESS!!! return EXIT_SUCCESS; }
int main ( int Argc, char **Argv) { // main program - menu for functions with using database double cstart, cend; int Response = 0; int Saved = 0; int Size = 0; double N_Comp = 0; double N_Move = 0; long *Array = NULL; char Filename[MAXFILELEN] = ""; while (1) { printf("\n"); printf("TEST MENU\n"); printf("1. Load array from file\n"); printf("2. Save array to file\n"); printf("3. Shell Sort (Insertion)\n"); printf("4. Improved Bubble Sort\n"); printf("5. Exit\n"); printf("Enter your choice: "); scanf("%d",&Response); getchar(); if (Response == 5) // quit program { if (Array != NULL) { printf("Removing and deallocating array\n"); free((void *)Array); Array = NULL; printf("done!\n"); } return (OK); } if (Response == 1) // load file { if (Array != NULL) { printf("\nRemoving and deallocating array\n"); free((void *)Array); Array = NULL; printf("done!\n"); } printf("\nEnter input file (including path): "); scanf("%s", Filename); Array = Load_File (Filename, &Size); if (Size <= 0) { printf("\nError in inputs, file not loaded..\n"); if (Array != NULL) { printf("Removing and deallocating array\n"); free((void *)Array); Array = NULL; printf("done!\n"); } } else { printf("\nLoaded %d long integers\n", Size); } } if (Response == 2) // save file { if (Array == NULL) { printf("\nMust load in data from file (option 1) before saving one!\n"); } else { printf("\nEnter output file (including path): "); scanf("%s", Filename); Saved = Save_File (Filename, Array, Size); if (Saved != Size) { printf("Error in saving! Only %d out of %d long integers saved\n", Saved, Size); } else { printf("Saved all %d long integers\n", Saved); } } } if ( (Response > 2) && (Response < 5)) { if (Array == NULL) { printf("\nMust load in data from file (option 1) before sorting one!\n"); } else { // initialize time function cstart = (double) clock(); // initialize numbers of comparisons and moves N_Comp = 0; N_Move = 0; switch(Response) { case 3: printf("Sorting by Shell Sort (Insertion)\n"); Shell_Insertion_Sort (Array, Size, &N_Comp, &N_Move); break; case 4: printf("Sorting by Improved Bubble Sort\n"); Improved_Bubble_Sort (Array, Size, &N_Comp, &N_Move); break; } // print results: Time, N_Comp, N_Move cend = (double) clock(); printf("\n"); printf(" Elapsed Time (sec): %f\n", (cend - cstart)/CLOCKS_PER_SEC); printf(" # Comparisons: %f\n", N_Comp); printf(" # Moves: %f\n", N_Move); } } } return (OK); } // main()