1. Given the following C++ code char name[8]; strcpy(name, \"Robert\"); mark the
ID: 2246330 • Letter: 1
Question
1. Given the following C++ code
char name[8];
strcpy(name, "Robert");
mark the following statements as ‘‘yes’’or “no” if they precisely output "Robert". Note that
cout << '';
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] != '')
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.
Explanation / Answer
There are multiple questions ..around 20, so I answered 17 questions. As per chegg rules we have to answer only top 5 questions in case of multiple questions, but I answered 17. (Please post remaining as separate question... thank you.. Here are the solutions...)
1)
a. cout << name;
Answer: YES (prints Robert)
b. for(int j = 0; j <= 5; j++)
cout << name[j];
Answer: YES (prints Robert)
c. int j = 0;
while(name[j] != '')
cout << name[j++];
Answer: YES (prints Robert)
d. for(int j = 0; j < 8; j++)
cout << name[j];
Answer: YES (prints Robert)
--------------------------------------------------------------------------------------------------------------------------------------------------
2)
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s1[25] = "pomegranate";
char s2[25];
// a)
strcpy(s1, "berry");
// b)
int length = strlen(s1);
// c)
strcpy(s2, s1);
//d)
if (strcmp(s1,s2) <= 0){
cout<<s1;
}
else{
cout<<s2;
}
cout<<endl;
return 0;
}
OUTPUT: berry
----------------------------------------------------------------------------------------------------------------------------------------------------
3)
int main()
{
int temp[3][4] =
{
{1,0,16,13},
{8,11,5,4},
{0,19,0,12}
};
}
----------------------------------------------------------------------------------------------------------------------------------------------
4)
Total elements = CAR_TYPES * COLOR_TYPES = 4x6 = 24 elements in sales array.
5) double firstVal = sales[0][0];
6) double lastVal = sales[3][5];
-------------------------------------------------------------------------------------------------------------------------------------------------
7)
#include <iostream>
using namespace std;
#define NUM_OF_ROWS 10
#define NUM_OF_COLS 10
void readUserInput(){
int array[NUM_OF_ROWS][NUM_OF_COLS];
//reading data
for(int i=0;i<NUM_OF_ROWS;i++){
for(int j=0;j<NUM_OF_COLS;j++){
cout<<array[i][j];
}
}
}
int main() {
readUserInput();
return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------
8)
#include <iostream>
using namespace std;
#define NUM_OF_ROWS 10
#define NUM_OF_COLS 10
void readUserInput(int array[][NUM_OF_COLS]){
//reading data
for(int i=0;i<NUM_OF_ROWS;i++){
for(int j=0;j<NUM_OF_COLS;j++){
cout<<array[i][j];
}
}
}
int main() {
int parkingSpots [NUM_OF_ROWS][NUM_OF_COLS];
readUserInput(parkingSpots );
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------
9)
struct vehicle
{
int year;
float length;
float width;
float height;
float weight;
string make;
string model;
bool luxurtStyling;
string color;
float retailPrice;
};
--------------------------------------------------------------------------------------------------------------------------------
10) INVALID
correct statement should be 'a_name.last = "Hoa"'
-------------------------------------------------------------------
11) INVALID
It cannot bind outside function... that is outside main declared variable.
--------------------------------------------------------------------------------
12)INVALID
classList is type of struct.. so use classList[0].studentName
-----------------------------------------------------------------------
13)VALID
--------------------------------------------------
14) VALID
-------------------------------------------------------
15) VALID
------------------------------------------------
16) INVALID .... iterate struct and then print.. directly cannot print struct
------------------------------------------------------------------
17) VALID
---------------------------------------------------------------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.