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

C program: (1) Prompt the user to input an integer, a double, a character, and a

ID: 3920713 • Letter: C

Question

C program:

(1) Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output those four values on a single line separated by a space. (2 pts)

Note: This zyLab outputs a newline after each user-input prompt. For convenience in the examples below, the user's input value is shown on the next line, but such values don't actually appear as output when the program runs.


(2) Extend to also output in reverse. (1 pt)


(3) Extend to cast the double to an integer, and output that integer. (2 pts)

Code to edit:

#include <stdio.h>

int main(void) {
int userInt;
double userDouble;
// FIXME: Define char and string variables similarly

printf("Enter integer: ");
scanf("%d", &userInt);

// FIXME (1): Finish reading other items into variables, then output the four values on a single line separated by a space


// FIXME (2): Output the four values in reverse


// FIXME (3): Cast the double to an integer, and output that integer

return 0;
}

Explanation / Answer

#include int main(void) { int userInt; double userDouble; char userChar; char userString[1000]; printf("Enter integer: "); scanf("%d", &userInt); // FIXME (1): Finish reading other items into variables, then output the four values on a single line separated by a space printf("Enter double: "); scanf("%lf", &userDouble); printf("Enter character: "); scanf(" %c", &userChar); printf("Enter string: "); scanf("%s", userString); // FIXME (2): Output the four values in reverse printf("%d %.2lf %c %s ", userInt, userDouble, userChar, userString); printf("%s %c %.2lf %d ", userString, userChar, userDouble, userInt); // FIXME (3): Cast the double to an integer, and output that integer printf("%.2lf cast to an integer is %d ", userDouble, (int)userDouble); return 0; }