Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Bool search (int a [], int n, int key, int *count); that is passed an integer ar

ID: 3691479 • Letter: B

Question

Bool search (int a [], int n, int key, int *count); that is passed an integer array a of size n and a value key to search for in a, and returns true if the value is found, false otherwise, plus the number of times the key is found back through the pointer count You may not use any array subscripting in the function search! Here is a driver program you may use, if you choose, to test your function. It produces a warning message (from the printf in main) when compiled that you can safely ignore. #include #include #define N 10 bool search (int a [], int n, int key, int *count); int main(voit) int I, key, count; int list [N] = {2,4,6,8,2,2,0,31, -6,0}; print ("Enter key to search for: "): scanf ("%d",$key); printf(search(list ,N,key, &count;)?"key is found %d time (s) ",count);

Explanation / Answer

#include<stdio.h>
#include<stdbool.h>

#define N 10

bool search(int a[], int n, int key, int *count);

int main(void){
  
   int i, key, count = 0;
   int list[N] = {2,4,6,8,2,2,0,31,-6,0};
  
   printf("Enter key to seach for: ");
   scanf("%d", &key);
   bool flag = search(list, N, key, &count);
   printf(flag?"key is found %d time(s) ":
                                       "key is not found ",count);
  
   return 0;
}

bool search(int a[], int n, int key, int *count){
  
   int i;
   for(i=0; i<n; i++){
       if(a[i] == key)
           (*count)++;
   }
  
   if(*count == 0)
       return false;
   return true;
}

/*

Sample run:

Enter key to seach for: 2
key is found 3 time(s)

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote