Hello, there is a function that rearranges the elements of the array in reverse order. Here is the code:

#include <stdio.h> #include <string.h> #include <stdlib.h> long double array[] = { 0.304411824158121, 0.702270983146229, 0.675987113307883, 0.652289676782947, 0.975411752562938, 0.284802741036713, 0.877898595240810, 0.726726914957288, 0.084725908720116, 0.946546040643011 }; void revarray(void*, unsigned long, unsigned long); int main(int argc, char** argv) { revarray(array, 10, sizeof(long double)); int i; for (i = 0; i < 10; i++) { printf("%.15Lf\n", array[i]); } return 0; } void revarray(void* base, unsigned long nel, unsigned long width) { void* ptr = malloc(width); int i = 0; int k = 0; int len = 0; if (nel % 2 == 0) { len = nel / 2; for (i = 0; i < len; i++) { memccpy(ptr, base + (nel - i - 1)*width, nel, width); memccpy(base + (nel - i - 1)*width, base + i * width, nel, width); memccpy(base + i * width, ptr, nel, width); } } else { len = nel / 2 + 1; for (i = 0; i < len; i++) { if (i == len - 1) { break; } memccpy(ptr, base + i * width, nel, width); memccpy(base + i * width, base + (nel - i - 1)*width, nel, width); memccpy(base + (nel - i - 1)*width, ptr, nel, width); } } free(ptr); } 

Do not tell me why the function does not work for real numbers, displays all zeros.

    2 answers 2

    Sorry, to understand your bells and whistles ... Why not make it easier?

     void revarray(long double * arr, int size) { for(int i = 0, j = size-1; i < j; ++i, --j) { long double tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } } 

    If so generalization is needed -

     void rev_array(void * arr, int count, int size) { unsigned char * x = (unsigned char *)arr; for(int i = 0, j = count-1; i < j; ++i, --j) { unsigned char * from = x + i*size; unsigned char * to = x + j*size; for(int k = 0; k < size; ++k) { unsigned char tmp = from[k]; from[k] = to[k]; to[k] = tmp; } } } 

    Here is the challenge:

     int main(int argc, char** argv) { int i; for (i = 0; i < 10; i++) { printf("%.15Lf\n", array[i]); } puts(""); puts(""); puts(""); puts(""); rev_array(array, 10, sizeof(long double)); for (i = 0; i < 10; i++) { printf("%.15Lf\n", array[i]); } return 0; } 
    • you need to make a function - MeloDy.
    • void revarray (void * base, unsigned long nel, unsigned long width) {...} - MeloDy.
    • No questions, see the amended answer. - Harry
    • thank you, figure it out - MeloDy.

    Somehow all too difficult and incomprehensible :) If you really want to work with uncertain data, then:

     #include <stdio.h> #include <string.h> #include <stdlib.h> static long double array[] = { 0.304411824158121, 0.702270983146229, 0.675987113307883, 0.652289676782947, 0.975411752562938, 0.284802741036713, 0.877898595240810, 0.726726914957288, 0.084725908720116, 0.946546040643011 }; static void rev_array( void *array, size_t nelem, size_t width ) { size_t i, j; char *data = ( char * )array; char *tmp = malloc( width ); for( i = 0, j = nelem - 1; i < j; i++, j-- ) { memcpy( tmp, data + j * width, width ); memcpy( data + j * width, data + i * width, width ); memcpy( data + i * width, tmp, width ); } free( tmp ); } static void print_array( long double *array, size_t size ) { size_t i; for( i = 0; i < size; i++ ) { printf( "%.15Lf\n", array[i] ); } } int main() { print_array( array, sizeof( array ) / sizeof( array[0] ) ); rev_array( array, sizeof( array ) / sizeof( array[0] ), sizeof( array[0] ) ); printf( "---\n" ); print_array( array, sizeof( array ) / sizeof( array[0] ) ); return 0; } 

    Conclusion, it is here: http://ideone.com/BLRIY2

     0.304411824158121 0.702270983146229 0.675987113307883 0.652289676782947 0.975411752562938 0.284802741036713 0.877898595240810 0.726726914957288 0.084725908720116 0.946546040643011 --- 0.946546040643011 0.084725908720116 0.726726914957288 0.877898595240810 0.284802741036713 0.975411752562938 0.652289676782947 0.675987113307883 0.702270983146229 0.304411824158121 
    • With one addition - I would allocate memory on the stack. - Harry
    • @Pink Tux if I understood correctly, tried to run, also zeros - MeloDy.
    • @MeloDy. it means you have a straight road under the debugger. The conclusion that I have - see the answer. - PinkTux
    • @Pink Tux Thank you, I had problems .. - MeloDy.