仅使用合并多个数组(Merging multiple arrays using only & )

我需要构建一个函数,它只使用stdio.h和stdlib.h将3个排序的整数数组合并成一个单独的排序数组。 我无法弄清楚如何使用limits.h构建一个,我还是无法弄清楚如何修改它以使用limits.h运行,或者从头开始构建可以执行任务的代码。 非常感谢任何和所有的帮助。

这是我的limits.h代码:

int multimerge( int * const * const arrays, // arrays holding the data int const * const arraysizes, // sizes of the arrays in `arrays` int number_of_arrays, // number of arrays int * const output // pointer to output buffer ){ int i = 0; // output cursor int j = 0; // index for minimum search int min; // minimum in this iteration int minposition; // position of the minimum // cursor for the arrays int * cursor = calloc(number_of_arrays,sizeof(int)); if(cursor == NULL) return -1; while(1){ min = INT_MAX; minposition = -1; // invalid position // Go through the current positions and get the minimum for(j = 0; j < number_of_arrays; ++j){ if(cursor[j] < arraysizes[j] && // ensure that the cursor is still valid arrays[j][cursor[j]] < min){ // the element is smaller min = arrays[j][cursor[j]]; // save the minimum ... minposition = j; // ... and its position } } // if there is no minimum, then the position will be invalid if(minposition == -1) break; // update the output and the specific cursor output[i++] = min; cursor[minposition]++; } free(cursor); return 0; }

I am required to build a function that merges 3 sorted arrays of integers into a singular sorted array using only stdio.h and stdlib.h. I am unable to figure out how to build one with limits.h and I am as of yet unable to figure out how to modify it to run with limits.h or build a code from scratch that can perform the task. Any and all help is greatly appreciated.

This is my code with limits.h:

int multimerge( int * const * const arrays, // arrays holding the data int const * const arraysizes, // sizes of the arrays in `arrays` int number_of_arrays, // number of arrays int * const output // pointer to output buffer ){ int i = 0; // output cursor int j = 0; // index for minimum search int min; // minimum in this iteration int minposition; // position of the minimum // cursor for the arrays int * cursor = calloc(number_of_arrays,sizeof(int)); if(cursor == NULL) return -1; while(1){ min = INT_MAX; minposition = -1; // invalid position // Go through the current positions and get the minimum for(j = 0; j < number_of_arrays; ++j){ if(cursor[j] < arraysizes[j] && // ensure that the cursor is still valid arrays[j][cursor[j]] < min){ // the element is smaller min = arrays[j][cursor[j]]; // save the minimum ... minposition = j; // ... and its position } } // if there is no minimum, then the position will be invalid if(minposition == -1) break; // update the output and the specific cursor output[i++] = min; cursor[minposition]++; } free(cursor); return 0; }

最满意答案

仅当您使用头文件中的任何常量时,才需要包含limits.sh。 在您的程序中,您使用的是INT_MAX。 如果您使用自己的常量,则无需使用头文件。 但您需要根据数据类型的大小或根据您的要求定义一些最大值来定义常量。

limits.sh needs to be included only if you use any constants from the header file. In your program, you are using INT_MAX. If you use your own constants then you need not use from the header file. But you need to define your constants based on the size of the data types or some max values based on your requirement.

更多推荐