Question: 1. Given the following C++ code char name[8]; strcpy(name, Robert); mark the following statements as yesor no if they precisely output Robert. Note that cout
1. Given the following C++ code
char name[8];
strcpy(name, "Robert");
mark the following statements as yesor no if they precisely output "Robert". Note that
cout << '\0';
is undefined. Consequently, any problem which outputs the NULL character after outputting "Robert" needs to be marked as no.
a. cout << name;
Answer:
b. for(int j = 0; j <= 5; j++)
cout << name[j];
Answer:
c. int j = 0;
while(name[j] != '\0')
cout << name[j++];
Answer:
d. for(int j = 0; j < 8; j++)
cout << name[j];
2. Time to review library function calls for C-strings. Assume the iostream library has been included and that you are using the namespace std. Given the following declaration, create code to perform different actions as described below:
char s1[25] = "pomegranate";
char s2[25];
a. Write a C++ statement that uses a function call to copy the string literal "berry" into s1.
b. Write a C++ statement that stores the length of s1 into the int variable length.
c. Write a C++ statement that uses a function call to copy the contents of c-string s2 into s1.
d. Write C++ statements that outputs the string s1 if s1 is less than or equal to s2, otherwise outputs s2. Hint: to compare two C-strings you must use a function and not <. Comparing two c-strings using the < operator would only compare their positions in memory which is not what you want.
3. Define a two-dimensional int array named temp of three rows and four columns such that the first row is initialized to 1, 0, 16, 13; the second row is initialized to 8, 11, 5, 4; and the third row is initialized to 0, 19, 0, 12.
4-6. Consider the following declarations:
const int CAR_TYPES = 4;
const int COLOR_TYPES = 6;
double sales[CAR_TYPES][COLOR_TYPES];
4. How many total elements (aka components) are there in the sales array?
5. What variable represents the sales corresponding to the first type of car with the first type of color?
6. What variable represents the sales corresponding to the last type of car with the last type of color?
7. Write the C++ definition of a general purpose function which reads from common input in order to fill the contents of a two-dimensional integer array using integer constants NUM_OF_ROWS and NUM_OF_COLS.
8. Write C++ statements which declare the parkingSpots array using integer constants NUM_OF_ROWS and NUM_OF_COLS, and then another statement which calls the function from the previous problem.
9. Define a struct for a recreational vehicle with the following members: year (int), length (float), width (float), height (float), weight (float), make (string), model (string), luxuryStyling (bool), color (string), retailPrice (float).
10. Consider the following statements. Although it may be confusing, it is legal to have name as the identifier of a one struct and separately as a member inside of the course struct.
struct name
{
string first;
string last;
};
struct course
{
char name[50];
int callNum;
int credits;
char grade;
};
struct student
{
name studentName;
int id;
course courses[10];
};
student cs_student;
student classList[100];
course cs_course;
name a_name;
Mark the following statements as valid or invalid. If a statement is invalid, explain why it your own words (compiler error messages are not acceptable).
10. name.last = "Hoa";
Answer:
11. cin >> cs_student.studentName;
Answer:
12. classList[0] = studentName;
Answer:
13. classList[1].id = 36701;
Answer:
14. a_name = classList[15].studentName;
Answer:
15. cs_student.studentName = a_name;
Answer:
16. cout << classList[10] << endl;
Answer:
17. for (int j = 0; j < 100; j++)
classList[j].studentName = a_name;
Answer:
18. Assume the declarations of the previous exercise. Write C++ statements that do the following:
a. Store the following information in cs_course. Do not redeclare cs_course.
name: the string literal "Programming I" (hint: you cannot use = to overwrite the previous value for the name member because c-strings are really character arrays and you cannot assign one array to another, so you must use a c-string function instead)
callNum : 40101
credits : 4
grade : 'A'
b. In the classList array initialize each id to 0.
c. Copy the first component of the array classList into cs_student.
d. Update the id of the last student in the array classList by adding 1000000 to its previous value.
19. Assume that you have the following definition of a struct:
struct partType
{
string partName;
int partNum;
float price;
int quantitiesInStock;
};
Declare an array, inventory, of 100 components of type partType.
20. Assume the definition from the previous exercise:
a. Write C++ code that initializes each component of inventory as follows: partName to "?", partNum to 0, price to -1.0, and quantitiesInStock to 0.
b. Write C++ code that uses a loop to output the members of each element of inventory, with each member separated by a space character and each partType on its own line. Assume that the variable length indicates the number of elements in inventory which might be much less than 100.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
