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

2.20 Warm up: Variables, input, and casting (C++) (1) Prompt the user to input a

ID: 3786123 • Letter: 2

Question

2.20 Warm up: Variables, input, and casting (C++)

(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. (Submit for 2 points).


(2) Extend to also output in reverse. (Submit for 1 point, so 3 points total).


(3) Extend to cast the double to an integer, and output that integer. (Submit for 2 points, so 5 points total).

=======and here is my code ===========

#include <stdio.h>
#include <string.h>   

int main(void) {
int userInt = 0;
double userDouble = 0.0;
char character;
char string[10];

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

printf("Enter integer: ");
scanf("%d ", &userInt);
printf("Enter double: ");
scanf("%lf ", &userDouble);
printf("Enter character: ");
scanf(" %c ", &character);
printf("Enter string: ");
scanf("%sz ", string);

// FIXME (2): Output the four values in reverse
  
printf("%d %.2lf %c %s ", userInt, userDouble, character, string);
printf("%s %c %.2lf %d ", string, character, userDouble, userInt);

return 0;

}

Explanation / Answer

//Modified program is given below:

#include <stdio.h>
#include <string.h>

int main(void) {
   int    userInt    = 0;
   double userDouble = 0.0;
   char character;
   char string[10];

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

   printf("Enter integer:");
   scanf("%d", &userInt);
   printf("Enter double:");
   scanf("%lf", &userDouble);
   printf("Enter character:");
   scanf(" %c", &character);
   printf("Enter string:");
   scanf("%sz", string);

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

   printf("%d %.2lf %c %s ", userInt, userDouble, character, string);
   printf("%s %c %.2lf %d ", string, character, userDouble, userInt);
   printf("%.2lf cast to an integer is %d ", userDouble, (int)userDouble);   //(int)userDouble is used for type Cast

return 0;

}

/*Output

sh-4.2$ main                                                                                                                                                             Enter integer:99                                                                                                                                                         Enter double:3.77                                                                                                                                                        Enter character:z                                                                                                                                                        Enter string:Howdy                                                                                                                                                       99 3.77 z Howdy                                                                                                                                                          Howdy z 3.77 99                                                                                                                                                          3.77 cast to an integer is 3                                                                                                                                             sh-4.2$ */