My Programming Lab - Ch10

25 July 2022
4.7 (114 reviews)
25 test answers

Unlock all answers in this set

Unlock answers (21)
question
Given a char variable c that has already been declared, write some code that repeatedly reads a value from standard input into c until at last a 'Y' or 'y' or 'N' or 'n' has been entered.
answer
do{ cin >> c; }while (!(c == 'Y' || c == 'y' || c=='N' || c == 'n'));
question
Declare a char array named line suitable for storing C-strings as large as 50 characters, and write a statement that reads in the next line of standard input into this array. (Assume no line of input is 50 or more characters.)
answer
char line[50]; cin.get(line, 50);
question
Declare a two-dimensional character array named "gasgiants" that is capable of storing the names of the gas giant planets of our solar system: jupiter, saturn, uranus, and neptune. Do not make the array any larger than it needs to be.
answer
char gasgiants[4][8];
question
Declare and initialize a two-dimensional character array named "stooges" to store the names "moe", "larry" and "curly" (in that order). Do not make the array any larger than it needs to be.
answer
char stooges[3][6] = {"moe", "larry", "curly"};
question
Assume that scotus is a two-dimensional character array that stores the family names (last names) of the nine justices on the Supreme Court of the United States. Assume no name is longer than twenty characters. Assume that scotus has been initialized in alphabetical order. Write some code that will print each to standard output , each on a separate line with no other characters
answer
for (int i = 0; i < 9; i++){ cout << scotus[i] << "n"; }
question
Write an expression that evaluates to true if and only if the C-string s equals the C-string "end"
answer
strcmp(s, "end") == 0
question
Write an expression that evaluates to true if and only if the C-string s does not equal "end"
answer
!strcmp(s, "end") == 0
question
Write an expression that evaluates to true if the value of the C-string s1 is greater than the value of C-string s2
answer
strcmp(s1, s2) > 0
question
Given the string variables name1 and name2, write a fragment of code that assigns the larger of the two to the variable first (assume all three are already declared and name1 and name2 assigned values).
answer
if (strcmp(name1, name2) > 0) first = name1; else first = name2;
question
Write a function max that has two C string parameters and returns the larger of the two. Instructor Notes: Hint: Use char* as the type for the c-strings here.
answer
char* max(char *number1, char *number2){ if (strcmp(number1, number2) > 0) return number1; else return number2; }
question
Write a function min that has three C string parameters and returns the smallest. Instructor Notes: Hint: Use char* as the type for the c-strings here.
answer
char* min(char *string1, char *string2, char *string3) { if(strcmp(string1, string2) > 0){ if (strcmp(string2, string3) > 0) return string3; else return string2; } else{ if(strcmp(string1, string3) > 0) return string3; else return string1; } }
question
Assume that name is a variable of type string that has been assigned a value. Write an expression whose value is the first character of the value of name. So if the value of name were "Smith" the expression 's value would be 'S'.
answer
name[0]
question
Assume that name is a variable of type string that has been assigned a value. Write an expression whose value is the last character of the value of name. So if the value of name were "Blair" the expression's value would be 'r'.
answer
name[name.length() -1]
question
Assume that name is a variable of type string that has been assigned a value. Write an expression whose value is the second character of the value of name. So if the value of name were "Green" the expression 's value would be 'r'
answer
name[1]
question
Assume that word is a variable of type string that has been assigned a value . Write an expression whose value is a string consisting of the first three characters of the value of word . So if the value of word were "dystopia" the expression 's value would be "dys".
answer
word.substr(0, 3)
question
Assume that word is a variable of type string that has been assigned a value . Write an expression whose value is a string consisting of the last three characters of the value of word . So if the value of word were "biggest" the expression 's value would be "est".
answer
word.substr(word.length() - 3, 3)
question
Assume that given , middle and family are three variables of type string that have been assigned values . Write an expression whose value is a string consisting of the first character of given followed by a period followed by the first character of middle followed by a period followed by the first character of family followed by a period: in other words, the initials of the name . So if the values of these three variables were "John" "Fitzgerald" "Kennedy", then the expression 's value would be "J.F.K.".
answer
given.substr(0, 1) + "." + middle.substr(0,1) + "." + family.substr(0,1) + "."
question
Assume that sentence is a variable of type string that has been assigned a value . Assume furthermore that this value is a string consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." Assume that there is another variable declared , firstWord , also of type string . Write the statements needed so that the first word of the value of sentence is assigned to firstWord . So, if the value of sentence were "Broccoli is delicious." your code would assign the value "Broccoli" to firstWord
answer
firstWord = sentence.substr(0, sentence.find(' ', 0));
question
Assume that word is a variable of type string that has been assigned a value . Assume furthermore that this value always contains the letters "dr" followed by at least two other letters. For example: "undramatic", "dreck", "android", "no-drip". Assume that there is another variable declared , drWord , also of type string . Write the statements needed so that the 4-character substring word of the value of word starting with "dr" is assigned to drWord . So, if the value of word were "George slew the dragon" your code would assign the value "drag" to drWord
answer
drWord = word.substr(word.find("dr", 0), 4);
question
Assume that sentence is a variable of type string that has been assigned a value . Assume furthermore that this value is a string consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." Assume that there is another variable declared , secondWord , also of type string . Write the statements needed so that the second word of the value of sentence is assigned to secondWord . So, if the value of sentence were "Broccoli is delicious." your code would assign the value "is" to secondWord
answer
secondWord = sentence.substr(sentence.find(" ") + 1); secondWord = secondWord.substr(0, secondWord.find(" "));
question
Write a sequence of statements that finds the first comma in the string line , and assigns to the variable clause the portion of line up to, but not including the comma. You may assume that an int variable pos , as well as the variables line and clause , have already been declared
answer
clause = line.substr(0, line.find(',') );
question
Given the string variable address , write an expression that returns the position of the first occurrence of the string "Avenue" in address
answer
address.find("Avenue")
question
Write an expression that results in a string consisting of the third through tenth characters of the string s
answer
s.substr(2, 8)
question
Write an expression that whose value is the fifth character of the string name
answer
name[4]
question
10.15: Character Analysis Write a program that reads the contents of a file named text.txt and determines the following: The number of uppercase letters in the file The number of lowercase letters in the file The number of digits in the file Prompts And Output Labels. There are no prompts-- nothing is read from standard in, just from the file text.txt. Each of the numbers calculated is displayed on a separate line on standard output , preceded by the following prompts (respectively): "Uppercase characters : ", "Lowercase characters : ", "Digits: ". Input Validation. None.
answer
#include #include #include using namespace std; int main() { char ch; int uppercase = 0; int lowercase = 0; int digit = 0; ifstream input; input.open("text.txt"); while (input >> ch){ if(isupper(ch)) uppercase++; if(islower(ch)) lowercase++; if(isdigit(ch)) digit++; } cout << "Uppercase characters: " << uppercase << endl; cout << "Lowercase characters: " << lowercase << endl; cout << "Digits: "<< digit << endl; lowercase++; input.close(); return 0; }