void quick_sort(int *s, int start, int end) { if(start >= end) return; int i = start; int j = end; printf("put %d in right position\n", s[start]); while(i != j) { while(s[j] >= s[start] && j>i) { j--; } while(s[i] <= s[start] && i<j) { i++; } if(i != j) { printf("swap %d %d\n", s[i], s[j]); s[i] = s[i] ^ s[j]; s[j] = s[i] ^ s[j]; s[i] = s[i] ^ s[j]; show_array(s, size); } } if(start != i) { printf("swap %d %d\n", s[start], s[i]); s[start] = s[start] ^ s[i]; s[i] = s[start] ^ s[i]; s[start] = s[start] ^ s[i]; show_array(s, size); } quick_sort(s, start, i-1); quick_sort(s, i+1, end); }
int main() { int values[SIZE] = {1,2,3,4,5,6,7,8,9,10}; int target[SIZE]; double curious[SIZE / 2] = {1.0, 2.0, 3.0, 4.0, 5.0}; puts("memcpy() used:"); puts("values (original data): "); show_array(values, SIZE); memcpy(target, values, SIZE * sizeof(int)); puts("target (copy of values):"); show_array(target, SIZE); puts("\nUsing memmove() with overlapping ranges:"); memmove(values + 2, values, 5 * sizeof(int)); puts("values -- elements 0-5 copied to 2-7:"); show_array(values, SIZE); puts("\nUsing memcpy() to copy double to int:"); memcpy(target, curious, (SIZE / 2) * sizeof(double)); puts("target -- 5 doubles into 10 int positions:"); show_array(target, SIZE); return 0; }
static void normalize_array(uint32_t n) { printf("--- original ---\n"); show_array(n); printf("--- normalized ---\n"); n = normalize_varexp_array(a, n); show_array(n); printf("---\n\n"); }
int run_game(int nb_lin, int nb_col, unsigned char board[nb_lin][nb_col] ) { unsigned char continue_game = 1; unsigned char nb_boxes = nb_lin * nb_col; unsigned char nb_player = 0; unsigned char winner = 0; do { if ( nb_boxes == 0 ) { winner = 0; continue_game = 0; break; } // Print the board #ifdef __DEBUG__ show_array(nb_lin, nb_col, board); #else clear_screen(); // Clear the screen show_board(nb_lin, nb_col, board); #endif if ( run_a_step(nb_lin, nb_col, board, nb_player) ) { winner = nb_player + 1; continue_game = 0; break; } // Change the player ++nb_player; nb_player %= 2; // Decrement the number of boxes available nb_boxes--; } while ( continue_game ); // Print the board #ifdef __DEBUG__ show_array(nb_lin, nb_col, board); #else clear_screen(); show_board(nb_lin, nb_col, board); #endif return (winner); }
int main(int argc, char *argv[]) { show_array(&sample); if (OK == merge_sort(&sample)) show_array(&sample); return 0; }
int main(void) { int a[N]; init_array(a, N); show_array(a, N); insert_sort(a, N); show_array(a, N); return 0; }
int main(void) { double array[SIZE] = {20.0, 17.66, 8.2, 15.3, 22.22}; printf("The original array:\n"); show_array(array, SIZE); mult_array(array, SIZE, 2.5); printf("The array after calling mult_array():\n"); show_array(array, SIZE); return EXIT_SUCCESS; }
int main(void) { int a[N]; init_array(a, N); show_array(a, N); bubble_sort(a, N); show_array(a, N); return 0; }
int main(void) { int a[N]; init_array(a, N); show_array(a, N); quick_sort(a, 0, N - 1); show_array(a, N); return 0; }
int main (void) { double dip[SIZE] = {20.0, 17.66, 8.2, 15.3, 22.22}; printf("The original dip array:\n"); show_array (dip, SIZE); mult_array (dip, SIZE, 2.5); printf("The dip array after calling mult_array():\n"); show_array(dip, SIZE); return 0; }
int main() { double arr_of_dbl[MAX_SZ]; unsigned size = fill_array(arr_of_dbl, MAX_SZ); show_array(arr_of_dbl, size); std::cout << std::endl; reverse_array(arr_of_dbl, size); show_array(arr_of_dbl, size); std::cout << std::endl; return 0; }
int main (void) { int n; float tmp, va[MAX_ELEMENTS], *pVa, *pVaB; printf("Combien d'elements souhaitez-vous encoder (1~%d) : ", MAX_ELEMENTS); scanf("%d", &n); if (n < 1) n = 1; else if (n > MAX_ELEMENTS) n = MAX_ELEMENTS; for (pVa = va; pVa < va + n; pVa++) scanf("%f", pVa); for (pVa = va, pVaB = va + n - 1; pVa < va + (n / 2 + n % 2); pVa++, pVaB--) { tmp = *pVa; *pVa = 1 / *pVaB; *pVaB = 1 / tmp; } show_array(&va[0], n); }
int main (void) { int n, i; float tmp, va[MAX_ELEMENTS]; printf("Combien d'elements souhaitez-vous encoder (1~%d) : ", MAX_ELEMENTS); scanf("%d", &n); if (n < 1) n = 1; else if (n > MAX_ELEMENTS) n = MAX_ELEMENTS; for (i = 0; i < n; i++) scanf("%f", &va[i]); for (i = 0; i < n / 2 + n % 2; i++) { tmp = va[i]; va[i] = 1 / va[n - 1 - i]; va[n - 1 - i] = 1 / tmp; } show_array(&va[0], n); }
int main(int argc, char* argv[]) { #define COUNT 10 #define RSEED 100 int i; int array[COUNT]; srand((unsigned int)time(NULL)); for (i = 0; i < COUNT; ++i) array[i] = rand() % RSEED; show_array(array, COUNT); bubble_sort(array, COUNT); show_array(array, COUNT); return 0; }
void show_2d_array(int rows, int cols, double ar[rows][cols]) { int i; for (i = 0; i < rows; i++) show_array(cols, ar[i]); }
int main(void) { int array[10] = {4, 2, 3, 1, 5, 6, 8, 9, 0, 7}; /* int array[5] = {4, 3, 2, 1, 5}; */ // 数组长度 size = (int)sizeof(array)/sizeof(array[0]); printf("show array at first\n"); show_array(array, size); quick_sort(array, 0, size-1); printf("show array at last\n"); show_array(array, size); return 0; }
int main(void) { int i; //float f = 123.456; long l = 1234567; void *vPtr; // Initialize usart for printf usart0_init_baud(57600); // Load memory with 64 char for(i = 0; i < 64; i++) { myArray.cArray[i] = '!' + (char)i; } // Point global void pointer to array vPtr = myArray.cArray; // Show the characters show_array(vPtr,CHAR); // Load memory with 32 ints for(i = 0; i < 32; i++) { myArray.iArray[i] = i+10; } // Point global void pointer to array vPtr = myArray.iArray; // Show the characters show_array(vPtr,INT); // Load memory with 16 floats for(i = 0; i < 16; i++) { myArray.lArray[i] = l + (long)i; } // Point global void pointer to array vPtr = myArray.lArray; // Show the characters show_array(vPtr,LONG); return 0; }
int main(void) { double source[SIZE] = {4.3, 1.1, 4.3, 5.9, -20.5}; printf("Array: "); show_array(SIZE, source); printf("Index of max: %d\n", max_i(SIZE, source)); return 0; }
int main(void) { int source[9] = {4, 124, 5, 1, 56, 7, 1, 299, 77}; printf("Source: "); show_array(9, source); printf("Max: %d\n", max(9, source)); return 0; }
int main(void) { double source[SIZE] = {4.3, 11.5, 9.9, 8.4, 0.5}; printf("Array: "); show_array(SIZE, source); printf("The diff between the largest and the smallest elements: %.1f\n", max(SIZE, source) - min(SIZE, source)); return 0; }
int main() { int test[][10] = { {6,4,1,0,3,7,8,9,5,2}, {63,43,121,60,73,37,28,19,52,21}, {96,94,71,60,32,73,84,95,56,62}, {66,49,31,40,43,73,81,92,53,25}, {62,14,21,20,43,37,856,964,53,2}, {61,24,19,63,35,70,88,97,56,72}, {6,34,14,30,3,7,82,94,56,26} }; printf("#test bubble sort\n"); dump_sort(TEST_SIZE, bubble_sort, int_compare); printf("#test insert sort\n"); dump_sort(TEST_SIZE, insert_sort, int_compare); printf("#test select sort\n"); dump_sort(TEST_SIZE, select_sort, int_compare); printf("#test shell sort\n"); dump_sort(TEST_SIZE, shell_sort, int_compare); printf("#test merge sort\n"); dump_sort(TEST_SIZE, merge_sort, int_compare); printf("#test quick sort\n"); dump_sort(TEST_SIZE, quick_sort, int_compare); printf("#test min heap sort\n"); printf("source array:"); show_array(test[0], 10); MakeMinHeap((void**)test[0], 10, int_compare); MinHeapSort((void**)test[0], 10, int_compare); printf("dest array:"); show_array(test[0], 10); printf("\n"); }
int main (void) { // Read the array in first int ** matrix = malloc (sizeof (int*) * DIMENSIONS); int index = 0; while (index < DIMENSIONS) { matrix[index] = malloc (sizeof (int) * DIMENSIONS); index++; } srand (time(NULL)); int col = 0; while (col < DIMENSIONS) { int row = 0; while (row < DIMENSIONS) { int value = rand() % 100; if (value < 100-FREQUENCY) { matrix[col][row] = 0; } else { matrix[col][row] = 1; } row++; } col++; } // Show the matrix show_array (matrix); // Keep track of values in an array int nextFree = 0; int * areas = malloc (sizeof (int) * DIMENSIONS * DIMENSIONS); // Parse matrix parse_array (matrix, areas, &nextFree); // See what values we come up with index = 0; printf ("AREAS: "); while (index < nextFree) { printf ("%d ", areas[index]); index++; } printf("\n"); // Perform quick select for median. printf ("Median Area: %d\n", quickselect (areas, nextFree - 1)); return EXIT_SUCCESS; }
int main(void) { int size = SIZE_ARRAY; int Array[SIZE_ARRAY]; double res; load_array(size, Array); show_array(size, Array); res = get_mean(size, Array); printf("%.3f\n", res); return (0); }
int main(int argc, char ** argv) { int * pa; int size; int value; printf("Enter the number of elements: "); scanf("%d", &size); while(size > 0){ printf("Enter the initialization value: "); scanf("%d", &value); pa = make_array(size, value); if(pa){ show_array(pa, size); free(pa); } printf("Enter the number of elements(<1 to quit):"); scanf("%d", &size); } printf("Done.\n"); exit(EXIT_SUCCESS); }
int main(void) { int *pa, size, value; printf("Enter the number of elements: "); scanf_s("%d", &size); while (size > 0) { printf("Enter the initialization value: "); scanf_s("%d", &value); pa = make_array(size, value); if (pa) { show_array(pa, size); free(pa); } printf("Enter the number of elements(-1 to quit): "); scanf_s("%d", &size); } printf("Done.\n"); return 0; }
void func_12_8(void){ int *pa; int size; int value; char ch; while((ch=getchar()) != '\n'); printf("Enter the number of elements:"); scanf("%d",&size); while(size>0){ printf("Enter the initialization value:"); scanf("%d",&value); pa = make_array(size,value); if(pa){ show_array(pa,size); free(pa); } printf("Enter the number of elements (<1 to qiut):"); scanf("%d",&size); } printf("Done.\n"); }
int main(){ #if 1 int* p = NULL; mem_creat(&p, 100); *p = 123; printf("%d\n", *p); mem_free(&p); printf("%p\n", p); #endif #if 0 size_t row = 3, col = 4; int** p = new_array(row, col); //set_array(p, row, col); show_array(p, row, col); #endif }
int main(void) { int * pa; int size; int value; printf("Enter the number of elements: "); while(scanf("%d", &size) == 1 && size > 0) { printf("Enter the initialization value; "); scanf("%d", &value); pa = make_array(size, value); if (pa) { show_array(pa, size); free(pa); } fputs("Enter the number of elements ( <1 to quit): ", stdout); } puts("Done."); return 0; }
void test_even(){ int a[] = {1, 3, 5, 7, 9, 11}; reverse_array(a, 6); show_array(a, 6); }
void test_odd(){ int a[] = {1, 3, 5, 7, 9}; reverse_array(a, 5); show_array(a, 5); }