In: Computer Science
Answer each of the following. Assume that single-precision floating-point numbers are stored in 8 bytes, and that the starting address of the array is at location 2030100 in memory. Each part of the exercise should use the results of previous parts where appropriate.
a) Define an array of type float called numbers with 5 elements, and initialize the elements to the values 0.11, 0.22, 0.33, …, 0.55. Assume the symbolic constant SIZE has been defined as 5.
b) Define a pointer, nPtr, that points to an object of typefloat.
c) Print the elements of array numbers using array subscript notation. Use a for statement and assume the integer control variable i has been defined. Print each number with 2 position of precision to the right of the decimal point.
d) Give two separate statements that assign the address of last element of array numbers to the pointer variable nPtr.
e) Printthe elements of array numbers using pointer/offset notation with the pointer nPtr.
f) Print the elements of array numbers using pointer/offset notation with the array name as the pointer.
g) Print the elements of array numbers by subscripting pointer nPtr. h) Refer to element 2 of array numbers using array subscript notation, pointer/offset notation with the array name as the pointer, pointer subscript notation with nPtr and pointer/offset notation with nPtr. i) Assuming that nPtr pointsto the end of array numbers (i.e., the memory location after the last element of the array), what addressisreferenced by nPtr - 5? What value is stored at thatlocation? j) Assuming that nPtr points to numbers[5], what address is referenced by nPtr – = 2? What’s the value stored at that location?
a) Define an array of type float called numbers with 5 elements,
and initialize the elements to the values 0.11, 0.22, 0.33, …,
0.55. Assume the symbolic constant SIZE has been defined as
5.
Answer:-----
float numbers[ SIZE ] ={ 0.11, 0.22,
0.33, 0.44, 0.55 };
b) Define a pointer, nPtr, that points to an object of
typefloat.
Answer:-----
float *nPtr;
c) Print the elements of array numbers using array subscript
notation. Use a for statement and assume the integer control
variable i has been defined. Print each number with 2 position of
precision to the right of the decimal point.
Answer:-----
for ( i = 0; i < SIZE; ++i )
printf( "%.2f ", numbers[ i ] );
d) Give two separate statements that assign the address of last
element of array numbers to the pointer variable nPtr.
Answer:------
nPtr = numbers;
nPtr = &numbers[ SIZE ];
e) Print the elements of array numbers using pointer/offset
notation with the pointer nPtr.
Answer:------
for ( i = 0; i < SIZE; ++i )
printf( "%.1f ", *( nPtr + i ) );
f) Print the elements of array numbers using pointer/offset
notation with the array name as the pointer.
Answer:------
for ( i = 0; i < SIZE; ++i )
printf( "%.1f ", *( numbers + i ) );
g) Print the elements of array numbers by subscripting pointer
nPtr.
Answer:------
for( i = 0; i < SIZE; ++i )
printf( "%.1f ", nPtr[ i ] );
h) Refer to element 2 of array numbers using array subscript
notation, pointer/offset notation with the array name as the
pointer, pointer subscript notation with nPtr and pointer/offset
notation with nPtr.
Answer:------
numbers[ 2 ]
*( numbers + 2)
nPtr[ 2]
*( nPtr + 2)
i) Assuming that nPtr points to the end of array numbers (i.e.,
the memory location after the last element of the array), what
address is referenced by nPtr - 5? What value is stored at that
location?
Answer:------
The address is 2030100 - 5 * 8 = 2030060.
No value.
j) Assuming that nPtr points to numbers[5], what address is
referenced by nPtr – = 2? What’s the value stored at that
location?
Answer:------
The address of numbers[ 5 ] is 2030100 + 5*8 = 2030140.
The address of nPtr -= 2 is 2030140 - 2*8 = 2030124
The value at that location is 0.44.