CS2401 Homework # 2

$30.00

Category: Tags: , , , You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (6 votes)

Part One: (Static and automatic variables.)
1. 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
2. Go into that directory
cd cs2401
3. Once you are in there make a directory for your labs and inside that one a directory for lab4.
4. Open a new file in your favorite editor, #include and put in your
using statement.
5. Type in this little function:
void pretty( ){
int x = 0;
x++;
for(int i = 0; i < x; ++i){ cout<<’*’;}
cout<<endl;
}
6. And write a main that has a loop that calls your function six times.
7. On your answer sheet write the output that you see after you have compiled and run this program.
8. Now change the first line in the function pretty so it looks like this:
static int x = 0;
9. Recompile and run this program.
10. Write the output for this function on your answer sheet.
11. What was the effect of using static? (Write on answer sheet.)
12. If you remove static what is the effect? (Write on answer sheet.)
Part Two: (Dynamic Variables.)
1. Start a new program – again starting out with the #include and the
using namespace std; – this time you will only need a main
2. Declare a pointer capable of pointing at an int. (int * ptr; )
3. Make the pointer point at a new integer. (ptr = new int; )
4. 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; )
5. 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; )
6. Now, using the * operator, set the value of your integer to 2401. (*ptr = 2401; )
7. 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;
8. On your answer sheet write down first and last line that appear here.
9. 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.
10. On your answer sheet write down what the last two lines of your output looks like.
Part Three (A dynamic array)
11. Now let’s make a dynamic array with the pointer that you have been using.
12. Begin by declaring a couple of size_t variables for capacity and used.
13. Initialize capacity to five and used to zero.
14. Declare an extra int* tmpptr, which will use in resizing.
15. Type this code:
ptr = new int[capacity];
for(size_t i=0; i<25; ++i){
ptr[used] = rand(); // you will need #include
// at the top for this to work
cout<< ptr[used];
used++;
if(used == capacity){
cout<<”Sorry no room left.\n”;
break;
}

}
16. Note how many numbers you see in the output.
17. Now replace the cout and the break for a full array with a resizing which will do this:
a. Set the tmpptr to a new integer array of capacity +5 size
b. Copy the data from the original array to the new one using a loop or the copy function which we talked about in class
c. Adjust the capacity variable so it accurately reflects the size of the new array
d. Delete the original array, remembering to use the [ ]’s
e. Assign ptr to tmpptr, so these pointers will both point at the newer array
f. Do a cout<<”Resized\n”; to report that this was done.
18. Now run your program – what do you see?
19. 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
a. tmpptr[2] = 0;
b. Write a loop that will output all the numbers in the ptr array.
20. What do you see?
21. 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.