Question
Part I: basics of Pointers 1. Compile and run this program. Working together with your lab partner, determine why it gives the results it does,
Part I: basics of Pointers
1. Compile and run this program. Working together with your lab partner, determine why it gives the results it does, and answer the questions posed in the output. Make a text file with an explanation of what each section is demonstrating, with answers to all the questions, and submit that as part of your lab work. It may help to put the code next to the output so you can study the output and make sense of what the code is doing. You may also find it helpful to use the debugger and single-step through the program, examining the output after each statement.
Name your file pointers_questions.txt (or pointers_questions.docx)
Part 2: Write some code using pointers
Heres the basic program for next few parts of this lab:
#include
using namespace std;
int main()
{
int i = 5, j=51, k=62;
int data[5] = {10, 20, 30, 40, 50};
char my_cstring[8] = "the fox";
int *p = NULL;
char *pc = NULL;
// do something useful with the arrays and the pointers
// . . .
// and put some clean-up code to delete any // dynamically allocated variables
cout << "done"<
#ifdef WIN32
system("pause");
#endif
return 0;
}
A side note on: #ifdef ... #endif:
These lines create a conditional compile. Visual Studio defines a precompiler constant called WIN32, while other compilers, like the Gnu C++ compiler, do not. Thus, the line system(pause); is compiled in Visual Studio, but left out of a gnu compile. This line lets you run with the debugger in VS and pause your program before exiting, so the window doesnt disappear before you can read the output.
Using a pointer to access data in memory:
2. Starting from the basic program:
assign p the address of i, and output the value where it points.
move it to point to j and k and output those values.
point it to the first element of the array data and output it
point it to the 3rd element of data and output it.
using your pointer variable, change the values of the i and the 1st and 3rd elements of data.
Compare your program with your partners, and make sure you all are understanding how things work.
3. Starting from the previous (part 2) program:
initialize pc to the beginning of my_cstring.
use it to capitalize the 1st and 5th characters of my_cstring. (hint: toupper(c) returns the upper-case equivalent of c)
NOTE:
Either of the following will point to the 1st char:
i. pc = my_cstring; // using name of array
ii. pc = &my_cstring[0]; // or taking address of 1st element
You could use either of the following to point to the 5th element:
i. pc = my_cstring+4; // 4 elements over from beginning of array
ii. pc = &my_cstring[4]; // or taking address of 5th element.
(you'll obviously want to output those values after you change them...)
use your char pointer to output the integer value of each element of my_cstring (including the 8th element, my_cstring[7]). (hint: you could copy each element to an integer and output it, or use static_cast(...) ).
NOTE:
The following might be helpful. (hmmm. why does "cout << *pc" print 1 char, but cout << pc more than 1 char?):
i. pc = my_cstring; cout << pc << endl; cout <<*pc << " "<< pc[0] << " " <(*pc)<
Name your program basic_pointers.cpp
Part II. Pointers and Arrays
4. Pointers and arrays: Now, starting from the (original) basic program
create a pointer, initialize it to the beginning of the array data
output the elements of data by indexing the array data[ ] using a loop (and the indexing operator []).
output the elements of data by indexing your pointer using a loop, with the pointer pointed at the beginning of the array and using [ ]
output the elements of data by incrementing the pointer each time through the loop. your loop control could look like this for (i=0; i < 5; i++) { p++; } or like this (why does this work?): for (p=data; p < data+5; p++)
reset your pointer to point at the beginning of the array again, then output the elements of data by adding i, a loop counter, to the pointer and dereferencing the summed address. Use something like: cout << *(p+i) (Be sure to point back at the beginning of the array before the loop!)
Q: how is cout << *(p+i) different from cout << *p+i ? If you're not sure, try both and see what happens. To verify your answer, look at the operator precedence table in the textbook and find the precedence of * (for dereferencing) and +. Which is higher precedence?
Name your program pointers_arrays.cpp
Part III: Dynamic memory allocation, and passing pointers to functions
5. Dynamic memory allocation:
C++ let's you request memory from the OS using the new operator, and use it like individual variables or arrays. Using it for single variables is more important for object-oriented programming and data structures (courses such as CS 1412 and CS 2413), but we'll do it here with ints and floats. Using it for dynamically allocated arrays is VERY useful, however, so be sure you understand this part of the lab when you're done!
Here is a sample program which allocates a double and uses it, then allocates a block of 10 doubles and uses them as an array. It also demonstrates how to pass pointers by value and by reference to functions. Compile and run it, discuss it with your partner, and make sure you understand how it works.
Write a program which:
dynamically allocates an array of 5 integers
uses a loop to set the elements to 5, 10, 15, 20 and 25
uses a second loop to output the elements of the array.
be sure you use a second loop for this!
deletes the array.
Compare programs with your partners, make sure youre all getting it, and add the following:
Move the array-printing to a function print_array
Write a function create_array with the following prototype: void create_array(int * &p, int size, int initval);
this function should dynamically allocate an array and set all elements to initval. Note: you'll need to use a loop to set all the values.
use both these functions in your program. Have your program create a dynamically allocated array of 10 elements and initialize it to all zeros (and print it out), then have it create another dynamically allocated array of 20 elements, and initialize it to all 999's (and then print it).
Name your program dynamic_arrays.cpp
6. Here are 4 files: numbers1.txt , numbers2.txt and numbers3.txt and numbers4.txt . The first two contains a "header" line telling you how many data items follow. (The first one is very small, so you can easily verify you are getting the right results; the second is larger, too large to verify by hand.) The second pair are like the first two, except they are just a list of numbers (no "header", i.e., no count at the top).
a.Write a program that can calculate the average, min and max of the numbers in the first 2 files. It should read the first value (# of points), allocate an array the correct size, read the data into that array, then analyze the array and print the count, average, min & max of the array.
a.Use a function to read the data, a second function to find the average, and a third function to find the min & max. Read the count and allocate the array itself in the (...) routine (...) routine will have to pass back both the count and the pointer after it allocates the array.
b.I recommend that you print out the first few elements and the last few elements of the data file when you read them in to make sure you get all the values. Some print statements in the various routines would also be helpful while writing/testing to make sure things are working properly.
c.Make sure you delete the dynamic array properly and set your pointer back to NULL.
Modify your program to work with an array of filenames, and read and print the statistics for each file in the list. You can use an array of strings for the filenames: string filenames[] = {"numbers1.txt", "numbers2.txt"};
c.Write a modified version of the program from part a. that can work with the files numbers3.txt and The only difference is that the file doesn't tell you the size upfront. Your program will have to read the file twice once to count how many values there are, and then (after allocating an array), read in the data and get the min, max and average.
a.Use a function (filename) that reads the file and counts the number of values.
b.Note: if you're not careful, you can end up reading and counting the last element twice! Make sure you don't do that...remember, is your friend here...
You can use an array of strings for the filenames: string filenames[] = {"numbers3.txt", "numbers4.txt"};
Name your programs read_nums.cpp (part b) and read_nums_nh.cpp (part c)
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.
Name your program pgm_negate.cpp
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started