-4
// RecursiveBinarySearch.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define N 9

int RecursiveBinarySearch(int A, int low, int high, int x);

int main()
{
    int A[N];
    int index = 0;
    //Put 
    A[0] = 2;
    A[1] = 6;
    A[2] = 13;
    A[3] = 21;
    A[4] = 36;
    A[5] = 47;
    A[6] = 63;
    A[7] = 81;
    A[8] = 97;

    printf("Elements in Array A\n");

    while (index <= 8) {
        printf("%d ", A[index]);
        index++;
    }

    printf("\nLocation(index) of element 63\n");

    printf("%d", RecursiveBinarySearch(A, 0, 8, 63));

    return 0;
}


int RecursiveBinarySearch(int A, int low, int high, int x) {

    //Base Condition
    if (low > high)
        return -1;

    int mid = low + (high - low) / 2;

    if (x == A[mid])
        return mid;
    else if (x < A[mid])
        return RecursiveBinarySearch(A, low, mid - 1, x);
    else
        return RecursiveBinarySearch(A, mid + 1, high, x);

}

Here's first problem. Visual studio says int A[9] argument of type "int*" is incompatible with parameter of type "int"

Here's second problem. int mid expression must have pointer-to-object type

I don't know well about pointer so i want to know why this code can't be compiled and how to use pointer in this code.

윤덕준
  • 1
  • 1
  • 2
    Please take the time to read some good book on C. An array (`int []`) is not the same as a single `int`, so what did you expect when passing an array where your function expects just a value? –  Oct 05 '17 at 09:40
  • 2
    Change the declaration of argument A to `int *A` – Tom Karzes Oct 05 '17 at 09:43
  • (With error messages including line numbers, please include that number in your quote. When referring to items from code included in your post, it is easier to read if you make them stand out using "backticks": `int A[9]`, `int mid`v) – greybeard Oct 05 '17 at 11:25

2 Answers2

2

Better remove all assignements A[0] = ..., A[1] = ... alltogether and write:

int A[] = {2,6,13,21,36,47,63,81,97}

And replace

while (index <= 8)

by:

while (index < sizeof(A)/sizeof(A[0]))

sizeof(A) / sizeof(A[0]) is the number of elements if the array A. sizeof(A) is the size in bytes of the whole array, and sizeof(A[0]) is the size of one elements of the array in bytes.


But the real problem is here:

Replace:

int RecursiveBinarySearch(int A, int low, int high, int x)

by

int RecursiveBinarySearch(int A[], int low, int high, int x)

There may be more errors though.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
1

Start taking compiler warnings seriously:

helpPointer.c: In function ‘main’:
helpPointer.c:30:40: warning: passing argument 1 of ‘RecursiveBinarySearch’ makes integer from pointer without a cast [-Wint-conversion]
     printf("%d", RecursiveBinarySearch(A, 0, 8, 63));
                                        ^
helpPointer.c:4:5: note: expected ‘int’ but argument is of type ‘int *’
 int RecursiveBinarySearch(int A, int low, int high, int x);
     ^~~~~~~~~~~~~~~~~~~~~

As pointed by people in comments already, you're passing an array into recursive binary search method, so you should change RecursiveBinarySearch like this:

int RecursiveBinarySearch(int A[], int low, int high, int x);

Or

int RecursiveBinarySearch(int *A, int low, int high, int x);

Which are one and the same thing, since array name is just a pointer which points to the first element of the array. Read this if you don't have much idea about relationship between array and pointers.

Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40