Please cheak and correct my answers: Problem #21: [] ---------------------------
ID: 3552706 • Letter: P
Question
Please cheak and correct my answers:
Problem #21: []
-------------------------------
[Points = 6]
Consider the declarations below:
string T;
char S[21];
cin >> T;
Write the statement(s) to store the value of string T into C-string S, if
string T contains 20 characters.
NOTE: DO NOT use { }.
-------------------------------
Answer: IF(STRLEN(T)<=20){FOR(INT I=0;I<21;I++){S[I]=T[I];}S[I]='';}
----------------
Problem #22: []
-------------------------------
[Points = 6]
Consider the declarations below:
char S[21];
cin >> S;
Write the statement(s) to print out the word SHORT if the length of string S
is 8 or less.
NOTE: DO NOT use { }.
DO NOT insert a new line.
-------------------------------
Previous Answer: IF (STRLEN(S)<=8) COUT<<"SHORT";
----------------
Enter Answer [Hit ENTER to RETAIN]:
-------------------------------
Your Answer: IF (STRLEN(S)<=8) COUT<<"SHORT";
Problem #23: []
-------------------------------
[Points = 4]
Consider the code:
...
int main()
{
ostringstream outS;
double myNum; string myNumeral;
cin >> myNum;
___________________________________________ // Lines go here.
return 0;
}
Use the choices below to construct the code sequence needed to convert
a double value in myNum into to an equivalent string.
A. ostringstream outS;
B. outS << myNum;
C. cin >> myNumeral;
D. myNumeral = outS.str();
E. #include <sstream>
-------------------------------
Answer: D
----------------
Problem #24: []
-------------------------------
[Points = 6]
Consider the declarations below:
int k; // FOR LOOP VARIABLE.
char S[21], T[25];
cin >> S >> T;
int blankCount = 0;
Write ON ONE LINE the loop to count the number of blanks/spaces in string T.
NOTE: DO: Take into account the length of the string.
DO: Use strict inequality.
DO: Use the ++ operator.
DO NOT introduce new variables.
DO NOT use { }.
-------------------------------
Answer: FOR (INT K=0; T[K]!=''; K++) IF(T[K]=="") BLANK++
----------------
Problem #25: []
-------------------------------
[Points = 3]
Write ONE assignment statement to copy all the data in the You record
into the Me record.
struct Person
{
string First;
string Last;
};
struct Four
{
Person Name;
int age;
char gender;
};
Four You, Me;
You.Name.First = "Mickey"; You.Name.Last = "Mouse";
You.age = 15; You.gender = 'X';
---------------------------------------
-------------------------------
Answer: ME=YOU;
----------------
Problem #26: []
-------------------------------
[Points = 3]
Consider the declaration below.
Write the statement to create a Data variable Three that is initialized
to the value 3.
_________________________________________
struct Data
{
int value;
};
Data Two;
-------------------------------
Answer: DATA THREE; THREE.VALUE=3;
----------------
Problem #27: []
-------------------------------
[Points = 2]
The statement below creates a ____________ named You.
struct Two
{
int age;
float salary;
}
You;
Two myBrother = {1.2, 2.95};
-------------------------------
Answer: STRUCT TWO VARIABLE
Problem #28: []
-------------------------------
[Points = 4]
The output from the code below is ___ :
struct Player
{
int points;
int rebounds;
}
You;
Player myBrother = {13, 4};
//-| Update Player You to have twice as many rebounds as myBrother.
_________________________________________ // MISSING
-------------------------------
Answer: YOU.REBOUNDS=2*(MYBROTHER.REBOUNDS);
----------------
Problem #29: []
-------------------------------
[Points = 5]
Write a declaration for a Two record named Giant that initializes
its fields to twice the value of the corresponding Midget field.
struct Two
{
int height;
int weight;
};
Two Midget;
cin >> Midget.height >> Midget.weigth;
-------------------------------
Answer: STRUCT TWO GIANT={2*(MIDGET.HEIGHT),2*(MIDGET.WEIGHT)};
----------------
Problem #30: []
-------------------------------
[Points = 4]
Show the value of record A after the call to function This.
NOTE: show values as {value1,value2}
*************************************
struct Guess
{
char X;
int Y;
};
void This(Guess & Me, Guess & You)
{
Me = You;
}
int main()
{
Guess A = {'Z',7},
B = {'Q',8};
This(A,B);
...
}//main
-------------------------------
Answer: A.X='Q';A.Y=8;
Explanation / Answer
#21 - Don't use {} as per ques
for(int i=0; i<21;i++) S[i]=T[i];S[i]='';
#22 - If possible use lowercase for statements
if(strlen(S)<=8)cout<<"SHORT";
#23 - We will have to use B and D in sequence
B D
#24
for(k=0; T[k]!=''; k++) if(T[k]==' ') blankCount++;
#25
Correct just use proper cases i.e. Me=You;
#26
Data Three={3};
#27
Correct
#28
Correct
#29
Correct - No need to precede with struct
#30 - Correct but question asks to show result between {}
{'Q',8}