Description
Part One: (Static and automatic variables.)
- If you have not already done so, make a directory for your CS2401 stuff
mkdir cs2401
// I’m pretty sure that most of you have already done this
- Go into that directory
cd cs2401 - Once you are in there make a directory for your labs and inside that one a directory for lab4.
- Open a new file in your favorite editor, #include<iostream> and put in your
using - Type in this little function:
void pretty( ){
int x = 0;
x++;
for(int i = 0; i < x; ++i){ cout<<’*’;}
cout<<endl;
}
- And write a main that has a loop that calls your function six times.
- On your answer sheet write the output that you see after you have compiled and run this program.
- Now change the first line in the function pretty so it looks like this:
static int x = 0;
- Recompile and run this program.
- Write the output for this function on your answer sheet.
- What was the effect of using static? (Write on answer sheet.)
- If you remove static what is the effect? (Write on answer sheet.)
Part Two: (Dynamic Variables.)
- Start a new program – again starting out with the #include<iostream> and the
using namespace std; – this time you will only need a main - Declare a pointer capable of pointing at an int. (int * ptr; )
- Make the pointer point at a new integer. (ptr = new int; )
- Print out the address of the new integer. (On your answer sheet write how you did this as well as the address that shows up.) (cout << ptr <<endl; )
- Print out the address of that pointer. (On your answer sheet write how you did this as well as the address that shows up.) (cout << &ptr; )
- Now, using the * operator, set the value of your integer to 2401. (*ptr = 2401; )
- Write a loop with an integer counter that counts to 10 and in the body of the loop does this:
++(*ptr); cout<<*ptr<< “is stored at “ <<ptr<<endl; - On your answer sheet write down first and last line that appear here.
- Now change the body of your loop to this: {
++(ptr); // notice I took out the *
cout<<*ptr<< “is stored at “ <<ptr<<endl; }The difference here is that you were moving the pointer instead of changing the value being pointed at.
- On your answer sheet write down what the last two lines of your output looks like.
Part Three (A dynamic array)
- Now let’s make a dynamic array with the pointer that you have been using.
- Begin by declaring a couple of size_t variables for capacity and used.
- Initialize capacity to five and used to zero.
- Declare an extra int* tmpptr, which will use in resizing.
- Type this code:
ptr = new int[capacity];
for(size_t i=0; i<25; ++i){
ptr[used] = rand(); // you will need #include<cstdlib>
// at the top for this to work
cout<< ptr[used];
used++;
if(used == capacity){
cout<<”Sorry no room left.\n”;
break;
}
}
- Note how many numbers you see in the output.
- Now replace the cout and the break for a full array with a resizing which will do this:
- Set the tmpptr to a new integer array of capacity +5 size
- Copy the data from the original array to the new one using a loop or the copy function which we talked about in class
- Adjust the capacity variable so it accurately reflects the size of the new array
- Delete the original array, remembering to use the [ ]’s
- Assign ptr to tmpptr, so these pointers will both point at the newer array
- Do a cout<<”Resized\n”; to report that this was done.
- Now run your program – what do you see?
- Finally, comment highlighted code “cout<< ptr[used];” in question 15, and then at the end of this loop that has filled and output the array, put this
- tmpptr[2] = 0;
- Write a loop that will output all the numbers in the ptr array.
- What do you see?
- Submit the finished code for each of the three parts and your answer sheet to Blackboard, making sure that your name appears on both your code and the answer sheet.