Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3. The subparts to this problem involve errors in the use of pointers. (a) This program is supposed to write 50 60 70. one per

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

3. The subparts to this problem involve errors in the use of pointers. (a) This program is supposed to write 50 60 70. one per line. Find all of the bugs and show a fixed version of the program: #include int main(void) *P int MyArray [3] = { 10, 20, 30 }; int *P = MyArray: *P 70; // set MyArray [0] to 70 += 1; 60; // set MyArray [1] to 60 P += 2: 50; // set MyArray [2] to 50 do { P-- printf ("%d ", *p); // print values } while (p >= MyArray); P[0] (b) The findmin junction is supposed to find the minimum item in nu army and set the proMin parameter to point to that items that the caller knows that it's location. Explain why this function won't do that, and wiow low to fix it. Your fix must be to the function only vollst not change the main 1:50 PM (b) The findmin function is supposed to find the minimum item in an array and set the proMin parameter to point to that item so that the caller knows that item's location. Explain why this function won't do that, and show how to fix it. Your fix must be to the function only, you must not change the main routine below in any way, yet as a result of your fixing the function, the main routine below must work correctly void findmin(int MyArray [], int n, int* pToMin) { if n int* ncubed) void computeCube (int n, { *ncubed n } * n int main (void) int ptr: computeCube (5. ptr): printf("Five cubed is %d "*ptr (d) The strequal function is supposed to return true if and only if its two strius arguments have exactly sale text. What are the problems with the implementation of the function and how can they be fixed! #include W return true if two C strings are equal bool strequal (const char stri[l, congt char str2 { while (stri != 0 && str2 = 0) if (stri != str2) // compare corresponding characters return false; stri++; // advance to the next character str2++ return str1 == *str2 // both ended at same time? int main(void) char a [15] = "Chen B."; char b [15] = "Chen, Y.J."; if (strequal(a, b)) printf("They're the same person! "); } (e) This program is supposed to write 5 4 3 2 1, but it probably does not. What is the problem with this program? (We're not asking you to propose a fix to the problem.) int* getPtrToArray (int& m) int anArray [5] = { 5, 4, 3, 2, 1 } m = 5: return anArray: 1 void f int junk [100]: for (int i = 0; k Now rewrite the function shown bu part (b) so that it uses teitlier spart brackets nor any integer variables. Your new fraction IESE NJE use any local variables other than the parameters 6. What does the followme programi print and wly Be sure to explain wly each line of output primits the why it does to BOL dl credit #include a intb int maxell (int 1 if (a > *b returna 6. What does the following program print and why? Be sure to explain why each line of output prints the why it does to get full credit. #include int* maxwell (int *a, int* b) 1 if (* > *b) return else return b 0 void swapi (int* a, int* b) int* tempa b = temp } void (inti a int* b int temp = *a a = b *b = tempi int main(void) int array [6] = 5, 3, 4. 17. 22, 19) int. ptr = mwell (array array [2] ): ptr = -1 per 2 int main(void) ptr [1] int array [6] = { 5, 3, 4, 17, 22, 19 }; int* ptr = maxwell (array, &array[2]); *ptr = -1; ptr += 2; = 9; *(array+1) = 79; printf("%d ", karray [5] ptr); swapi (karray [0], karray [1]); swap2 (array, &array [2]); for (int i = 0; i

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2019 Wurzburg Germany September 16 20 2019 Proceedings Part 2 Lnai 11907

Authors: Ulf Brefeld ,Elisa Fromont ,Andreas Hotho ,Arno Knobbe ,Marloes Maathuis ,Celine Robardet

1st Edition

3030461467, 978-3030461461

More Books

Students also viewed these Databases questions