Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Programming This is a practice about pointer and binary search. It asks me to write two functions. int binary_search (int * a, int *

C Programming

This is a practice about pointer and binary search. It asks me to write two functions.

int binary_search (int * a, int * endPtr, int key, int ** findPtr) Returns 1 if there is a key in m 1-dimensional arrays, 0 otherwise. It stores the address of the found value in findPtr. int all_binary_search (int a [] [100], int n, int m, int key, int ** findPtr) Returns 1 if key exists in a two-dimensional array of n rows and m columns, and 0 otherwise.

I don't know how to use the parameter findPtr. Please help me, THANK YOU!

Here is my code.

#include

void swap(int *a, int *b)/*{{{*/ { int temp; temp = *a; *a = *b; *b = temp; } /*}}}*/ void sort(int *a, int m)/*{{{*/ { int i, j; for (i = 0; i < m - 1; i++) for (j = 0; j < m - 1 - i; j++) if (*(a + j) > *(a + j + 1)) swap(a + j, a + j + 1); } /*}}}*/ void all_sort(int a[][100], int n, int m)/*{{{*/ { int i, j; for (i = 0; i < n; ++i) { int *temp[m]; for (j = 0; j < m; ++j) temp[j] = &a[i][j];

sort(*temp, m); } } /*}}}*/ int binary_search(int *a, int *endPtr, int key, int **findPtr)/*{{{*/ { int start, end, mid; start = 0; end = (endPtr - &a[0]);

while (start <= end) { mid = start + (end - start) / 2; if (a[mid] < key) start = mid + 1; else if (a[mid] > key) end = mid - 1; else //key found { return 1; } } return 0; } /*}}}*/ int all_binary_search(int a[][100], int n, int m, int key, int **findPtr)/*{{{*/ { int i, j; for (i = 0; i < n; ++i) { int temp[m]; for (j = 0; j < m; ++j) temp[j] = a[i][j]; if (binary_search(temp, temp + m, key, findPtr) == 1) //key found return 1; } return 0; } /*}}}*/

int main()/*{{{*/ { int N, M, K, i, j, a[100][100]; scanf("%d %d %d", &N, &M, &K); for (i = 0; i < N; ++i) for (j = 0; j < M; ++j) scanf("%d", &a[i][j]);

all_sort(a, N, M);

if (all_binary_search(a, N, M, K, a) == 1) { printf("1 "); // found } else { printf("0 "); // not found }

return 0; }/*}}}*/

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 11G SQL

Authors: Joan Casteel

2nd Edition

1133947360, 978-1133947363

More Books

Students also viewed these Databases questions

Question

A 30 year old person should know better.

Answered: 1 week ago

Question

Our service is (good) than theirs.

Answered: 1 week ago