Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Determine if it is syntactically correct and if it is legal code. If it is, answ

ID: 3748850 • Letter: D

Question

Determine if it is syntactically correct and if it is legal code. If it is, answer the question about the value(s) of certain expressions and/or variables at the end of the code.

1.

void getPowers(int x, int xSqr, int xCube){

            xSqr = x * x;

            xCube = x * xSqr;

}

int main(){

            int x, xSqr, xCube;

            x = 3;

            getPowers(x, xSqr, xCube);

}

What are x, xSqr, xCube?

2.

void getPowers(int x, int *pxSqr, int *pxCube){

            *pxSqr = x * x;

            *pxCube = x * *pxSqr;

}

int main(){

            int x, xSqr, xCube;

            x = 3;

            getPowers(x, xSqr, xCube);

}

What are x, xSqr, xCube?

3.

void getPowers(int x, int *pxSqr, int *pxCube){

            *pxSqr = x * x;

            *pxCube = *pxSqr * x;

}

int main(){

            int x, *pxSqr, *pxCube;

            x = 3;

            getPowers(x, pxSqr, pxCube);

}

What are x, *pxSqr, *pxCube?

4.

void getPowers(int x, int *pxSqr, int *pxCube){

            *pxSqr = x * x;

            *pxCube = *pxSqr * x;

}

int main(){

            int x, xSqr, xCube;

            x = 3;

            getPowers(x, &xSqr, &xCube);

}

What are x, xSqr, xCube?

Explanation / Answer

1.

x = 3; // Initially x value is 3

getPowers(x, xSqr, xCube); // calling function

void getPowers(int x, int xSqr, int xCube) // Here x is 3

            xSqr = x * x = 3 * 3 = 9

            xCube = x * xSqr = 3*9=27

Therefore x= 3, xSqr = 9 and xCube = 27

2.

x = 3; // Initially x value is 3

getPowers(x, xSqr, xCube); // calling function

void getPowers(int x, int *pxSqr, int *pxCube) // Here raises an error invalid conversion from 'int' to 'int*' because  xSqr, xCube non pointer variables where as  *pxSqr, *pxCube are pointer variables.

it is syntactically not correct

3.

int x, *pxSqr, *pxCube; // *pxSqr and *pxCube are pointer variables

x = 3;

getPowers(x, pxSqr, pxCube); // Since *pxSqr and *pxCube are pointer variables we pass through address so it is not legal

Therefore it is not legal

4.

int x, xSqr, xCube; // normal variables

            x = 3;

            getPowers(x, &xSqr, &xCube); // We can do pass by address called function use pointers to accept value

void getPowers(int x, int *pxSqr, int *pxCube) // *pxSqr and *pxCube contain address and x is 3

*pxSqr = x * x; // *pxSqr = 3*3 = 9

*pxCube = *pxSqr * x = 3*9 = 27

Therefore x= 3, *pxSqr = 9 and *pxCube = 27

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote