Question

In: Computer Science

1. For each of the following, write C++ statements that perform the specified task. Assume that...

1. For each of the following, write C++ statements that perform the specified task. Assume that unsigned integers are stored in four bytes and that the starting address of the built-in array is at location 1002500 in memory.

  1. Declare an unsigned int built-in array values with five elements initialized to the even integers from 2 to 10. Assume that the constant size has been defined as 5.
  2. Declare a pointer vPtr that points to an object of type unsigned int.
  3. Use a for statement to display the elements of built-in array values using array subscript notation.
  4. Write two separate statements that assign the starting address of built-in array values to pointer variable vPtr.
  5. Use a for statement to display the elements of built-in array values using pointer/offset notation.
  6. Use a for statement to display the elements of built-in array values using pointer/offset notation with the built-in array’s name as the pointer.
  7. Use a for statement to display the elements of built-in array values by subscripting the pointer to the built-in array.
  8. Refer to the fifth element of values using array subscript notation, pointer/offset notation with the built-in array name’s as the pointer, pointer subscript notation and pointer/offset notation.
  9. What address is referenced by vPtr + 3? What value is stored at that location?
  10. Assuming that vPtr points to values[4], what address is referenced by vPtr -= 4? What value is stored at that location?

2. Combine the statements in part 1 into one C++ program. Here is a sample output of your program.

Part C: Printing the using array subscript notation:

Elem. Value Addresses

0 2 00AFFC4C

1 4 00AFFC50

2 6 00AFFC54

3 8 00AFFC58

4 10 00AFFC5C

Part E: Printing the array using pointer/offset notation:

Elem. Value Addresses

0 2 00AFFC4C

1 4 00AFFC50

2 6 00AFFC54

3 8 00AFFC58

4 10 00AFFC5C

Part F: Printing the array using pointer/offset notation with the built-in arrays' name as the pointer

Elem. Value Addresses

0 2 00AFFC4C

1 4 00AFFC50

2 6 00AFFC54

3 8 00AFFC58

4 10 00AFFC5C

Part G: Printing the array by subscripting the pointer to the built-in array

Elem. Value Addresses

0 2 00AFFC4C

1 4 00AFFC50

2 6 00AFFC54

3 8 00AFFC58

4 10 00AFFC5C

Part H. Printing the fifth element of values using:

1. Array subscript notation: 10

2. Pointer/offest notation with array name as a pointer: 10

3. Pointer subscript notation: 10

4. Pointer/offset notation: 10

Part I. The address of vPtr + 3 : 00AFFC58

The values storedat vPtr + 3 : 8

Part J. The address of vPtr -= 4 : 00AFFC4C

The values stored at vPtr -= 4 : 2

Press any key to continue . . .

Solutions

Expert Solution

#include <iostream>

using namespace std;

int main()
{
   // (A)Declare an unsigned int built-in array values with five elements 
   //initialized to the even integers from 2 to 10.
   //Assume that the constant size has been defined as 5.
   unsigned int A[5]={2,4,6,8,10};
  // (B) Declare a pointer vPtr that points to an object of type unsigned int.
  unsigned int *vPtr;
  //(C) Use a for statement to display the elements of built-in array values using array subscript notation.
  cout<<"Elem.\t Value\t Addresses"<<endl;
  for(int i=0;i<5;i++)
  cout<<i<<"\t  "<<A[i]<<"\t"<<&A[i]<<endl;
  //(D) Write two separate statements that assign the starting address 
  //of built-in array values   to pointer variable vPtr.
  vPtr=&A[0];
  vPtr=A;
  //(E)Use a for statement to display the elements of built-in array values using pointer/offset notation.
  cout<<"Elem.\t Value\t Addresses"<<endl;
  for(int i=0;i<5;i++)
  cout<<i<<"\t  "<<*(vPtr+i)<<"\t"<<(vPtr+i)<<endl;
  //(F) Use a for statement to display the elements of built-in array values using pointer/offset 
  //notation with the built-in array’s name as the pointer.
  cout<<"Elem.\t Value\t Addresses"<<endl;
  for(int i=0;i<5;i++)
  cout<<i<<"\t  "<<*(A+i)<<"\t"<<(A+i)<<endl;
  //(G)Use a for statement to display the elements of built-in array values
  //by subscripting the pointer to the built-in array.
  cout<<"Elem.\t Value\t Addresses"<<endl;
  for(int i=0;i<5;i++)
  cout<<i<<"\t  "<<vPtr[i]<<"\t"<<&(vPtr[i])<<endl;
  //(H) Refer to the fifth element of values using array subscript notation, 
  //pointer/offset notation with the built-in array name’s as the pointer, 
  //pointer subscript notation and pointer/offset notation.
  
    cout<<"Printing the fifth element of values using:"<<endl;
    cout<<"1. Array subscript notation:"<<A[4]<<endl;
    cout<<"2. Pointer/offest notation with array name as a pointer:"<<*(A + 4)<<endl;
    cout<<"3. Pointer subscript notation:"<< vPtr[4]<<endl;
    cout<<"4. Pointer/offset notation: "<<*(vPtr + 4)<<endl;
 //(I) What address is referenced by vPtr + 3? What value is stored at that location?
 // (vptr+3) points to memory of 4th element which is 1002512 (if in decimal)
 //100250C (if in hexadecimal).    value stored at that location is 8.
   cout<<"The address of vPtr + 3 :"<<(vPtr + 3) <<endl;
   cout<<"The values storedat vPtr + 3 :"<<*(vPtr + 3) <<endl;
 //(J) Assuming that vPtr points to values[4], what address is referenced by
 //vPtr -= 4? What value is stored at that location?
 //vPtr references Address: 1002500; value stored is 2 ;
 vPtr=&A[4];   //vPtr points to values[4],
 unsigned int *cpy = vPtr;  //cpy points to 0th element
 cout<<"The address of vPtr -= 4 :"<<(vPtr -= 4)<<endl; 
 // vPtr=vPtr-4(move backward 4 elent positions), it points to 0th element.
 vPtr=cpy;   // cpy copied to  vPtr

cout<<"The values stored at vPtr -= 4 :"<<*(vPtr -= 4)<<endl;
//prints the value of 0th element.

cout<<"Press any key to continue . . .";
   
    
    
}

At run time the storage addresses may vary.

Array

A[0]

A[1]

A[2]

A[3]

A[4]

Address(in decimal)

1002500

1002504

1002508

1002512

1002516

Address(hexa)

1002500

1002504

1002508

100250C

1002510

Values

2

4

6

8

10

Since each int is 4 bytes size, next element address=current address +4

vPtr ++ or vPtr=vPtr+1, vPtr advances to address of next element .i.e of vptr= 1002500, then vPtr++ will point to 1002504(address of next element) , not point to 1002501


Related Solutions

Write the following task in C++1) Write the definition of a function numOccurrences thatsearches...
Write the following task in C++1) Write the definition of a function numOccurrences that searches for a character in a character array and returns the number of times it occurs in the array. The function has three formal parameters: a char array array, an int variable arraySize representing the size of the array, and a character variable letter representing the character to be searched for in the array.2) Assume the array numbers is an int array of size 10 and...
The following statements are from Sanderson Farms Inc.’s annual report for 2017. Task 1: Perform a...
The following statements are from Sanderson Farms Inc.’s annual report for 2017. Task 1: Perform a ratio analysis using at least seven ratios of your choosing. Explain what these ratios tell you about the company and for each one provide at least ONE recommendation of an action that management could take to improve that ratio. Task 2: Create a cash flow statement for the company for 2017. Sanderson’s Farm Inc. and Subsidiaries CONSOLIDATED BALANCED SHEETS October 31, 2017 2016 Assets...
Write a program fragment (not a complete program) which will perform the following task: The int...
Write a program fragment (not a complete program) which will perform the following task: The int variable m currently contains the number of minutes a basketball player played in their last game. Use an IF statement(s) to print out an appropriate message based on the following: If m is from 35 to 48, print the message "very tired" If m is from 10 to 34, print the message "little tired" If m is from 1 to 9, print the message...
Write a C++ code to perform the following steps: 1. Ask the user to enter two...
Write a C++ code to perform the following steps: 1. Ask the user to enter two whole numbers number1 and number2 ( Number 1 should be less than Number 2) 2. calculate and print the sum of all even numbers between Number1 and Number2 3. Find and print all even numbers between Number1 and Number2 4. Find and print all odd numbers between Number1 and Number2 5. Calculate and print the numbers and their squares between 2 and 20 (inclusive)...
Write a C++ code to perform the following steps: 1. Ask the user to enter two...
Write a C++ code to perform the following steps: 1. Ask the user to enter two whole numbers number1 and number2 (Number1 should be less than number2) 2. Calculate and print the sum of all even numbers between Number1 and Number2 3. Find and print all even numbers between Number1 and Number2 4. Find and print all odd numbers between Number1 and Number2 5. Calculate and print the numbers and their squares between 1 and 20 (inclusive) 6. Calculate and...
C++ Assignment 1) Write a C++ program specified below: a) Include a class called Movie. Include...
C++ Assignment 1) Write a C++ program specified below: a) Include a class called Movie. Include the following attributes with appropriate types: i. Title ii. Director iii. Release Year iv. Rating (“G”, “PG”, “PG-13”, etc) - Write code that instantiates a movie object using value semantics as text: - Write code that instantiates a movie object using reference semantics: - Write the print_movie method code: - Write Constructor code: - Write Entire Movie class declaration
Write a C++ program to perform the addition of two hexadecimal numerals each with up to...
Write a C++ program to perform the addition of two hexadecimal numerals each with up to 10 digits. If the result of the addition is more than 10 digits long, then simply give the output message "Addition overflow" and not the result of the addition. Use arrays to store hexadecimal numerals as arrays of characters. Input Notes: The program reads two strings, each a sequence of hex digits (no lowercase, however) terminated by a "q". Example: 111q AAAq Output Notes...
C++ questions, please make sure to dividet he program into functions which perform each major task,...
C++ questions, please make sure to dividet he program into functions which perform each major task, A leap year is defined as any calendar year which meets the following criteria: If the year is not divisible by 4, it is a common year If the year is not divisible by 100, it is a leap year If the year is not divisible by 400, it is a common year Otherwise it is a leap year For your program you will...
C++ questions, Please make sure to divide your program into functions which perform each major task....
C++ questions, Please make sure to divide your program into functions which perform each major task. The game of rock paper scissors is a two player game in which each each player picks one of the three selections: rock, paper and scissors. The game is decided using the following logic: ROCK defeats SCISSORS (“smashes”) PAPER defeats ROCK (“wraps”) SCISSORS defeats PAPER (“slices”) If both players choose the same selection the game ends in a tie. Write a program that asks...
In C: Write a complete program that performs the following task: Ask the user for the...
In C: Write a complete program that performs the following task: Ask the user for the number of sequences to display. For each sequence, Ask the user for a starting value Print out the value and double it (multiply by 2). Continue printing and doubling (all on the same line, separated by one space each) as long as the current number is less than 1000, or until 8 numbers have been printed on the line. You may assume that the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT