147k views
2 votes
Warm up: Variables, input, and casting

(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.
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.
Enter integer: 99 Enter double: 3.77 Enter character: Z Enter string: Howdy 99 3.770000 z Howdy
(2) Extend to also output in reverse.
Enter integer: 99 Enter double:
3.77 Enter character: z Enter string: Howdy 99 3.770000 z Howdy Howdy z 3.770000 99
(3) Extend to cast the double to an integer, and output that integer.
Enter integer: 99 Enter double: 3.77 Enter character: Z Enter string: Howdy 99 3.770000 z Howdy Howdy z 3.770000 99 3.770000 cast to an integer is 3 LAB ACTIVITY 2.29.1: LAB: Warm up: Variables, input, and casting 0/5 main.c Load default template... 1 #include 2 3 int main(void) { 4 int user Int; double userDouble; // FIXME: Define char and string variables similarly printf("Enter integer: \\"); scanf("%d", &user Int); // FIXME
(1): Finish reading other items into variables, then output the four values on a single line separated by a space 19 11 12 13 14 15 16 17 18 // FIXME
(2): Output the four values in reverse // FIXME (3): Cast the double to an integer, and output that integer

User Virag
by
4.7k points

1 Answer

6 votes

Final answer:

The college-level computer science question involves defining variables, prompting user input, outputting values, reversing the output order, and casting a double to an integer using C programming language.

Step-by-step explanation:

To complete the lab activity in C programming, first, you need to define the missing variables for character and string inputs. This can be done with the following code:

char userChar;
char userString[50]; // Assumes a max length of 49 chars for string

Next, prompt the user for input and use scanf to read the different types:

printf("Enter integer: \\");
scanf("%d", &userInt);
printf("Enter double: \\");
scanf("%lf", &userDouble);
printf("Enter character: \\");
scanf(" %c", &userChar); // Note the space before %c to catch any preceding whitespace
printf("Enter string: \\");
scanf("%s", userString); // %s will stop reading at whitespace

For the output, print the values on a single line:

printf("%d %.6f %c %s\\", userInt, userDouble, userChar, userString);

To reverse the output, simply change the order of the variables:

printf("%s %c %.6f %d\\", userString, userChar, userDouble, userInt);

Lastly, to cast the double to an integer and output it, you can use:

int castedDouble = (int)userDouble;
printf("%.6f cast to an integer is %d\\", userDouble, castedDouble);
User Fenec
by
4.9k points