Starting from:
$30

$24

Lab 7 Solution

Get checked off for up to 3 points of incomplete work from the previous lab within the first 10 minutes of the lab.

In this lab, you can form a group of 2-3 individuals. ​You must be checked off together as a group at the end of the lab.​Although you perform tasks as a group, ensure that you understand the work and ask questions to TAs as needed.


Design(3 pts), Code (4 pts)

To help you practice functions, you will write a short program that asks the user to enter a string

(​get_string()​), then makes a copy of the string, and takes the copy and changes all non-space letters to dashes (​set_replace_string()​). The program then gets a letter from the user to search in the original string and replace the dashes in the copy string with the letter found, returning the number of letters found (​get_search_replace()​).

You should design first. You can have more functions, but you must have at least the three below with the EXACT function prototypes​. Each of your functions should have the proper function headers/descriptions

void get_string(string *);

void set_replace_string(string, string *);

int get_search_replace(char, string, string &);

Write the function headers/descriptions for each of the functions above, as well as all the functions you create. This includes information about parameters, return values, and pre/post conditions.

Design ​– Give as much detail as possible for the main function and all the functions above.
    • ​Why do the function prototypes have the specific parameter types on them? •​ ​Why do the functions have void or a return value?

    • ​How do all the functions interact together?
Testing ​– Provide testing values with expected results.
    • ​What do you plan to use as bad values? Good values?

    • ​What do you expect to happen with bad values? Good values?


Get checked off by a TA before beginning to implement. This will help with logic and function mistakes.

Now, incrementally write your functions and program.





1
Understand Pointers/Dynamic Memory(3 pts)


Write 3 different functions in C++ to create memory on the heap ​without causing a memory leak. Hint: You will need to assign the address to a pointer that was created outside the function. Remember, you can return an address or change what a pointer points to (the contents of a pointer) in a function by passing the pointer by reference or by passing the address of the pointer.

What if you want to change the contents of what the pointer points to? ​Make a function that will set the contents of the space on the heap​. Do you still need to pass by reference or the address of the pointer? Why or why not?

How will you delete the memory off the heap? Try doing it outside a function, now inside a function. Make sure your delete function is setting your pointer back to NULL​, since it is not supposed to be pointing anywhere anymore.

You can check to see if you have any memory leaks using valgrind.

%valgrind program_exe

Show your completed work and answers to the TAs for credit. You will not get points if you do not get checked off.




































2

More products