7. Complete this program, using functions and dynamic memory allocation. The pro
ID: 3818375 • Letter: 7
Question
7. Complete this program, using functions and dynamic memory allocation. The program will create a “negative” of the image in the original PGM image (providing that your readPGM and writePGM functions work properly!). Be sure to delete [ ] the array before exiting.
Question: How is it that the function negatePGM(...) can change the actual pixel array, even though the pointer pix is passed by value?
To test your program, I recommend testing initially with a small PGM, such as your initials.pgm from the beginning of the semester. When you have verified that it is working, try it on some of the images from the image archive, available on the course materials web page.
PROGRAM:
Name your program pgm_negate.cpp
Explanation / Answer
An array passed to a function is converted to a pointer. When you pass a pointer as argument to a function, you simply give the address of the variable in the memory. So when you modify the value of the cell of the array, you edit the value under the address given to the function.
When you pass a simple integer to a function, the integer is copied in the stack, when you modify the integer within the function, you modify the copy of the integer, not the original.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.