Home > Software design >  array flip function С
array flip function С

Time:12-24

here is the code, you need to write a function to flip the array backwards, please help example: (the first element is swapped with the last, the second with the penultimate)

I tried to create an array inside the function, but I didn't understand what to write in the body if you can help, I will be very grateful

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <locale.h>
void scan(int* , size_t );

void print(int* , size_t );


int get_max(int* , size_t );


int get_min(int* , size_t );


void invert(int*, size_t);


float get_average(int* , size_t);


void turn(int* , size_t );


int main()
    {
    setlocale(LC_ALL, "Russian");
    size_t n;
    printf("Введите размер массива: ");
    scanf("%llu", &n);
    int* bebra = (int*)calloc(n, sizeof(int));
    scan(bebra, n);
    print(bebra, n);
    int max = get_max(bebra, n);
    printf("Значение максимального элемента: %d\n", max);
    int min = get_min(bebra, n);
    printf("Значение минимального  элемента: %d\n", min);
    float avg = get_average(bebra, n);
    printf("Средне арифметическое значение:  %f\n", avg);
    invert(bebra, n);
    print(bebra, n);
    }

void scan(int* a, size_t n)
    {
    printf("Ввод массива: \n");
    for (size_t i = 0; i < n; i  )
        {
        printf("arr[%llu] = ", i);
        scanf("%d", &a[i]);
        }
    }


void print(int* a, size_t n)
    {
    printf("Печать массива: \n");
    for (size_t i = 0; i < n; i  )
        {
        printf("arr[%llu] = %d\n", i, a[i]);
        }
    }


int get_max(int* a, size_t n)
    {
    int max = INT_MIN;
    for (size_t i = 0; i < n; i  )
        {
        if (max < a[i])
            {
            max = a[i];
            }
        }
    return max;
    }


int get_min(int* a, size_t n)
    {
    int min = INT_MAX;
    for (size_t i = 0; i < n; i  )
        {
        if (min > a[i])
            {
            min = a[i];
            }
        }
    return min;
    }


void invert(int* a, size_t n)
    {
    for (size_t i = 0; i < n; i  )
        {
        a[i] = a[i] * -1;
        }
    }


float get_average(int* a, size_t n)
    {
    float sum = 0;
    for (size_t i = 0;i < n;i  )
        {
        sum = sum   a[i];
        }
    return sum / n;
    }

CodePudding user response:

I think this should work :)


void flip(int * array, size_t length)
{
        for (int i = 0; i < length / 2;   i) {
                int backwards_index = length - 1 - i;
                
                int array_at_index = array[i];
                array[i] = array[backwards_index];
                array[backwards_index] = array_at_index;
        }
}

The code swaps elements from the front and the back of the array, one by one. i will loop through half the values in array, rounded down (if there is a middle element, it doesn't matter if it's swapped with itself or not). If i iterated through the entire array, the elements would be swapped twice and the array wouldn't change.

backwards_index will be the index of the i'th element of the array from the back. Since the element in the array ranges from 0 to length - 1, inclusive, this element should have an index of length - 1 when i is 0 and count backwards (aka subtract i). Then, the values of the i'th element from the front and the back of the array are swapped.

Hope it helps!

CodePudding user response:

int *flip(int *arr, const size_t length)
{
    int *start = arr, *end = arr   length - (1 & (!!length));

    while(start < end)
    {
        int tmp = *start;
        *start   = *end;
        *end-- = tmp;
    }
    return arr;
}
  • Related