Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment you will walk through a series of steps and do some more exploration of pointers. You will have to write a program

In this assignment you will walk through a series of steps and do some more exploration of pointers. You will have to write a program but instead of having 1 objective you will write code snippets (much like the fun.cxx in fun with pointers we went through in-class) to figure out each part. Along the way there will be questions that you must answer (plus a few optional extra credit ones). You must turn in your program, with informative COUTS and comments as to what part of the code corresponds to which part of the assignment, and an additional page or pages (in digitial form) that answer the questions that are asked. If you dont do this you will not receive any credit for the assignment. This lab uses Mug you can get a copy of Mug.h and Mug.cpp here

https://drive.google.com/drive/folders/1Jr3QmSoZlOGCHdWwSGE91tisA1x8Rwax To summarize, the two things you will turn in Code commented that correspond to each part of the pointers lab assignment A document with your name and your partners name (if you had one) along with any questions you had to answer as you went through the lab

Part 1 Wont you my neighbor? : Determining if two variables are next to each other in memory

Task 1: In code create and initialize an 2 ints, a double, a char, and a mug (in that order)

Task 2: In code Create 5 pointers to the 5 variables created in Task 1

Task 3: In code Print out the 5 memory locations of your variables. For the char* type you have to cast it (otherwise itll think its a C-stringmore on this later) For example if you have a char* k that holds the memory address of a char variable, you would cout it by saying static_cast(k).

Task 4: In code Print out the size of each variable type: int, double, char, and mug, by using the sizeof(). For example, if I have an int x, I can print out sizeof(x) and it will give me the number of bytes in memory x takes up. THIS MIGHT BE DIFFERENT FROM COMPUTER TO COMPUTER.

Task 4b: In the document answer the following: What is the type (from the ones listed above) with the minimum and maximum number of bytes?

Task 4c: In the document look at the Mug.h file and come up with an argument for what the lowest bound should be on the Mug memory (i.e. the mug should be at least ____ big because _______________________). Is the Mug actually that big or is it bigger or smaller? For a hint read this article.

Task 5: In the document, use the pointer values and a hexadecimal calculator (you can find one online) to determine if any of the variables are next to another variable in memory. For example, if I wanted to see if the two ints were next to each other in memory, and I knew that the size of an int was 4 bytes from Task 4, and the memory location of my first int was 0x447 and the memory location of my second int was 0x555 I could use the online hexadecimal calculator to see how far apart these two integers were. In this case it gives me the answer 10e bytes apart. If we convert this to a decimal value by using this online converter we get that these two variables are 270 bytes apart! Definitely not neighbors. However, if the first int was at memory location 0x447 and the second int was stored at memory location 0x443 then I would find that these two integers are in fact neighbors (i.e. they are stored next to each other).

Extra Credit 1 (you will only get extra credit if you finished the whole assignment): Write a function that takes in 2 memory locations of pointers of the same type, and returns the number of objects that can fit in between these two locations. Read this for a hint

Part 2 The pointers of arrays: Investigating the relationship between arrays and pointers in C++ As weve talked about, arrays are stored in contiguous memory. That means each element of an array is neighbored by another element of the array.

Task 1a: In code Create an integer array of 5 elements and use a for loop to initialize these array elements to anything you want. Then cout the array (so if my array name is p, Ill cout<

Task 1b: In the document fill in the blanks of the following sentence The array name is a ______ to the _______ element

Task 1c: In the document using what you found in 1b, give a reason why we dont have to explicitly pass an array by reference to a function.

Task 2: In the document, using the the information from Part 1 Task 4a How big would expect this array to be in memory?

Task 3: In In code use the sizeof function on the array to cout the size of the array

Part 3 Brackets and stars: Iterating through an array using a pointer addition We know that we can use the * operator to dereference a pointer and get the value inside the memory location held by the pointer. The [] we use in arrays is just another way to dereference. In fact, if we have int array[5]; then we can access the first element (zeroth index) via array[0], but we can also say *array. Similarly array[1] can be written *(array+1). Task 1: In code create a for loop that prints out all the contents of an array without using []

Task 2: In code: create an array of 5 doubles and redo task 1 (i.e. double arrayD[5];).

Task 3: In the document explain the differences between Task 1 and Task 2. Meaning what is the difference between *(array+1) and *(arrayD+1). Is it the 1 adding the same amount in memory? If not what is the amount its adding for each case? Part 4 Pointing to the future: Wrapping up the pointers lab Task 1: In code create a pointer to a pointer (int **p that points to another integer pointer, int*q which points to some integer int x (so q holds xs memory location and **p holds qs memory location).

Task 2: In the document Draw a picture of p and q and x in memory Task 3: In code: use p (and only p) to add one to xs value. hint: parenthesis are your friends. Extra credit 2a: In code Write some code to help you figure out the size of an int pointer and a double pointer. Extra Credit 2b: In the document writeWhat did you find out about their relative sizes? Does this make sense, why or why not.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions