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

What will be the output of the following code snippets and briefly explain the l

ID: 3850086 • Letter: W

Question

What will be the output of the following code snippets and briefly explain the logic: (C PROGRAM PLEASE)

5.
int main(void) { int a = 3, b = 4, c; c = b –a; switch (c) { case 1 || 2:   printf("God give me an opportunity to change things");   break; case a || b:

printf("God give me an opportunity to run my show");   break; }

return 0;

}

6.
#include <stdio.h>

int main(void) { int i = 1; switch (i - 2) { case -1:   printf(" Feeding fish"); case 0:   printf(" Weeding grass"); case 1:   printf(" mending roof"); default:   printf(" Just to survive"); }


return 0;

}


7. Suppose, x is a variable of type int, y is a variable of type float, and z is a variable of type double. What is the type of the expression x * y / z ?

8. Write statements to accomplish the following: a. Define table to be an integer array and to have 3 rows and 3 columns. Assume the symbolic constant SIZE has been defined to be 3. b. How many elements does the array table contain? Print the total number of elements. c. Use a for repetition statement to initialize each element of table to the sum of its subscripts. Assume the integer variables x and y are defined as control variables.

9. For the following program, state the scope (either function scope, file scope, block scope or function-prototype scope) of each of the following elements. • The variable x in main. • The variable y in cube. • The function cube. • The function main. • The function prototype for cube. • The identifier y in the function prototype for cube.

int cube(int y); int main(void) { int x; for (x = 1; x <= 10; ++x)   printf("%d ", cube(x)); } int cube(int y) { return y * y * y; }

10. Give the function header(prototypes) for each of the following functions. a. Function hypotenuse that takes two double-precision floating-point arguments, side1 and side2, and returns a double-precision floating-point result. b. Function smallest that takes three integers, x, y, z, and returns an integer. c. Function instructions that does not receive any arguments and does not return a value. [Note: Such functions are commonly used to display instructions to a user.] d. Function intToFloat that takes an integer argument, number, and returns a floating- point result.

11. Find the error in each of the following program segments and correct the error. a. #define SIZE 100; b. int b[10] = { 0 }, i; for (i = 0; i <= 10; ++i) {   b[i] = 1;


c. #define VALUE = 120;

d. int a[2][2] = { { 1, 2 }, { 3, 4 } }; a[1, 1] = 5;

12. Find the error (if any) in the following code and specify how to correct it or predict the output along with brief explanation:

a. void product(void) { int a, b, c, result; printf("%s", "Enter three integers: ") scanf("%d%d%d", &a, &b, &c); result = a * b * c; printf("Result is %d", result); return result; }


13. Compare Iteration with Recursion.

14. Answer each of the following. Assume that single-precision floating-point numbers are stored in 4 bytes, and that the starting address of the array is at location 1002500 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 10 elements, and initialize the elements to the values 0.0, 1.1, 2.2, ..., 9.9. Assume the symbolic constant SIZE has been defined as 10.

b. Define a pointer, nPtr, that points to an object of type float.

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

d. number with 1 position of precision to the right of the decimal point.

e. Give two separate statements that assign the starting address of array numbers to the pointer variable nPtr.

f. Print the elements of array numbers using pointer/offset notation with the pointer nPtr. 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 4 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 points to the beginning of array numbers, what address is referenced by nPtr + 8? What value is stored at that location?

j. Assuming that nPtr points to numbers[5], what address is referenced by nPtr –= 4? What’s the value stored at that location?

15. Answer each of the following. Assume that unsigned integers are stored in 2 bytes and that the starting address of the array is at location 1002500 in memory.

a. Define an array of type unsigned int called values with five elements, and initialize the elements to the even integers from 2 to 10. Assume the symbolic constant SIZE has been defined as 5.


b. Define a pointer vPtr that points to an object of type unsigned int.

c. Print the elements of array values using array subscript notation. Use a for statement and assume integer control variable i has been defined.

d. Give two separate statements that assign the starting address of array values to pointer variable vPtr.

e. Print the elements of array values using pointer/offset notation.

f. Print the elements of array values using pointer/offset notation with the array name as the pointer.

g. Print the elements of array values by subscripting the pointer to the array.

h. Refer to element 5 of array 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 vPtr points to values[4], what address is referenced by vPtr -= 4? What value is stored at that location

Explanation / Answer

5 Answer)

The program will not compile due to the statement case a || b: It is so because in writing a case we need to write a constant value instead of a variable. Hence the program will not compile.

6 Answer)

The program will print following lines

Feeding fish
Weeding grass
mending roof
Just to survive

Explanation: It is so because the statement switch(i - 2) will evaluate to switch( -1). Hence program will match the case -1 from the given cases when its found will execute its body. So here case -1 is found and its body is executed printing the line "Feeding fish" but at the same time the program will continue executing all other print statements of remaining cases, this is so because there is no break statement in any of the cases. Hence if we need to execute a single matching case then we must have to put a break statement after its body.

7 Answer)

The output is of type float because it has higher precision than both int and double and since one of the input is of type float hence compiler implicitly converts the result to float type assuming that high precision is maintained in the result.

8 Answer)

a)

int SIZE = 3;
int table[SIZE][SIZE];

b) The array table contains total of 3 x 3 = 9 elements.

printf("The total number of elements in array table is: %d", (SIZE * SIZE));

c)

int x, y;

for(x = 0; x < SIZE; x++)

{

for(y=0; y < SIZE; y++)

table[x][y] = x + y;

}

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