a) The variable userChar is a char and encodrdVal is an int. What will encodedVa
ID: 3812696 • Letter: A
Question
a) The variable userChar is a char and encodrdVal is an int. What will encodedVal be for ech userChar value?
switch (userChar)
{
case 'A':
encodedVal = 1;
break;
case 'B':
encodedVal = 2;
break;
case 'C':
case 'D':
encodedVal = 4;
break;
case 'E':
encodedVal = 5;
break;
case 'F':
encodedVal = 6;
break;
default:
encodedVal = -1;
break;
}
b) If the following fragments were part of a complete program, what would they print without running the code?
1)
int x = 0;
while (++x < 3)
{
printf("%4d", x);
}
2)
int x =100;
while (x++ < 103)
{
printf("%4d ", x);
}
3)
char ch = 's';
while (ch < 'w')
{
printf("%c", ch);
ch++;
}
c) What will the following program print without running the code?
#include <stdio.h>
int main (void)
{
int i = 0;
while (i < 3) {
switch (i++) {
case 0: printf ("fat");
case 1: printf ("hat");
case 2: printf ("cat");
defult: printf ("oh no!");
}
putchar (' ');
}
return 0;
}
Explanation / Answer
Please give a thumbs up, if it is helpful for you!!
a)
for userChar= 'A' , the encodedVal is 1.
for userChar= 'B' , the encodedVal is 2.
for userChar= 'C' , the encodedVal is 4.
for userChar= 'D' , the encodedVal is 4.
for userChar= 'E' , the encodedVal is 5.
for userChar= 'F' , the encodedVal is 6.
for any other userChar value the encodedVal is -1.
b)
1)
output: 1 2
explanation: printf("%4d", x) it indent each output to 4 places.
2)
output:
100
102
103
3)
output:
stuv
explanation: print each letter from 's' to less than 'w'. i.e stuv
c)
output:
fathatcatoh no!
hatcatoh no!
catoh no!
Explanation: there is no break statements between the case statements so all statements will execute from condition match to the end default.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.