Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following is in C: Given the code below, please answer the questions (below the code)-- I know there is a lot of code, but

The following is in C:

Given the code below, please answer the questions (below the code)-- I know there is a lot of code, but there are only 4 relevant questions about the code.

image text in transcribed

}

35

36

image text in transcribed

a) The first call to gfind_max is valid, and produces the expected result, however, the following three calls are invalid, producing 0xfffff, 0x44 (or 'D'), and 0x90000 respectively. I understand why 0xfffff is produced, but not the latter two, how does the char comparison reach 0x44 (my initial thought was overflow? But I was unable to decide which value it ultimately caster to be the max char in the first place). The last one compares them as char*s, (I assume to compare the pointers, but I don't know why you would want to compare them since it's just comparing the addresses I assume), why are the last two calls invalid, and why are they producing these results?

b) If you run the code as is--it works. but if you change the code to use cmp_first_char for both sort and search, when will your code crash (and why?)?

(c and d see image below)

image text in transcribed

1 #include 2 #include 4 int cmp_ptr(const void *p, const void q 6 return (const char *)p (const char )q; 9 int cmp_int(const void *p, const void *q) 10 { 11 return *(const int )p*(const int *)q; 12 > 13 14 int cmp_char(const void *p, const void *q) 15 f 16 return *(const unsigned char *)p-*(const unsigned char)q; 17 18 19 int cmp_first_char(const void *p, const void *q) 20 21 return*(const char)p(const char**)q 23 24 int p_a symmetric(const void *p, const void *q) 25 { 26 return *(const char )p**(const char)q; 27 28 29 void test bsearch(chararrl,int n) 30 char ch = 'z'; 32 qsort (arr, n, sizeof (arr[0]), cmp_first_char) 33 char **found bsearch(&ch, arr, n, sizeof(arr[0]), cmp_asymmetric) 34 printf(" %c matches? %s ", ch, found ? *found : "none"); 1 #include 2 #include 4 int cmp_ptr(const void *p, const void q 6 return (const char *)p (const char )q; 9 int cmp_int(const void *p, const void *q) 10 { 11 return *(const int )p*(const int *)q; 12 > 13 14 int cmp_char(const void *p, const void *q) 15 f 16 return *(const unsigned char *)p-*(const unsigned char)q; 17 18 19 int cmp_first_char(const void *p, const void *q) 20 21 return*(const char)p(const char**)q 23 24 int p_a symmetric(const void *p, const void *q) 25 { 26 return *(const char )p**(const char)q; 27 28 29 void test bsearch(chararrl,int n) 30 char ch = 'z'; 32 qsort (arr, n, sizeof (arr[0]), cmp_first_char) 33 char **found bsearch(&ch, arr, n, sizeof(arr[0]), cmp_asymmetric) 34 printf(" %c matches? %s ", ch, found ? *found : "none")

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

Students also viewed these Databases questions

Question

Defensiveness and how do they deal with it?

Answered: 1 week ago