Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

For each of the following, write C++ statements that perform the specified task. Assume that unsigned integers are stored in two bytes and that the starting address of the array is at location 1002500 in memory. a. Declare an array of type unsigned int called values with five elements, and initialize the elements to the even integers from 2 to 10. Assume that the symbolic constant SIZE has been defined as 5. b. Declare a pointer vPtr that points to an object of type unsigned int. c. Use a for statement to print the elements of array values using array subscript notation. d. Write two separate statements that assign the starting address of array values to pointer variable vPtr. e. Use a for statement to print the elements of array values using pointer/offset notation. f. Use a for statement to print the elements of array values using pointer/offset notation with the array name as the pointer. g. Use a for statement to print the elements of array values by subscripting the pointer to the array. h. Refer to the fifth element of values using array subscript notation, pointer/offset notation with the array name as the pointer, pointer subscript notation and pointer/offset notation. i. What address is referenced by vPtr + 3? What value is stored at that location? j. Assuming that vPtr points to values[ 4 ], what address is referenced by vPtr -= 4? What value is stored at that location

Short Answer

Expert verified
The elements in the array are printed using different notations. Addresses and values are computed based on pointer arithmetic.

Step by step solution

01

Declare and Initialize Array

To declare an array of type unsigned int called values with five elements and initialize it to even integers from 2 to 10, we use the statement: ```cpp const int SIZE = 5; unsigned int values[SIZE] = {2, 4, 6, 8, 10}; ```
02

Declare a Pointer

Declare a pointer `vPtr` that points to an object of type unsigned int with the following statement: ```cpp unsigned int *vPtr; ```
03

Print Array with Subscripts

Use a for loop to print the elements of `values` using array subscript notation: ```cpp for (int i = 0; i < SIZE; ++i) { std::cout << values[i] << std::endl; } ```
04

Assign Array Address to Pointer

Two statements to assign the starting address of the array `values` to pointer `vPtr` are: ```cpp vPtr = values; ``` or equivalently: ```cpp vPtr = &values[0]; ```
05

Print Array with Pointer/Offset, Using Pointer

Using a for loop, print the elements of `values` using pointer/offset notation: ```cpp for (int offset = 0; offset < SIZE; ++offset) { std::cout << *(vPtr + offset) << std::endl; } ```
06

Print Array with Pointer/Offset, Using Array as Pointer

Print the elements using pointer/offset notation with the array name as the pointer: ```cpp for (int offset = 0; offset < SIZE; ++offset) { std::cout << *(values + offset) << std::endl; } ```
07

Print Array with Pointer Subscripts

Using a for loop, print the elements of the array by subscripting the pointer: ```cpp for (int i = 0; i < SIZE; ++i) { std::cout << vPtr[i] << std::endl; } ```
08

Referencing Fifth Element

The fifth element can be referred to using different notations: - Array subscript notation: `values[4]` - Pointer/offset notation with the array name as the pointer: `*(values + 4)` - Pointer subscript notation: `vPtr[4]` - Pointer/offset notation: `*(vPtr + 4)`
09

Compute Address vPtr + 3

If the starting address is 1002500, `vPtr + 3` references address `1002500 + 3 * 2 = 1002506`. The value at that location (fourth element of the array) is `8`.
10

Compute Address vPtr -= 4

Assuming `vPtr` initially points to `values[4]` with address `1002508`, executing `vPtr -= 4` moves it to address `1002500`. The value stored there is `2`, the first element of `values`.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Pointer Arithmetic
Pointer arithmetic in C++ allows programmers to perform operations on pointer variables to navigate through memory locations. When you have a pointer pointing to an array, adding an integer to the pointer moves it to the next addressable location in memory for that type. For instance, if the pointer points to an unsigned integer at memory location 1002500, and the size of an unsigned integer is two bytes, adding 3 to the pointer moves it to the memory location 1002506. Pointers increase or decrease by the size of the data type they point to:
  • Adding 1 moves the pointer to the next element.
  • Subtracting 1 moves it to the previous element.
  • Using pointer arithmetic, you can efficiently iterate over arrays without using array indexing, which can be especially useful in low-level programming or performance-sensitive code.
Array Subscript Notation
Array subscript notation is a straightforward and common way to access elements within an array. You use brackets [ ] with the array name and pass the index of the desired element inside the brackets. For example, ```cpp values[0], values[1] ``` accesses the first and second element of the array `values`, respectively.

When accessing the elements, always remember:
  • Arrays in C++ are 0-indexed, meaning the first element starts at index 0.
  • Out-of-bounds access can lead to undefined behavior, so be cautious when looping over
    arrays.
Overall, array subscript notation is simple and highly readable, especially for beginners.
Pointer/Offset Notation
Pointer/offset notation provides another way to access array elements without directly using an index. Instead of array subscripting, you use a pointer, then add an offset, and dereference the pointer to get to the element. For example: ```cpp *(vPtr + index) ``` Here, `vPtr` is a pointer to the start of an array, and `index` is an offset which helps in accessing different elements. This form is often preferred in low-level programming where control over direct memory access is needed. To avoid common pitfalls, remember:
  • Ensure the offset doesn't exceed the array bounds.
  • Pointer arithmetic respects the type size the pointer is associated with, and calculations adjust accordingly.

Using pointer/offset notation can lead to more compact and sometimes faster code execution when done correctly.
Address Computation in Memory
Address computation in memory is crucial for understanding how data is stored and accessed, especially with arrays and pointers in C++. Each element in an array occupies a consecutive memory location. Given a starting address, a pointer can calculate the address of any array element using the formula: \[\text{Address} = \text{base address} + (\text{index} \times \text{size of element})\]Using this formula, if you have an array starting at 1002500 and each element occupies 2 bytes, calculating the address of the fourth element (\[\text{index} = 3\]) means adding \(3 \times 2 = 6\) to the base address, resulting in 1002506. This formula is vital in pointer arithmetic and understanding how dereferencing and indexing occur in memory. Correct memory addressing is essential in avoiding bugs and ensuring data integrity, particularly in manual memory management typically required in C++.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

State whether the following are true or false. If false, explain why. a. Two pointers that point to different arrays cannot be compared meaningfully. b. Because the name of an array is a pointer to the first element of the array, array names can be manipulated in precisely the same manner as pointers.

Perform the task specified by each of the following statements: a. Write the function header for a function called exchange that takes two pointers to double-precision, floating-point numbers x and y as parameters and does not return a value. b. Write the function prototype for the function in part (a). c. Write the function header for a function called evaluate that returns an integer and that takes as parameters integer x and a pointer to function poly. Function poly takes an integer parameter and returns an integer. d. Write the function prototype for the function in part (c). e. Write two statements that each initialize character array vowel with the string of vowels, "AEIOU".

(Check Protection) Computers are frequently employed in check-writing systems such as payroll and accounts-payable applications. Many strange stories circulate regarding weekly paychecks being printed (by mistake) for amounts in excess of \(1 million. Weird amounts are printed by computerized check-writing systems, because of human error or machine failure. Systems designers build controls into their systems to prevent such erroneous checks from being issued. Another serious problem is the intentional alteration of a check amount by someone who intends to cash a check fraudulently. To prevent a dollar amount from being altered, most computerized check-writing systems employ a technique called check protection. Checks designed for imprinting by computer contain a fixed number of spaces in which the computer may print an amount. Suppose that a paycheck contains eight blank spaces in which the computer is supposed to print the amount of a weekly paycheck. If the amount is large, then all eight of those spaces will be filled, for example, 12345678 (position numbers) On the other hand, if the amount is less than \)1000, then several of the spaces would ordinarily be left blank. For example, 99.87 \-------- 12345678 contains three blank spaces. If a check is printed with blank spaces, it is easier for someone to alter the amount of the check. To prevent a check from being altered, many check-writing systems insert leading asterisks to protect the amount as follows: ***99.87 \-------- 12345678 Write a program that inputs a dollar amount to be printed on a check and then prints the amount in check-protected format with leading asterisks if necessary. Assume that nine spaces are available for printing an amount.

Write a program that uses function strncmp to compare two strings input by the user. The program should input the number of characters to compare. The program should state whether the first string is less than, equal to or greater than the second string. Write a program that uses random number generation to create sentences. The program should use four arrays of pointers to char called article, noun, verb and preposition. The program should create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article and noun. As each word is picked, it should be concatenated to the previous words in an array that is large enough to hold the entire sentence. The words should be separated by spaces. When the final sentence is output, it should start with a capital letter and end with a period. The program should generate 20 such sentences.

Perform the task specified by each of the following statements: a. Write the function header for function zero that takes a long integer array parameter bigIntegers and does not return a value. b. Write the function prototype for the function in part (a). c. Write the function header for function add1AndSum that takes an integer array parameter oneTooSmall and returns an integer. d. Write the function prototype for the function described in part (c)

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free