int main(int argc, char *argv[]) { int i, item; int quiet_count = 0; node *list, *list_sorted; list = NULL; list_sorted = NULL; if (argc == 1) /* error message if no number entered on command-line */ { fprintf(stderr, "usage: %s [-q] number1 [number2 number3 ...]", argv[0]); exit(1); } /* check optional argument -q in the command-line */ for(i = 1; i < argc; i++) { if (strcmp(argv[i], "-q") == 0) { quiet_count = 1; } else { item = atoi(argv[i]); list = create_node(item, list); } } list_sorted = quicksort(list); if (quiet_count == 0) { print_list(list_sorted); } free_list(list); free_list(list_sorted); print_memory_leaks(); return 0; }
int main(int argc, char *argv[]) { int i; int data; int quiet = 0; /* Value for the "-q" optional argument. */ int counter = 0; /* Counts number of number args */ /* The program name, to be passed to usage() */ char *program_name = argv[0]; node *list = NULL; node *sorted_list; /* Loop through argv[] */ for (i = 1; i < argc; i++) /* Skip argv[0] (program name). */ { if (strcmp(argv[i], "-q") == 0) /* Process -q optional argument. */ { quiet = 1; /* This is used as a boolean value. */ } else /* Process number args */ { /* * Add node to list. If the first * node, make its next field point * to NULL. */ data = atoi(argv[i]); list = create_node(data, list); counter++; } } /* If number arguments were entered, quicksort. */ if (counter > 0) { sorted_list = quicksort(list); free_list(list); list = sorted_list; } else { /* * Else (if no number arguments were entered) * print usage message and exit */ usage(program_name); } /* If -q was not entered, print the sorted list */ if (!quiet) { print_list(list); } free_list(list); print_memory_leaks(); return 0; }
int main(int argc, char *argv[]) { /* The program name, to be passed to usage() */ char *program_name = argv[0]; int num_cells; /* Number of cells in automaton */ int num_gens; /* Number of gens for automaton */ int i; int zero_or_one; /* Temp int to fill array */ int *numbers; /* Array to be used for automaton */ int *numbers2; /* Array used to generate next gen */ /* If not the correct number of elements, print usage and exit */ if (argc != 3) { usage(program_name); } num_cells = atoi(argv[1]); /* Get num cells from args */ num_gens = atoi(argv[2]); /* Get num gens from args */ /* Dynamically allocate memory for the arrays */ numbers = (int *) calloc(num_cells, sizeof(int)); numbers2 = (int *) calloc(num_cells, sizeof(int)); /* Check to make sure the memory is correctly allocated */ if (numbers == NULL || numbers2 == NULL) { fprintf(stderr, "Error! Memory allocation failed!\n"); exit(1); } /* Seed the random generator */ srand(time(NULL)); /* Fill both arrays with random zeroes and ones */ for (i = 0; i < num_cells; i++) { zero_or_one = rand() % 2; numbers[i] = zero_or_one; numbers2[i] = zero_or_one; } /* Print the initial array */ print_int_array(numbers, num_cells); /* Loop that makes a new gen then prints it num_gens number of times */ for (i = 0; i < num_gens; i++) { make_next_gen(numbers, numbers2, num_cells); print_int_array(numbers, num_cells); } /* Free the memory */ free(numbers); free(numbers2); print_memory_leaks(); return 0; }
int main(int argc, char* argv[]) { int i, num_cells, num_generations; int *arr_next, *arr_current; int count_generations = 0; /* * If we do not have two additional command line arguments (number of * cells in 1dCA and number of generations to compute), then print * an error message. * */ if (argc != 3) { cmd_args_error(argv[0]); } num_cells = atoi(argv[1]); num_generations = atoi(argv[2]); /* Initialize and dynamically allocate the memory using calloc. */ arr_next = initialize_arr(num_cells); arr_current = (int *) calloc(num_cells, sizeof(int)); /* * Copy the next array's cells into the current array's cells. Then, * update the next array for the next generation. * */ while (count_generations < num_generations) { for (i = 0; i < num_cells; i++) { arr_current[i] = arr_next[i]; } print_array(arr_current, num_cells); update(arr_next, num_cells); count_generations ++; } /* Free the memory that we calloced. */ free(arr_current); free(arr_next); print_memory_leaks(); return 0; }
int main(int argc, char *argv[]) { int *arr1, *arr2; int i; int num_element = atoi(argv[1]); int num_generation = atoi(argv[2]); /* Check command-line violations */ if (argc > MaxArgc + 1 || num_element == 0 || num_generation == 0) { fprintf(stderr, "usage: %s #cells #generations\n", argv[0]); exit(1); } arr1 = initial_array(num_element); print_arr(arr1, num_element); if (num_generation > 1) /* If there is 1 generation, loop does not work */ { for (i = 1; i < num_generation; i++) { /* Alternate between arr1, arr2 to store the updated array */ if (i % 2 == 1) { arr2 = update(arr1, num_element); print_arr(arr2, num_element); free(arr1); } else { arr1 = update(arr2, num_element); print_arr(arr1, num_element); free(arr2); } } /* Prevent memory leakage of the array used last */ if (num_generation % 2 == 0) { free(arr2); } else { free(arr1); } } else { free(arr1); } print_memory_leaks(); return 0; }
int main(int argc, char *argv[]) { int numberCells; int generations; int *cells; int *nextGen; int i, x = 0; if(argc != 3) { fprintf(stderr, "usage: %s numCells numGens\n", argv[0]); exit(1); } numberCells = atoi(argv[1]); generations = atoi(argv[2]); srand((unsigned) time(0)); cells = (int *) calloc(numberCells, sizeof(int)); nextGen = (int *) calloc(numberCells, sizeof(int)); if(cells == NULL || nextGen == NULL) { fprintf(stderr, "Error! Memory allocation failed!\n"); exit(1); } for(i = 1; i < numberCells - 1; i++) { *(cells + i) = rand() % 2; } printGeneration(numberCells, cells); while(x < generations) { computeNextGenP(numberCells, cells, nextGen); printGeneration(numberCells, nextGen); x++; } free(cells); free(nextGen); print_memory_leaks(); return 0; }
int main(int argc, char *argv[]) { int numCells, numGens, *cells, *cells2, i; /* Print usage when we don't have the three arguments necessary */ if (argc != 3) { fprintf(stderr, "usage: %s (numCells) (numGens)\n", argv[0]); exit(1); } numCells = atoi(argv[1]); numGens = atoi(argv[2]); /* Seed random number generator */ srand(time(0)); cells = (int*) malloc (numCells * sizeof(int)); cells2 = (int*) malloc(numCells * sizeof(int)); if (cells == NULL || cells2 == NULL) { fprintf(stderr, "Memory Allocation Failure!\n"); exit(1); } /* Initialize the arrays with random values */ for (i = 0; i < numCells; i++) { int randomNumber = rand() % 2; cells[i] = randomNumber; cells2[i] = randomNumber; } /* Print the first generation. */ printArray(numCells, cells); /* Alternate between two arrays, calculating the next generation and printing the automaton out. */ for (i = 0; i < numGens; i++) { if (i % 2 == 0) { calculateNextGeneration(numCells, cells, cells2); printArray(numCells, cells2); } else { calculateNextGeneration(numCells, cells2, cells); printArray(numCells, cells); } } free(cells); free(cells2); print_memory_leaks(); return 0; }