OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next...

25
OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ Page | 1 Print Name _________________________________________ Time Required = ______.____ Hrs. Signature _______________________________________________________________ (pledged) OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points – Due Next Class Short Integer Review 1] ___________________________________________________________________________________ Write the line of code to create a short integer variable, called No1; 2] ___________________________________________________________________________________ The value automatically placed in the No1 container will be _?_ {0, 1, 32767, unknown} 3] printf("%ld", ______________________________________________________________________ Write the line of code to display the number of bytes that this compiler assigns to a short integer. 4] ___________________________________________________________________________________ The smallest short int that can be assigned ( using a visual studio 2017's 64 bit compiler) is _?_. 5] ___________________________________________________________________________________ The largest short int that can be assigned (using a visual studio 2017's 64 bit compiler) is _?_. 6] ___________________________________________________________________________________ The smallest short int that can be assigned (using a default 32 bit compiler) is _?_. 7] ___________________________________________________________________________________ The largest short int that can be assigned (using a default 32 bit compiler) is _?_. 8] ___________________________________________________________________________________ Write the line of code to display the value stored in No1 using printf; include a line feed in the display. 9] ___________________________________________________________________________________ Write the line of code to display the value stored in No1 using cout; include a line feed in the display. 10] ___________________________________________________________________________________ Write the line of code to interactively fill No1 using scanf. 11] ___________________________________________________________________________________ The 32 bit short integer container is _?_ bytes. 12] ___________________________________________________________________________________ The 64 bit short integer container is _?_ bytes. 13] ___________________________________________________________________________________ Write the line of code to display the value stored in No1 using printf display the contents right justified in a six character field; include a line feed in the display. 14] ___________________________________________________________________________________ Write the line of code to display the value stored in No3 using printf display the contents left justified in a six character field; include a line feed in the display.

Transcript of OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next...

Page 1: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 1

Print Name _________________________________________ Time Required = ______.____ Hrs. Signature _______________________________________________________________ (pledged)

OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points – Due Next Class

Short Integer Review

1] ___________________________________________________________________________________ Write the

line of code to create a short integer variable, called No1;

2] ___________________________________________________________________________________ The value

automatically placed in the No1 container will be _?_ {0, 1, 32767, unknown}

3] printf("%ld", ______________________________________________________________________ Write the

line of code to display the number of bytes that this compiler assigns to a short integer.

4] ___________________________________________________________________________________ The

smallest short int that can be assigned ( using a visual studio 2017's 64 bit compiler) is _?_.

5] ___________________________________________________________________________________ The largest

short int that can be assigned (using a visual studio 2017's 64 bit compiler) is _?_.

6] ___________________________________________________________________________________ The

smallest short int that can be assigned (using a default 32 bit compiler) is _?_.

7] ___________________________________________________________________________________ The largest

short int that can be assigned (using a default 32 bit compiler) is _?_.

8] ___________________________________________________________________________________ Write the

line of code to display the value stored in No1 using printf; include a line feed in the display.

9] ___________________________________________________________________________________ Write the

line of code to display the value stored in No1 using cout; include a line feed in the display.

10] ___________________________________________________________________________________ Write the

line of code to interactively fill No1 using scanf.

11] ___________________________________________________________________________________ The 32 bit short integer container is _?_ bytes.

12] ___________________________________________________________________________________ The 64 bit short integer container is _?_ bytes.

13] ___________________________________________________________________________________ Write the

line of code to display the value stored in No1 using printf display the contents right justified in a six character

field; include a line feed in the display.

14] ___________________________________________________________________________________ Write the

line of code to display the value stored in No3 using printf display the contents left justified in a six character

field; include a line feed in the display.

Page 2: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 2

15] ___________________________________________________________________________________ Write the

line of code to fill No1 with 123 display the contents left justified in a ten character field.

16] ___________________________________________________________________________________ Write the

line of code to fill No1 with the largest possible/acceptable value 64 bit compiler.

17] ___________________________________________________________________________________ Write the

line of code to fill No1 with the smallest possible/acceptable value 64 bit compiler..

18] ___________________________________________________________________________________ Write the

line of code to decrement No1 by 1..

19] ___________________________________________________________________________________ Write the

line of code to increment No1 by 1..

20] ___________________________________________________________________________________ Write the

line of code to display the address (in hex) indicating where No1 is stored in memory at the moment using printf.

21] ___________________________________________________________________________________ Write the

line of code to display the address (in decimal) indicating where No1 is stored in memory at the moment using

printf.

22] ___________________________________________________________________________________ Write the

line of code to interactively fill No1 using cin.

Integer Review

1] ___________________________________________________________________________________ Write the line of code to create an integer variable, called No2;

2] ___________________________________________________________________________________ The value

automatically placed in the No2 container will be _?_ {0, 1, 32767, unknown}

3] printf("%ld", ______________________________________________________________________ Write the

line of code to display the number of bytes that this compiler assigns to an integer.

4] ___________________________________________________________________________________ The

smallest int that can be assigned (using a visual studio 2017's 64 bit compiler) is about _?_ billion.

5] ___________________________________________________________________________________ The largest

int that can be assigned (using a visual studio 2017's 64 bit compiler) is about _?_ billion.

6] ___________________________________________________________________________________ The

smallest int that can be assigned (using a default 32 bit compiler) is _?_.

7] ___________________________________________________________________________________ The largest

int that can be assigned (using a default 32 bit compiler) is _?_.

Page 3: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 3

8] ___________________________________________________________________________________ Write the

line of code to display the value stored in No2using printf; include a line feed in the display.

9] ___________________________________________________________________________________ Write the

line of code to display the value stored in No2using cout; include a line feed in the display.

10] ___________________________________________________________________________________ Write the

line of code to interactively fill No2using scanf.

11] ___________________________________________________________________________________ The 32 bit integer container is _?_ bytes.

12] ___________________________________________________________________________________ The 64 bit integer container is _?_ bytes.

13] ___________________________________________________________________________________ Write the

line of code to display the value stored in No2 using printf display the contents right justified in a ten character

field; include a line feed in the display.

14] ___________________________________________________________________________________ Write the

line of code to display the value stored in No2 using printf display the contents left justified in a ten character

field; include a line feed in the display.

15] ___________________________________________________________________________________ Write the

line of code to fill No2 with 123.

16] ___________________________________________________________________________________ Write the

line of code to fill No2 with the largest possible/acceptable value 64 bit compiler.

17] ___________________________________________________________________________________ Write the

line of code to fill No2 with the smallest possible/acceptable value 64 bit compiler..

18] ___________________________________________________________________________________ Write the

line of code to decrement No2 by 1..

19] ___________________________________________________________________________________ Write the

line of code to increment No2 by 1..

20] ___________________________________________________________________________________ Write the

line of code to display the address (in hex) indicating where No2 is stored in memory at the moment using printf.

21] ___________________________________________________________________________________ Write the

line of code to display the address (in decimal) indicating where No2 is stored in memory at the moment using

printf.

22] ___________________________________________________________________________________ Write the

line of code to interactively fill No2 using cin.

Page 4: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 4

Long Integer Review

1] ___________________________________________________________________________________ Write the

line of code to create a long integer variable, called No3;

2] ___________________________________________________________________________________ The value

automatically placed in the No3 container will be _?_ {0, 1, 32767, unknown}

3] printf("%ld", ______________________________________________________________________ Write the

line of code to display the number of bytes that this compiler assigns to a long integer.

4] ___________________________________________________________________________________ The

smallest long int that can be assigned ( using a visual studio 2017's 64 bit compiler) is about _?_ billion.

5] ___________________________________________________________________________________ The largest

long int that can be assigned ( using a visual studio 2017's 64 bit compiler) is about _?_ billion.

6] ___________________________________________________________________________________ The

smallest long int that can be assigned (using a default 32 bit compiler) is about _?_ billion.

7] ___________________________________________________________________________________ The largest

long int that can be assigned (using a default 32 bit compiler) is about _?_ billion.

8] ___________________________________________________________________________________ Write the

line of code to display the value stored in No3 using printf; include a line feed in the display.

9] ___________________________________________________________________________________ Write the

line of code to display the value stored in No3 using cout; include a line feed in the display.

10] ___________________________________________________________________________________ Write the

line of code to interactively fill No3 using scanf.

11] ___________________________________________________________________________________ The 32 bit long integer container is _?_ bytes.

12] ___________________________________________________________________________________ The 64 bit long integer container is _?_ bytes.

13] ___________________________________________________________________________________ Write the

line of code to display the value stored in No3 using printf display the contents right justified in a seven

character field; include a line feed in the display.

14] ___________________________________________________________________________________ Write the

line of code to display the value stored in No3 using printf display the contents left justified in a seven character

field; include a line feed in the display.

15] ___________________________________________________________________________________ Write the

line of code to fill No3 with 123.

Page 5: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 5

16] ___________________________________________________________________________________ Write the

line of code to fill No3 with the largest possible/acceptable value 64 bit compiler.

17] ___________________________________________________________________________________ Write the

line of code to fill No3 with the smallest possible/acceptable value 64 bit compiler..

18] ___________________________________________________________________________________ Write the

line of code to decrement No3 by 1..

19] ___________________________________________________________________________________ Write the

line of code to increment No3 by 1..

20] ___________________________________________________________________________________ Write the

line of code to display the address (in hex) indicating where No3 is stored in memory at the moment using printf.

21] ___________________________________________________________________________________ Write the

line of code to display the address (in decimal) indicating where No3 is stored in memory at the moment using

printf.

22] ___________________________________________________________________________________ Write the

line of code to interactively fill No3 using cin.

Float Review

1] ___________________________________________________________________________________ Write the

line of code to create a floating point variable, called No4;

2] ___________________________________________________________________________________ The 32 bit floating point container is _?_ bytes.

3] ___________________________________________________________________________________ The 64 bit floating point container is _?_ bytes.

4] ___________________________________________________________________________________ The float provides at least _?_ digits of accuracy.

5] printf("%ld", ______________________________________________________________________ Write the

line of code to display the number of bytes that this compiler assigns to a floating point number.

6] ___________________________________________________________________________________ Write the

line of code to display the value stored in No4 using printf; include a line feed in the display.

7] ___________________________________________________________________________________ Write the

line of code to display the value stored in No4 using cout; include a line feed in the display.

8] ___________________________________________________________________________________ Write the

line of code to interactively fill No4 using scanf.

Page 6: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 6

9] ___________________________________________________________________________________ Write the

line of code to display the value stored in No4 using printf display the contents right justified in a 10 character

field with 3 digits to the right of the decimal point; include a line feed in the display.

10] ___________________________________________________________________________________ Write the

line of code to display the value stored in No4 using printf display the contents left justified in a 10 character

field with 3 digits to the right of the decimal point; include a line feed in the display.

11] ___________________________________________________________________________________ Write the

line of code to fill No4 with 12.3456.

12] ___________________________________________________________________________________ Write the

line of code to display the address (in hex) indicating where No4 is stored in memory at the moment using printf.

13] ___________________________________________________________________________________ Write the

line of code to display the address (in decimal) indicating where No4 is stored in memory at the moment using

printf.

14] ___________________________________________________________________________________ Write the

line of code to interactively fill No4 using cin.

Double Review

1] ___________________________________________________________________________________ Write the

line of code to create a double variable, called No5;

2] ___________________________________________________________________________________ The 32 bit double container is _?_ bytes.

3] ___________________________________________________________________________________ The 64 bit double container is _?_ bytes.

4] ___________________________________________________________________________________ The float provides at least _?_ digits of accuracy.

5] printf("%ld", ______________________________________________________________________ Write the

line of code to display the number of bytes that this compiler assigns to a double number.

6] ___________________________________________________________________________________ Write the

line of code to display the value stored in No5 using printf; include a line feed in the display.

7] ___________________________________________________________________________________ Write the

line of code to display the value stored in No5 using cout; include a line feed in the display.

8] ___________________________________________________________________________________ Write the

line of code to interactively fill No5 using scanf.

9] ___________________________________________________________________________________ Write the

line of code to display the value stored in No5 using printf display the contents right justified in a 10 character

field with 3 digits to the right of the decimal point; include a line feed in the display.

Page 7: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 7

10] ___________________________________________________________________________________ Write the

line of code to display the value stored in No5 using printf display the contents left justified in a 10 character

field with 3 digits to the right of the decimal point; include a line feed in the display.

11] ___________________________________________________________________________________ Write the

line of code to fill No5 with 12.3456.

12] ___________________________________________________________________________________ Write the

line of code to display the address (in hex) indicating where No5 is stored in memory at the moment using printf.

13] ___________________________________________________________________________________ Write the

line of code to display the address (in decimal) indicating where No5 is stored in memory at the moment using

printf.

14] ___________________________________________________________________________________ Write the

line of code to interactively fill No5 using cin.

Double Review

1] ___________________________________________________________________________________ Write the

line of code to create a character variable, called Ch6;

2] ___________________________________________________________________________________ The 32 bit character container is _?_ byte(s).

3] ___________________________________________________________________________________ The 64 bit double container is _?_ byte(s).

4] printf("%ld", ______________________________________________________________________ Write the

line of code to display the number of bytes that this compiler assigns to a character.

5] ___________________________________________________________________________________ Write the

line of code to display the value stored in Ch6 using printf; include a line feed in the display.

6] ___________________________________________________________________________________ Write the

line of code to display the value stored in Ch6 using cout; include a line feed in the display.

7] ___________________________________________________________________________________ Write the

line of code to interactively fill Ch6 using scanf.

8] ___________________________________________________________________________________ Write the

line of code to display the value stored in Ch6 using printf display the contents right justified in a 10 character

field; include a line feed in the display.

9] ___________________________________________________________________________________ Write the

line of code to display the value stored in Ch6 using printf display the contents left justified in a 10 character

field; include a line feed in the display.

10] ___________________________________________________________________________________ Write the

line of code to fill Ch6 with A.

Page 8: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 8

11] ___________________________________________________________________________________ Characters are delimited with a(n) _?_ symbol.

12] ___________________________________________________________________________________ Characters are delimited with a(n) _?_ {single quote/double quote/asterisk/period} symbol.

13] ___________________________________________________________________________________ Write the

line of code to display the address (in hex) indicating where Ch6 is stored in memory at the moment using printf.

14] ___________________________________________________________________________________ Write the

line of code to display the address (in decimal) indicating where Ch6 is stored in memory at the moment using

printf.

15] ___________________________________________________________________________________ Write the

line of code to interactively fill Ch6 using cin.

Review

1] Assume that variable x contains the value 123. To the right of each of the print statements below, generate the respective output; I strongly suggest you check your answers using the computer.

1 2 3 4 5 6 7 8 9

printf ("*%ld*", 54); * 5 4 * _ _ _ _ _

printf ("*%ld*", 54); * 5 4 * _ _ _ _ _

printf ("*%ld*", x); _ _ _ _ _ _ _ _ _

printf ("*%ld*", x); * 1 2 3 * _ _ _ _

printf ("*%5ld*", x); _ _ _ _ _ _ _ _ _

printf ("*%5ld*", x); * _ _ 1 2 3 * _ _

printf ("*%2ld*", x); _ _ _ _ _ _ _ _ _

printf ("*%2ld*", x); * 1 2 3 * _ _ _ _

printf ("*%-5ld*", x); _ _ _ _ _ _ _ _ _

printf ("*%-5ld*", x); * 1 2 3 _ _ * _ _

printf ("*%ld*", -1*x); _ _ _ _ _ _ _ _ _

printf ("*%ld*", -1*x); * - 1 2 3 _ * _ _

printf ("*%ld*", 6+4/2); _ _ _ _ _ _ _ _ _

printf ("*%ld*", 6+4/2); * 8 * _ _ _ _ _ _

Page 9: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 9

Print Name _________________________________________ Time Required = ______.____ Hrs. Signature _______________________________________________________________ (pledged)

Static Array Review

1] ___________________________________________________________________________________ Write the

line of C/C++ code to create an array, called Nos1, which has the capacity to store 6 integers.

2] ___________________________________________________________________________________ Each of the

5 elements in Nos1 is automatically initialize to _?_ {o's/ 1's/32767/Unknown}

3] ___________________________________________________________________________________ Write the

line of C/C++ code to create an array, called Nos2, which has the capacity to store 5 integers Initialize the values

to 1,2,3,4,5.

4] ___________________________________________________________________________________ Write the

line of C/C++ code to display the number of bytes in array Nos2;

5] ___________________________________________________________________________________ Write the output from the following line of code:

printf("%ld", sizeof(Nos1)/sizeof(int));

6] ___________________________________________________________________________________ Write the

line of C/C++ code to display the number of bytes in array Nos2;

7] _____ _____ _____ _____ _____ All of the acceptable subscripts for Nos2 include _?_.

8] Why should the programmer go to great lengths to avoid subscripts outside the acceptable bounds? _______________________________________________________________________________________

_______________________________________________________________________________________

_______________________________________________________________________________________

9] Write the block of code to display the contents of array Nos2 using a for loop right justified in 10 character

fields.

Page 10: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 10

10] Write the block of code to place 5, 10, 15, 20, 25 into array Nos2 using a for loop (Fixed Repetition).

11] ___________________________________________________________________________________ Write the

line of code to display the address (in hex) indicating where Nos2 is stored in memory at the moment using

printf.

12] ___________________________________________________________________________________ Write the

line of code to display the address (in decimal) indicating where No2 is stored in memory at the moment using

printf.

13] ___________________________________________________________________________________ Write the

line of code to display the address (in hex) indicating where Nos2 is stored in memory at the moment using

printf.

14] Write the block of code to display the contents of array Nos2 using a while loop (Pretest) right justified in 10

character fields.

15] Write the block of code to display the contents of array Nos2 using a repeat until loop (Posttest) right justified

in 10 character fields.

16] Write the block of code to display the contents of array Nos2 using a for loop right justified in 10 character

field one to a line. Create this loop in such a way that it will work – no matter what the number of elements in the array.

Page 11: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 11

17] ___________________________________________________________________________________ Write the

line of code to display the contents of element 3 of array Nos2 and the address (in hex) indicating where Nos2[3] is stored in memory at the moment using printf. Add a line feed to the end. Make the output look like the following: Nos2[3] = 4 &Nos2[3] = 93FA28

18] ___________________________________________________________________________________ According to the slides, the Base Address of Nos3 will always be the S_?_ as the address off Nos3[0].

short int Nos44[] = { 0,1,2,3,4,5 };

19] ___________________________________________________________________________________ When using a 64 bit compiler, the addresses of each of the elements of Nos44 will be _?_ bytes apart.

20] ___________________________________________________________________________________ When using a 32 bit compiler, the addresses of each of the elements of Nos44 will be _?_ bytes apart.

21] ___________________________________________________________________________________ According to the slides, each Array will always be stored In one C_?_ block of memory

22] ___________________________________________________________________________________ Write the

line of C/C++ code to create an array, called Values1, which has the capacity to store 1000 double numbers.

23] ___________________________________________________________________________________ Write the output from the following line of code:

printf("%ld", sizeof(Values1));

24] ___________________________________________________________________________________ Write the output from the following line of code:

printf("%ld", sizeof(Values1)/sizeof(float));

Character String Array Review

1] ___________________________________________________________________________________ Write the

line of C/C++ code to create a character string array, called Name1, which has the capacity to store 10 characters.

2] ___________________________________________________________________________________ Each of the

5 elements in Name1 is automatically initialize to _?_ {blanks/ A's/1's/Unknown}

3] ___________________________________________________________________________________ Write the

line of C/C++ code to create a character string array, called Name2, which has the capacity to store Initialize the

string "Trinity".

4] ___________________________________________________________________________________ Write the output from the following line of code:

printf("%ld", sizeof(Name2));

5] ___________________________________________________________________________________ Write the output from the following line of code:

printf("%ld", strlen(Name2));

Page 12: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 12

6] Write the block of code to display all of the elements in Name2 one element at a time using printf and a for

loop.

7] ___________________________________________________________________________________ Write the

line of C/C++ code to display the contents of Name2 using a single puts..

8] ___________________________________________________________________________________ Write the

line of C/C++ code to display the contents of Name2 using a single printf include a line feed.

9] Write the block (4 lines) of code to manually place the string "Tom" into Name2 one character at a time. Do not

use strcpy_s.

10] ___________________________________________________________________________________ Write the output from the following line of code. Assume that all previous coding was done correctly.

printf("%ld", strlen(Name2));

11] ___________________________________________________________________________________ Write the output from the following line of code. Assume that all previous coding was done correctly.

printf("%ld", sizeof(Name2));

12] ___________________________________________________________________________________ Write the

line of C/C+ code to fill Name2 with the string "Doc" using strcpy_s.

13] ___________________________________________________________________________________ Write the

line of code to display the address (in hex) indicating where Name2 is stored in memory at the moment using

printf.

14] ___________________________________________________________________________________ When using

a 64 bit compiler, the addresses of each of the elements of Name2 will be _?_ bytes apart.

15] ___________________________________________________________________________________ When using

a 32 bit compiler, the addresses of each of the elements of Name2 will be _?_ bytes apart.

16] __________ {T/F} Character String Arrays are stored in one contiguous block of memory.

Page 13: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 13

17] __________ {T/F} The following assignment statement can be used to fill Name2. Name2 = "Stupid";

18] __________ {T/F} According to the slides, it is the Programmer's Responsibility to make sure that their Arrays are of the proper size.

19] __________ {T/F} According to the slides, Bad Things Happen when you start changing elements outside the boundaries of your array; when you overflow the capacity of an array, it always destroys something else.

20] Write the block of C/C++ code to prompt the user for Name2 fill it using scanf. Don't forget the 2 flushes!

21] Write the block of C/C++ code to prompt the user for Name2 fill it using gets. Don't forget the flush!

22] __________ {T/F} It is a good programming practice to include a flush after each and every scanf.

23] __________ {T/F} It is a good programming practice to include a flush after each and every gets_s.

24] ___________________________________________________________________________________ Suppose

the user enters "Jo Jackson" when prompted for Name2 the data is read with the following line of code:

scanf("%s", &Name2); What is the value now stored in Name2?

25] ___________________________________________________________________________________ Suppose

the user enters "Jo Jack" when prompted for Name2 the data is read with the following line of code:

gets_s(Name2); What is the value now stored in Name2?

26] __________ {T/F} The gets_s function inserts the EOS marker.

27] ___________________________________________________________________________________ EOS is an acronym for _?_.

Page 14: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 14

28] __________ {T/F} The strcpy_s function inserts the EOS marker.

29] __________ {T/F} The strncpy function inserts the EOS marker.

char Temp[500]; strcpy_s(Temp, "Tom Terrific");

30] Assume the code above. Write the block (2 lines) of C/C++ code that could be used to fill Name2 with the first 3

characters from Temp fill it using strncpy. Don't forget the EOS!

31] ___________________________________________________________________________________ Write the output from the following line of C/C++ code:

cout << strcmp("David", "Adam")

32] ___________________________________________________________________________________ Write the output from the following line of C/C++ code:

cout << strcmp("David", "Roger")

33] ___________________________________________________________________________________ Write the output from the following line of C/C++ code:

cout << strcmp("David", "David")

34] ___________________________________________________________________________________ Write the output from the following line of C/C++ code:

cout << strcmp("David", "david")

35] ___________________________________________________________________________________ Write the output from the following line of C/C++ code:

cout << strcmp("David", " David")

36] ___________________________________________________________________________________ According

to the slides, strcmp is often used when So_?_ strings.

37] ___________________________________________________________________________________Write the

line of code to change the character in position 0 of string Name2 to an 'Z'.

38] ___________________________________________________________________________________Write the

line of code to change the character in position 3 of string Name2 to an 'A'.

Page 15: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 15

Print Name _________________________________________ Time Required = ______.____ Hrs. Signature _______________________________________________________________ (pledged)

Function Terminology

1] ___________________________________________________________________________________ A C_?_ is a Value that cannot be altered by the program during normal execution

2] ___________________________________________________________________________________ Write a line

of C/C++ code to create a constant, called TaxRate, whose assigned value is 8.575. HINT there is no

semicolon.

3] ___________________________________________________________________________________ AV_?_ is a storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a Value.

4] ___________________________________________________________________________________ Write a line

of C/C++ code to create an integer variable, called Major, whose known/initialized value is CS.

5] ___________________________________________________________________________________ Both C & C++ are languages whose datatyping is _?_ {static/dynamic}.

6] ___________________________________________________________________________________ List at least three languages whose datatyping is static.

7] ___________________________________________________________________________________ List at least three languages whose datatyping is static.

8] The slides list 3 reasons we need functions. List those reasons.

R____________________________ D____________________________

R____________________________ A____________________________

P____________________________ tasks into smaller partitions for teams.

9] The slides provide the recommended program layout for C & C++. Assign numbers 1-5 to the following to reflect that order.

_____ Structs & Classes _____Include Statements _____Functions _____Define Statements _____Prototypes

10] ___________ {T/F} It is never appropriate to have 1 line functions.

11] ___________________________________________________________________________________ Write the line of C/C++ code to clear the terminal window when using Visual Studio.

Page 16: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 16

About Memory Maps

1] ___________ {T/F} The memory maps, in the slides includes [1] a box representing the container [2] the number of bytes required to store the container at the top [3] the symbolic name for the container at the side [4] an address where the computer has chosen to store the variable at the bottom (sometimes made up but could be determined)

2] ___________________________________________________________________________________ Write the line of C/C++ code to create the64 bit Visual Studio variable represented in the memory map above.

3] ___________________________________________________________________________________ What is the symbolic name stored in the memory map above?

4] ___________________________________________________________________________________ Write the

line of C/C++ code to increment the value stored in No1.

5] ___________________________________________________________________________________ Write the

line of C/C++ code to display the value stored in No1. Output format is to be: No1 = 5

6] ___________________________________________________________________________________ Write the

line of C/C++ code to display the address (hex) where No1 is stored in memory. Output format: &No1 = 1B2A

7] ___________________________________________________________________________________ Write the

line of C/C++ code to display the address (decimal) where No1 is stored in memory. Output format: &No1 = 1274

19] ___________________________________________________________________________________ Write the

line of C/C++ code to display the number of bytes allocated to No1.

Memory Maps

Assume Compiler Assigns Memory Starting At &1000 (Decimal) for each of the following

char CH = 'A';

1] Sketch the Memory Map for CH

.

Page 17: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 17

long int X;

1] Sketch the Memory Map for X

.

Functions

1] Write the complete C/C++ of code for function Decrement. suppose the contents of long int container X = 5

after passing X to Decrement, the value is to be 1 less X would be 4. You may not use reference variables.

There is to be no explicit return. Test your code if you are not sure.

2] ___________________________________________________________________________________ Write the

line of C/C++ code that you might place in main to call your function Decrement pass X to your function.

3] __________ {T/F} You can pass the constant 5 to your Decrement function.

double A = 1, B = 0, C = -25, Root1, Root2;

4-13] Write the complete C/C++ of code for function Quadratic. Pass your function A, B, C by value. Pass

Root1 & Root2 by reference. When finished, Root1 & Root2 should contain the quadratic roots ( 5 & -5 this

time). Display an error message if unable to set the roots. Test your solution! <If you choose to code this, you may staple a printout, that includes this function, to the back>

Page 18: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 18

short int ExamGrade = GetShort("Enter Exam Grade", 0, 100); cout << "Your ExamGrade = " << ExamGrade << endl << endl;

14-23] You can see, in the function call above, that GetShort is passed a prompt, a low, and a high. The function will continue to prompt the user & solicit a response until the user enters a value in the range low <= x <= high. Your output is to match the example below: Don't forget flush <If you choose to code this, you may staple a printout, that includes this function, to the back>

Page 19: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 19

long int RandNo (long int Low, long int High);

24] ___________________________________________________________________________________ Both of the

arguments in function RandNo are passed by _?_ {Value/Reference}

25] ___________________________________________________________________________________ Function

RandNo _?_ {Has/Does Not Have} an explicit return.

void Quadratic (double A, double B, double C, double* Root1, double* Root2)

26] ___________________________________________________________________________________ Arguments

A, B, & C in function Quadratic are passed by _?_ {Value/Reference}

27] ___________________________________________________________________________________ Arguments

Root1 & Root2 in function Quadratic are passed by _?_ {Value/Reference}

28] ___________________________________________________________________________________ Function

Quadratic _?_ {Has/Does Not Have} an explicit return.

Quadratic (1.0, -8.0, 15.0, &R1, &R2);

29] ____________{T/F} When Function Quadratic is called in the line of code above, the compiler creates a double

container [called A] and places 1.0 into that container.

30] ____________{T/F} When Function Quadratic is called in the line of code above, the compiler creates a pointer to a

double container [called Root1] and places the address of R1 into that container. 31] ____________{T/F} At the end of Function Quadratic, the memory allocated for A, B, C, Root1, & Root2 is

returned to the operating system for reuse.

void FillIntArray (int Array[], int StartingNo, int NoElements)

32] ___________________________________________________________________________________ Arguments

StartingNo & NoElements in function FillIntArray are passed by _?_ {Value/Reference}

33] ___________________________________________________________________________________ Argument

Array in function FillIntArray is passed by _?_ {Value/Reference}

34] ___________________________________________________________________________________ Function

FillIntArray _?_ {Has/Does Not Have} an explicit return. 35] ___________________________________________________________________________________ Arrays are

always passed by _?_ {Value/Reference}

Page 20: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 20

void FillIntArrayRandom(int Array[ ], int Low, int High, int NoElements);

int main (int argc, char * argv[]) { int Exams[25];

FillIntArrayRandom (Exams, 50, 100, 25);

36] ___________________________________________________________________________________ Arguments

Low, High, & NoElements in function FillIntArrayRandom are passed by _?_ {Value/Reference}

37] ___________________________________________________________________________________ Argument

Array in function FillIntArrayRandom is passed by _?_ {Value/Reference}

38] ___________________________________________________________________________________ Function

FillIntArrayRandom _?_ {Has/Does Not Have} an explicit return.

printf("Main--> sizeof(Exams)= %ld\n", sizeof(Exams));

39] ___________________________________________________________________________________ Write the output from the following line of code [main] above.

printf("FillIntArray--> sizeof(Array)= %ld\n", sizeof(Array))

40] ___________________________________________________________________________________ Write the output from the following line of code [FillIntArrayRandom] above.

41-50] Write the complete C/C++ of code for function FillIntArrayRandom. Pass your function Low, High,

NoElements by value. Pass Array by reference. When finished, all of the elements of Array should contain

the random numbers in the range Low <= x <= High <If you choose to code this, you may staple a printout, that includes this function, to the back>

51] ___________________________________________________________________________________

Character Strings are always passed by _?_ {Value/Reference}

Page 21: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 21

52-58] Write the block of C/C++ code to create a structure called Part.. Data members shall include Name – 26 <If

you choose to code this, you may staple a printout, that includes this function, to the back>

Part Mac;

Set (&Mac, 112233, "Macbook Pro", 4, 1, 'F', 1999.952);

59-68] Write the complete of C/C++ code to create function Set; when the code above is placed in main, all of the class

data members will be filled. <If you choose to code this, you may staple a printout, that includes this function, to the back>

Page 22: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 22

Part Mac;

Set (&Mac, 112233, "Macbook Pro", 4, 1, 'F', 1999.952); Display (&Mac, "My Macbook Pro:");

Display Cost 2 digits To Right Of Decimal Point

69-78] Write the complete of C/C++ code to create function Display; when the code above is placed in main, all of the

class data members will be filled and then displayed exactly like the above right. <If you choose to code this, you may staple a printout, that includes this function, to the back>

Page 23: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 23

Part Mac, Dell;

Get(&Mac, "Enter Data For The Mac:"); Get(&Dell, "Enter Data For The Dell:"); Display (&Mac, "My Macbook Pro:"); Display(&Dell, "My Dell Laptop:");

79-100] Write the complete of C/C++ code to create function Get; when the code above is placed in main, the user will

be prompted to interactively fill the Part exactly as illustrated below. <If you choose to code this, you may staple a printout, that includes this function, to the back>

Page 24: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 24

Print Name _________________________________________ Time Required = ______.____ Hrs. Signature _______________________________________________________________ (pledged)

Dynamic Memory

1] ___________________________________________________________________________________ Write the

line of C/C++ code to create a pointer to a long integer called PtrLong Do not initialize it to any value.

2] ____________{T/F} PtrLong is automatically initialized to NULL.

3] ___________________________________________________________________________________ Write the

line of C/C++ code to create a pointer to a long integer called PtrLong Iinitialize it to NULL.

4] ___________________________________________________________________________________ RAM is an acronym for _?_.

5] ___________________________________________________________________________________ If you had

only 4 GB of RAM on your personal computer, the acceptable range of values for PtrLong would be from 0 to _?_.

6] ___________________________________________________________________________________ Write the

line of C++ code to dynamically allocate one long integer use PtrLong do not use malloc.

7] ___________________________________________________________________________________ Once we

dynamically allocate memory and associate it with PtrLong, we must remember to return that memory. Write the

line of C++ code to return the memory do not use free.

8] ___________________________________________________________________________________ Once we

free dynamic memory, it is a good programming practice to assign PtrLong to _?_ avoid "Dangling Pointers"!

Write the line of C/C++ code to do this.

9] ___________________________________________________________________________________ Assume

that memory has been allocated to PtrLong. Write the line of C/C++ code to fill that dynamic container with 25.

10] ___________________________________________________________________________________ Write the

line of C++ code to dynamically allocate one million integer use PtrInt do not use malloc.

11] ___________________________________________________________________________________ If there was

not a contiguous block of 4,000,000 bytes of memory available, the compiler would assign _?_ to PtrInt when

attempting to dynamically allocate memory.

Page 25: OOP-2 Lab -INDIVIDUAL ASSIGNMENT 20 Points Due Next Classcarme.cs.trinity.edu/.../Labs/OOP-01-Review-Pointers-Dynamic-MemoryHW.pdf · OOP-2 Lab -INDIVIDUAL ASSIGNMENT – 20 Points

OOP-1-Review-Pointers-Dynamic-MemoryHW.docx CSCI 2320 Initials ______ P a g e | 25

12-21] Write the complete code for function main which will (1) create a dynamic array with 5,000,000 long integers to a

pointer called Ptr, (2) fill this array with values 0,3,6,9,…(if successful) , (3) display the values one to a line right

justified in a ten character field <You may attach a printout if you choose to code this>

struct Student { char Name[26]; long No; };

22] ___________________________________________________________________________________ Write the

line of C/C++ code to create a pointer to a student called St Iinitialize it to NULL.

23] ___________________________________________________________________________________ Write the

line of C/C++ code to dynamically allocate one student to St.

24] ___________________________________________________________________________________ Write the

line of C/C++ code to assign the "Jane Doe" to the Name of the memory pointed to by St.

25] ___________________________________________________________________________________ Write the

line of C/C++ code to assign the 21 to the No of the memory pointed to by St.

26] ___________________________________________________________________________________ Write the

line of C/C++ code to display the Name of the memory pointed to by St in the following format:

Name.. = Jane Doe;

27] ___________________________________________________________________________________ Write the

line of C/C++ code to display the No of the memory pointed to by St in the following format:

No…... = Jane Doe;