Breakout Lab 05, CSCI 1730 More Pointer Activities Learning Objectives ˆ Understand and use pointers and references ˆ Discover the relationship between arrays and pointers ˆ Work with functions,...

1 answer below »
I had a lab assignment attached the file


Breakout Lab 05, CSCI 1730 More Pointer Activities Learning Objectives ˆ Understand and use pointers and references ˆ Discover the relationship between arrays and pointers ˆ Work with functions, pointers, and const pointers Problem / Exercise Pointers are an integral part of C and C++. While they may be the cause of many headaches, they allow for a great deal of control and flexibility. Because they are low-level constructs, they require a solid understanding of the memory model behind it all. Not every language has pointers, but what you learn about memory management will apply to most programming languages. This lab is structured differently from previous code writing labs. You’ll be asked to observe some code and answer questions on what their output is. Also, you will fill out memory maps (as needed) and tables. Submission Before the date and time stated on eLC, you need to submit answers to all activities (which include answering all questions and filling out all tables) in a single .pdf file, named lab05.pdf. At the top of this document, write your first and last name as they appear on eLC. You may use Microsoft Word installed on the computers in the lab (or in the libraries on campus) to answer the questions for this lab assignment, but export your final answers to this assignment as a single .pdf file, named lab05.pdf, and then submit your lab05.pdf file on odin. Your lab05.pdf file must be neat and organized (otherwise, points might be deducted). You may use scp or a sftp program to upload your file to odin. If you do not know how to upload a file to odin, then you are required to ask a lab instructor during your lab period this week. You must number and label all of your answers in the same way as questions are numbered and labeled in this lab, and you should create and fill in tables in the same way they are shown in the lab. If you cannot upload your file to odin, then you’ll need to email it to your lab instructor(s) before the deadline; however, your grade will be reduced by one point for failing to properly submit the document on odin. Make sure your work is on odin.cs.uga.edu in a directory called LastName-FirstName-lab05. This directory must include all of the following. 1. A single lab05.pdf file containing all of your answers to the activities in this lab assignment along with your name and the name(s) of your group members. To submit from within the parent directory, execute the following command: $ submit LastName-FirstName-lab05 csci-1730 Grading (10 points) Students are expected to do all activities, answer all questions, and fill in all tables for this lab. We will choose a subset of things to grade for this lab (you will not be told which parts we are grading since you must complete all parts). Furthermore, neatness and organization is expected (otherwise, we may deduct points). The following grading will be used. Correctly answered a subset of things we are grading 10 points Not submitting to csci-1730 -1 point Late penalty for submitting 0 hours to 24 hours late -2 points Penalty for not following instructions (disorganization, etc.) Penalty decided by grader 1 1 Functions, Pointers, and Tricky Declarations Activity In this part, you will get to play with pointers as function parameters and practice how to simulate pass-by-reference by passing pointers by value. Also, you will become familiar with complicated mixed representations of pointers, arrays, and functions. 1. What is the output of the following code (assume proper includes, usings, etc)? Why? Which variable in the code is “passed-by-reference?” void divBy2(int & n) { n = n / 2; } // divBy2 void divBy2(int * np) { *np = *np / 2; } // divBy2 int main() { int x = 44; divBy2(x); cout < x="">< endl;="" divby2(&x);="" line="" 16="" cout="">< x="">< endl; return exit_success; } // main 2. in the code presented in the previous question, why is &x used for the function parameter on line 16? 3. complete the following table by providing plain english meanings to the corresponding valid c++ declarations. declaration meaning int x; x is an int int * x; x is a pointer to an int char ** x; int * x [5]; int (* x) [5]; x is a pointer to an array of 5 ints int (* x [5]) [5]; int * (* x [5]) [5]; x is an array of 5 pointers to arrays of 5 pointers to ints int x(); int x(int); int * x(); int * x(int *); int (* x)(); int ** (* x)(int **); 2 const pointers activity before we get started working with const pointers, we should be clear about the terminology (which sometimes varies from textbook to textbook). for this class, we will define the following terms. ˆ nonconstant pointer to nonconstant data: the pointer can be modified to point other data of the appropriate type, and the data to which it points can be modified ˆ nonconstant pointer to constant data: the pointer can be modified to point to other data of the appropriate type, but the data to which it points cannot be modified 2 ˆ constant pointer to nonconstant data: the pointer cannot be modified to point to other data of the appropriate type, but the data to which it points can be modified ˆ constant pointer to constant data: the pointer cannot be modified to point to other data of the appropriate type, and the data to which it points cannot be modified declaration syntax meaning int * ptr1; ptr1 is a nonconstant pointer to a nonconstant int const int * ptr2; ptr2 is a nonconstant pointer to a constant int int const * ptr3; ptr3 is a nonconstant pointer to a constant int (same as ptr2) int * const ptr4; ptr4 is a constant pointer to a nonconstant int const int * const ptr5; ptr5 is a constant pointer to a constant int int const * const ptr6; ptr6 is a constant pointer to a constant int (same as ptr5) also read the section(s) in your c++ textbook about using const with pointers, and then proceed to finish this lab. 1. consider the following code (assume proper includes, usings, etc): int main() { int x = 44; int y = 22; // part (a) int * p1 = &x; *p1 = 11; // part (b) const int * p2 = &x; *p2 = 11; p2 = &y; // part (c) const int * const p3 = &x; *p3 = 11; p3 = &y; return exit_success; } // main (a) which statements of the code marked for part (a) are valid, and which statements are invalid? if a statement is invalid, please explain why. (b) which statements of the code marked for part (b) are valid, and which statements are invalid? if a statement is invalid, please explain why. (c) which statements of the code marked for part (c) are valid, and which statements are invalid? if a statement is invalid, please explain why. 2. consider the following code (assume proper includes, usings, etc): int main() { int a [2] = {1, 2}; int b [2] = {3, 4}; // part (a) int * p1 = a; const int * p2 = a; 3 const int * const p3 = a; // part (b) p1++; p2++; p3++; // part (c) a = b; a++; (*a)++; return exit_success; } // main (a) which statements of the code marked for part (a) are valid, and which statements are invalid? if a statement is invalid, please explain why. (b) which statements of the code marked for part (b) are valid, and which statements are invalid? if a statement is invalid, please explain why. (c) which statements of the code marked for part (c) are valid, and which statements are invalid? if a statement is invalid, please explain why. 3. consider the following code (assume proper includes, usings, etc): int a [2] = {1, 2}; const int foo() { return 2; } // foo const int * bar() { return a; } // bar int * const baz() { return a; } // baz int main() { // part (a) int x = foo(); x++; foo()++; // part (b) const int * p1 = bar(); p1++; (*p1)++; bar()++; (*bar())++; // part (c) const int * p2 = baz(); p2++; (*p2)++; baz()++; 4 (*baz())++; return exit_success; } // main (a) which statements of the code marked for part (a) are valid, and which statements are invalid? if a statement is invalid, please explain why. (b) which statements of the code marked for part (b) are valid, and which statements are invalid? if a statement is invalid, please explain why. (c) which statements of the code marked for part (c) are valid, and which statements are invalid? if a statement is invalid, please explain why. 5 functions, pointers, and tricky declarations activity const pointers activity endl;="" return="" exit_success;="" }="" main="" 2.="" in="" the="" code="" presented="" in="" the="" previous="" question,="" why="" is="" &x="" used="" for="" the="" function="" parameter="" on="" line="" 16?="" 3.="" complete="" the="" following="" table="" by="" providing="" plain="" english="" meanings="" to="" the="" corresponding="" valid="" c++="" declarations.="" declaration="" meaning="" int="" x;="" x="" is="" an="" int="" int="" *="" x;="" x="" is="" a="" pointer="" to="" an="" int="" char="" **="" x;="" int="" *="" x="" [5];="" int="" (*="" x)="" [5];="" x="" is="" a="" pointer="" to="" an="" array="" of="" 5="" ints="" int="" (*="" x="" [5])="" [5];="" int="" *="" (*="" x="" [5])="" [5];="" x="" is="" an="" array="" of="" 5="" pointers="" to="" arrays="" of="" 5="" pointers="" to="" ints="" int="" x();="" int="" x(int);="" int="" *="" x();="" int="" *="" x(int="" *);="" int="" (*="" x)();="" int="" **="" (*="" x)(int="" **);="" 2="" const="" pointers="" activity="" before="" we="" get="" started="" working="" with="" const="" pointers,="" we="" should="" be="" clear="" about="" the="" terminology="" (which="" sometimes="" varies="" from="" textbook="" to="" textbook).="" for="" this="" class,="" we="" will="" define="" the="" following="" terms.="" ˆ="" nonconstant="" pointer="" to="" nonconstant="" data:="" the="" pointer="" can="" be="" modified="" to="" point="" other="" data="" of="" the="" appropriate="" type,="" and="" the="" data="" to="" which="" it="" points="" can="" be="" modified="" ˆ="" nonconstant="" pointer="" to="" constant="" data:="" the="" pointer="" can="" be="" modified="" to="" point="" to="" other="" data="" of="" the="" appropriate="" type,="" but="" the="" data="" to="" which="" it="" points="" cannot="" be="" modified="" 2="" ˆ="" constant="" pointer="" to="" nonconstant="" data:="" the="" pointer="" cannot="" be="" modified="" to="" point="" to="" other="" data="" of="" the="" appropriate="" type,="" but="" the="" data="" to="" which="" it="" points="" can="" be="" modified="" ˆ="" constant="" pointer="" to="" constant="" data:="" the="" pointer="" cannot="" be="" modified="" to="" point="" to="" other="" data="" of="" the="" appropriate="" type,="" and="" the="" data="" to="" which="" it="" points="" cannot="" be="" modified="" declaration="" syntax="" meaning="" int="" *="" ptr1;="" ptr1="" is="" a="" nonconstant="" pointer="" to="" a="" nonconstant="" int="" const="" int="" *="" ptr2;="" ptr2="" is="" a="" nonconstant="" pointer="" to="" a="" constant="" int="" int="" const="" *="" ptr3;="" ptr3="" is="" a="" nonconstant="" pointer="" to="" a="" constant="" int="" (same="" as="" ptr2)="" int="" *="" const="" ptr4;="" ptr4="" is="" a="" constant="" pointer="" to="" a="" nonconstant="" int="" const="" int="" *="" const="" ptr5;="" ptr5="" is="" a="" constant="" pointer="" to="" a="" constant="" int="" int="" const="" *="" const="" ptr6;="" ptr6="" is="" a="" constant="" pointer="" to="" a="" constant="" int="" (same="" as="" ptr5)="" also="" read="" the="" section(s)="" in="" your="" c++="" textbook="" about="" using="" const="" with="" pointers,="" and="" then="" proceed="" to="" finish="" this="" lab.="" 1.="" consider="" the="" following="" code="" (assume="" proper="" includes,="" usings,="" etc):="" int="" main()="" {="" int="" x="44;" int="" y="22;" part="" (a)="" int="" *="" p1="&x;" *p1="11;" part="" (b)="" const="" int="" *="" p2="&x;" *p2="11;" p2="&y;" part="" (c)="" const="" int="" *="" const="" p3="&x;" *p3="11;" p3="&y;" return="" exit_success;="" }="" main="" (a)="" which="" statements="" of="" the="" code="" marked="" for="" part="" (a)="" are="" valid,="" and="" which="" statements="" are="" invalid?="" if="" a="" statement="" is="" invalid,="" please="" explain="" why.="" (b)="" which="" statements="" of="" the="" code="" marked="" for="" part="" (b)="" are="" valid,="" and="" which="" statements="" are="" invalid?="" if="" a="" statement="" is="" invalid,="" please="" explain="" why.="" (c)="" which="" statements="" of="" the="" code="" marked="" for="" part="" (c)="" are="" valid,="" and="" which="" statements="" are="" invalid?="" if="" a="" statement="" is="" invalid,="" please="" explain="" why.="" 2.="" consider="" the="" following="" code="" (assume="" proper="" includes,="" usings,="" etc):="" int="" main()="" {="" int="" a="" [2]="{1," 2};="" int="" b="" [2]="{3," 4};="" part="" (a)="" int="" *="" p1="a;" const="" int="" *="" p2="a;" 3="" const="" int="" *="" const="" p3="a;" part="" (b)="" p1++;="" p2++;="" p3++;="" part="" (c)="" a="b;" a++;="" (*a)++;="" return="" exit_success;="" }="" main="" (a)="" which="" statements="" of="" the="" code="" marked="" for="" part="" (a)="" are="" valid,="" and="" which="" statements="" are="" invalid?="" if="" a="" statement="" is="" invalid,="" please="" explain="" why.="" (b)="" which="" statements="" of="" the="" code="" marked="" for="" part="" (b)="" are="" valid,="" and="" which="" statements="" are="" invalid?="" if="" a="" statement="" is="" invalid,="" please="" explain="" why.="" (c)="" which="" statements="" of="" the="" code="" marked="" for="" part="" (c)="" are="" valid,="" and="" which="" statements="" are="" invalid?="" if="" a="" statement="" is="" invalid,="" please="" explain="" why.="" 3.="" consider="" the="" following="" code="" (assume="" proper="" includes,="" usings,="" etc):="" int="" a="" [2]="{1," 2};="" const="" int="" foo()="" {="" return="" 2;="" }="" foo="" const="" int="" *="" bar()="" {="" return="" a;="" }="" bar="" int="" *="" const="" baz()="" {="" return="" a;="" }="" baz="" int="" main()="" {="" part="" (a)="" int="" x="foo();" x++;="" foo()++;="" part="" (b)="" const="" int="" *="" p1="bar();" p1++;="" (*p1)++;="" bar()++;="" (*bar())++;="" part="" (c)="" const="" int="" *="" p2="baz();" p2++;="" (*p2)++;="" baz()++;="" 4="" (*baz())++;="" return="" exit_success;="" }="" main="" (a)="" which="" statements="" of="" the="" code="" marked="" for="" part="" (a)="" are="" valid,="" and="" which="" statements="" are="" invalid?="" if="" a="" statement="" is="" invalid,="" please="" explain="" why.="" (b)="" which="" statements="" of="" the="" code="" marked="" for="" part="" (b)="" are="" valid,="" and="" which="" statements="" are="" invalid?="" if="" a="" statement="" is="" invalid,="" please="" explain="" why.="" (c)="" which="" statements="" of="" the="" code="" marked="" for="" part="" (c)="" are="" valid,="" and="" which="" statements="" are="" invalid?="" if="" a="" statement="" is="" invalid,="" please="" explain="" why.="" 5="" functions,="" pointers,="" and="" tricky="" declarations="" activity="" const="" pointers="">
Answered 2 days AfterFeb 25, 2022

Answer To: Breakout Lab 05, CSCI 1730 More Pointer Activities Learning Objectives ˆ Understand and use pointers...

Chirag answered on Feb 27 2022
94 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here