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 lab3.
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 use the word auto instead of 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 110 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. Now change the loop so that it counts to 1,000,000, and have the ptr move backwards by changing the ++(ptr) to a –(ptr). Compile and run this.
11. On your answer sheet write down what the last two lines of your output looks like.
12. It’s not always so easy to tell exactly where the crash is happening, and this is where a debugger can be very useful. Let’s run the debugger on the program you just did:
a. g++ -g parttwo.cc
b. gdb a.out
c. run
d. When the program crashes ask it
e. where
13. On your answer sheet write down what gdb just told you.
14. Sometimes it’s good to single-step up through your program. Try this for the first few iterations of the loop. You can always quit by hitting q.
a. gdb a.out // note that you don’t have to recompile here since we’re not changing anything
b. break main // you could also have set up your break point right above the loop by doing break parttwo.cc:12 — or whatever line number is right above your loop
c. run
d. Now single-step through your program by hitting
n
e. If you want to see the value of the pointer you can always “peek” at the variable:
p variable_name
15. Submit the finished code for each of the two parts and your answer sheet to Blackboard, making sure that your name appears on both your code and the answer sheet.
3, Assignment, CS2401, Lab
[SOLVED] Cs2401 lab assignment 3
$25
File Name: Cs2401_lab_assignment_3.zip
File Size: 216.66 KB
Only logged in customers who have purchased this product may leave a review.
Reviews
There are no reviews yet.