Question

In: Electrical Engineering

5. Given the following declarations and assignments, what do these expressions evaluate to? int a1[10] =...

5. Given the following declarations and assignments, what do these expressions evaluate to?

int a1[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

int *p1, *p2;

p1 = a1+3;

p2 = &a1[2];

(a) *(a1+4) (b) a1[3] (c) *p1 (d) *(p1+5) (e) p1[-2]

(f) *(a1+2) (g) a1[6] (h) *p2 (i) *(p2+3) (j) p2[-1]

Solutions

Expert Solution

Dear Student,

The given line of statements are in C /C++ programming language.

int a1[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

This line of code states that a 1-dimensional integer array named a1 of 10 elements has been declared and initialized with numbers from 9 to 0.

Array - It is a group of elements of same data types. Example integer array can contain elements which are integers only. Index of an array gives the position of elements in that array. It starts with 0 and wriiten in third brackets [ ].

a1[0] = 9 ----- The first element

a1[1] = 8 ----- The second element and so on upto last element

a1[9] = 0 ----- The last element

Last index is always 1 less than the size of array

Last index = size of array - 1 = 10 - 1 = 9

int *p1, *p2;

This code declares two pointer variables named p1​​​​​ and p2​​​​​. Pointer variables are the variables that stores address of other variables and we can access the value of that variable through this pointer variable.

p1 = a1+3;

This code stores address in pointer variable p1. a1 gives the base address of array a1 i.e. address of first element a1[0]. When 3 is added to it, (a1 + 3) gives address of address of element a1[0+3] i.e. a1[3] element.

Hence p1 stores address of element a1[3].

p2 = &a1[2];

This code stores address of element a1[2] in pointer variable p2. & is used to provide the address of any variable.

a) *(a1+4)  The expression (a1 + 4) gives the address of a1[0+4] = a1[4]. Now the * operator gives access to the value stores in address of a1[4]. The value of a1[4] is 5

Hence *(a1 + 4) = 5

b) a1[3] = 6

c) *p1 It gives the value store in pointer variable p1. p1 stores the address of element a1[3].

Hence *p1 = 6

d) *(p1+5) p1 stores the address of element a1[3]. So (p1 + 5) gives the address of element a1[3+5] = a1[8]. Now * operator on (p1 + 5) gives the value of a1[8].

Hence *(p1 + 5) = 1

e) p1[-2]   Index of pointer variable start from 0 and goes in reverse direction. p1 stores address of element a1[3].

So p1[0] = a1[3]

p1[-1] = a1[2]

p1[-2] = a1[1]

p1[-3] = a1[0]

Hence p1[-2] = 8

f) *(a1+2) = a1[0+2] = a1[2] = 7

Hence *(a1 + 2) = 7

g) a1[6] = 3

h) *p2  p2 stores the address of a1[2]. On applying* operator in it gives the value of a1[2] i.e. 7

Hence *p2 = 7

i) *(p2+3) p2 stores address of a1[2], so (p2 + 3) gives address of a1[2+3]=a1[5]. On applying * operator it gives value of a1[5].

Hence *(p2 + 3) = 4

j) p2[-1]  p2 stores address of a1[2]. Therefore

p2[0] = a1[2]

p2[-1] = a1[1]

p2[-2] = a1[0]

Hence p2[-1] = 8

Please give a positive rating to this answer. Thank you !


Related Solutions

Evaluate the following expressions: (int)(18.357 * 100)/10 result: _____________ (int)18.357 * 100/10.0 result: _____________ int x...
Evaluate the following expressions: (int)(18.357 * 100)/10 result: _____________ (int)18.357 * 100/10.0 result: _____________ int x = 5; int y = 8; int z = ++x - y--; x=_____ y=_____ z=_____ int a = 20; int b = 10; a -= b - 15; a=_____ b=_____   CSE 118 Fundamentals of programming Java 14.0
#Python 5. Write function called evaluate() that evaluates the following Python expressions or assignments as specified:...
#Python 5. Write function called evaluate() that evaluates the following Python expressions or assignments as specified: Request input from the user for three variables (floating-point or integer) x, y, z, and myAverage. If the average of the first three numbers equals the fourth number, print 'Your average is correct.'. If not print 'Your average is not correct'. and print the correct average. Print the largest value among x, y, and z. Print the minimum value of x, y, y. >>>...
Evaluate the following expressions. The answer must be given as a fraction, NO DECIMALS. If the...
Evaluate the following expressions. The answer must be given as a fraction, NO DECIMALS. If the answer involves a square root it should be entered as sqrt . For instance, the square root of 2 should be written as sqrt(2). If tan ( θ ) = − 5/3 and sin ( θ ) > 0 , then find a) sin(θ)=sin(θ)=     (b) cos(θ)=cos(θ)=     (c) sec(θ)=sec(θ)=     (d) csc(θ)=csc(θ)=     (e) cot(θ)=cot(θ)=
Regular Expressions. What are they? What do they do for us?
in c#Regular Expressions. What are they? What do they do for us? What is their power and what challenges to they present to writing solid code?
#include <stdio.h> int main () { int value= 10; int value2= 5; value = value %2;...
#include <stdio.h> int main () { int value= 10; int value2= 5; value = value %2; printf("he FInal =value=%d\n", value); value +=3; printf("he FInal =value=%d\n", value); value ++; printf("he FInal =value=%d\n", value); value= ++value2; printf("he FInal =value=%d\n", value); value= value2--; printf("he FInal =value=%d\n", value); } what is output explain each print statement? exlain why?
Given the root C++ code: void sort() {    const int N = 10;    int...
Given the root C++ code: void sort() {    const int N = 10;    int x[N];    for(int i = 0; i < N; i++)    {        x[i] = 1 + gRandom-> Rndm() * 10;        cout<<x[i]<<" "; }    cout<<endl;    int t;       for(int i = 0; i < N; i++)    {    for(int j = i+1; j < N; j++)    {        if(x[j] < x[i])        {   ...
Given the following variable declarations: const size_t n = 50; Write the declaration of an array...
Given the following variable declarations: const size_t n = 50; Write the declaration of an array of pointers to n memory blocks containing 16-bit signed integer values. Use 'x' for the name of the variable. (Don't forget the semi-colon at the end of the declaration!)
for C++ pointers: a. Given int arr [10], *ap = &arr[2]; what is the result of...
for C++ pointers: a. Given int arr [10], *ap = &arr[2]; what is the result of (ap - arr)? b. Given int val = 10, *x = &val, *y; y = x; *y=100; what is the output of cout << *x << *y;? c. Given int arr [10], *ap = arr; what element does ap point to after ap +=2; ?
x is 5, what is the result of the following Boolean expressions: 1. x != 0  ...
x is 5, what is the result of the following Boolean expressions: 1. x != 0   2. x > 0   3. x != 0 4. x > 0 5. (x >= 0) || (x < 0) 6. (x != 1) == !(x == 1) 7. (true) && (3 > 4) True or False? Please explain how you got your answers. I've been struggling with Boolean expressions so I'd like a little bit more of an explanation as to why each...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num ==...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num == 0) return 0; else if (num > 100) return -1; else     return num + tq( num – 1 ); } Group of answer choices: 0 4 -1 10 Q2: Given that values is of type LLNode<Integer> and references a linked list (non-empty) of Integer objects, what does the following code do if invoked as mystery(values)? int mystery(LLNode<Integer> list) {    if (list.getLink() ==...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT