Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
907 views
in Technique[技术] by (71.8m points)

c++ - Can we add an array variable to integer?

This is a program to find median of two sorted arrays.

A divide and conquer based efficient solution to find median of two sorted arrays of same size.

    #include<bits/stdc++.h>
    using namespace std;

    int median(int [], int); /* to get median of a sorted array */

    /* This function returns median of ar1[] and ar2[].
    Assumptions in this function:
    Both ar1[] and ar2[] are sorted arrays
    Both have n elements */
    int getMedian(int ar1[], int ar2[], int n)
    {
        /* return -1 for invalid input */
        if (n <= 0)
            return -1;
        if (n == 1)
            return (ar1[0] + ar2[0])/2;
        if (n == 2)
            return (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1])) / 2;

        int m1 = median(ar1, n); /* get the median of the first array */
        int m2 = median(ar2, n); /* get the median of the second array */

        /* If medians are equal then return either m1 or m2 */
        if (m1 == m2)
            return m1;

        /* if m1 < m2 then median must exist in ar1[m1....] and
            ar2[....m2] */
        if (m1 < m2)
        {
            if (n % 2 == 0)
            {

                return getMedian(ar1 + n/2 - 1, ar2, n - n/2 +1);

            }
            return getMedian(ar1 + n/2, ar2, n - n/2);
        }

        /* if m1 > m2 then median must exist in ar1[....m1] and
            ar2[m2...] */
        if (n % 2 == 0)
            return getMedian(ar2 + n/2 - 1, ar1, n - n/2 + 1);
        return getMedian(ar2 + n/2, ar1, n - n/2);
    }

    /* Function to get median of a sorted array */
    int median(int arr[], int n)
    {
        if (n%2 == 0)
            return (arr[n/2] + arr[n/2-1])/2;
        else
            return arr[n/2];
    }

    /* Driver program to test above function */
    int main()
    {
        int ar1[] = {1, 2, 3, 6};
        int ar2[] = {4, 6, 8, 10};
        int n1 = sizeof(ar1)/sizeof(ar1[0]);
        int n2 = sizeof(ar2)/sizeof(ar2[0]);
        if (n1 == n2)
            printf("Median is %d", getMedian(ar1, ar2, n1));
        else
            printf("Doesn't work for arrays of unequal size");
        return 0;
    }

My question is how can I add a array variable to an integer. I means whether it is referring to memory when we do like this (ar1 + n/2 - 1) here in the getmedian function call ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In expressions an array designator is implicitly converted to the pointer to its first element. Adding an integer to a pointer you will get again a pointer. It is so called the pointer arithmetic.

Thus for example if you have an array

int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const size_t N = sizeof( a ) / sizeof( *a );

then expression

a + N / 2 

will point to the sixth element of the array.

Here is a demonstrative program

#include <stdio.h>

int main(void) 
{
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    const size_t N = sizeof( a ) / sizeof( *a );

    for ( size_t i = 0; i < N; i++ ) printf( "%p: %d
", a + i, *( a + i ) );

    return 0;
}

Its output might look like

0xbfd3d9c8: 0
0xbfd3d9cc: 1
0xbfd3d9d0: 2
0xbfd3d9d4: 3
0xbfd3d9d8: 4
0xbfd3d9dc: 5
0xbfd3d9e0: 6
0xbfd3d9e4: 7
0xbfd3d9e8: 8
0xbfd3d9ec: 9

Also a function parameter declared like an array is also adjusted to pointer. So for example these function declarations

int median(int a[100], int n); 
int median(int a[10], int n); 
int median(int a[], int n);
int median(int *a, int n);  

are equivalent and declare the same one function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...