CSCI 240 - Quiz 10
1. (2) A function has the following prototype:
void fn(char [], int n);
Rewrite the prototype using pointer notation (no [])
2. (2) Assuming an array declared as
char ar[80];
Write a single line of code, using pointer notation with pointer arithmetic, to set the third element (the one with two elements in front of it) in the array to the value 'A'.
3. (4) Fill in the blanks in this code:
void main()
char choice;
menu(___); // pass choice so menu can alter it
// end of main
void menu(____________); // menu needs 1 argument so it can alter that argument
{
do
{
cout << "\nenter a or b";
cin >> _______; // code this so the char is stored in main's choice
}
while (________________________); // the user-entered value is invalid
}
More than 1 answer may be correct. Circle all that are correct
4. (2) How do C++ string library functions tell how long a string is?
a. by the declared length of the string
b. by the terminating null character after the last character in the string
c. by a length argument passed to the function
d. by a length variable stored in the string
5. (2) What is the unsubscripted name of an array?
6. If you declare a char array as
char aName[20];
a. (2) if you try to cout << aName what is the maximum number of displayable characters that you could see? (Assume that aName has been properly initialized with a legal string.)
b. (2) assuming you have the largest legal string that will fit in this array, what would the for loop index variable range between if you wanted to print the all characters one per line? Write a number in each blank in the first line and fill in correct answers in the second line.
for (i = ____; i < ______; i++) cout << "___" << __________;
c. (2) assuming the string does not fill the array, what would the for loop index variable range between if you wanted to print the characters one per line? (Hint: use a string function.)
for (i = _____; i < _______________; i++) cout (as above);
7. Suppose you have a string "Hello, world" stored in a char array named greeting.
a. (2) Write ONE statement to change the string to just "Hello" by altering a single character.
b. (2) Now change greeting (as it exists after part (a) by executing TWO statements, each of which alters just one character, to change the "Hello" (from part a) to "Jello, world". Assume that no other changes have taken place inside greeting.
8. (2) Using pointer arithmetic notation, print all the characters in a string, one per line. (No subscripts; but there are a couple of acceptable answers.)
9. (2) Given the following:
char name[80] = "Cat"; char *ptr;
Which of the following are legal?
a. ptr = name;
b. ptr = &Cat;
c. name = &ptr;
d. name = ptr;
e. name = "Dog";