In: Computer Science
For each of the questions that follow, fill in the blanks. Blanks are denoted by --[N.]--, where N is an upper-case Roman numeral.
(a) Complete the following code so that the loop executes 10 times (3) int j = −−[I.]−−;
int i=1;
while ( −−[II.]−− ) {
i = i + −−[III.]−−; }
(b) Complete the following function which sorts C-strings into alphabetic order. (4)
Page 4
void bubbleSort( char names[ ][COLS] , int count) {
int pass , index ;
char temp[COLS];
for ( pass = 0; pass < count; pass++ )
}
for ( index = 0; index < (count −1); index++ ) if ( −−[IV.]−− )
{
}
// swap t h e names
−−[V.]−− −−[VI.]−− −−[VII.]−−
(c) You are given the following incomplete program to test whether a given password is valid or not. The (6) rules for a valid password are given by:
1. A password must contain at least one lowercase letter, one uppercase letter and one decimal digit 2. The password length must be at least 7 characters.
int main() {
char∗ password ;
password = new char[250];
// Read the password
cin >> password;
//Test whether the password is valid
if (isValidPassword(password)) {
cout << ”The password complies with the rules” << endl; }
else
{
cout << ”The password does not comply with the rules” << endl; }
delete [ ] password ; return 0 ;
}
Complete the function to test for a valid password.
bool isValidPassword( const −−[VIII.]−−) {
const int MINLEN = 7;
int lowerCount = 0 , upperCount = 0 , digitCount = 0, index = 0;
bool valid = false ;
−−−[IX.]−− (password[index] != ’\0’) {
if ( −−[X.]−− (password[index]) ) digitCount++;
else if ( −−[XI.]−− (password[index] )) lowerCount++;
else if ( −−[XII.]−− (password[index] )) upperCount++;
index++;
}
Page 5
if ((digitCount > 0) && (lowerCount > 0) && (upperCount > 0) && (strlen(password) >= MINLEN))
valid = true; −−[XIII.]−−
}
(d) Complete the following struct definition. (1)
struct Bus{
−−[XIV.]−− routeName ;
int routeLength ; };
(e) Given a struct (2)
struct Cart {
string material ;
double volume ;
int cap[3]; };
and the following code
int main() {
Cart c = −−[XV.]−−; }
Instantiate c with the following values: • tin
• 70.3
• {1,2,3}
(f) Use the following prompts to define a class Cube that contains information about a cube’s length and volume.
i. Complete the declaration for the Cube class by filling in the missing code statements. The class should (3) contain a public interface to modify and retrieve the length of the cube and to compute its volume.
class Cube {
−−[XVI]−− int length ;
−−[XVII]−− −−[XVIII]−− −−[XIX]−− −−[XX]−−
// access specifier
// access specifier
// setLength function prototype // getLength function prototype //
getVolume function prototype
};
Implement the getVolume function to return the cube’s volume as follows: V = l3, where V and l are (2)
the cube’s length and volume, respectively.
int −−[XXI]−− {
return −−[XXII]−− }
Why is it advisable to not have a variable volume to store the cube’s volume value? (1)
1.) set j = 10 and make i increment until it reaches j value.
int j = 10;
int i = 1;
while(i <= 10){
i = i+1;
}
2.) use strcmp to compare the string and strcpy() to swap string using temp variable.
void bubbleSort( char names[ ][COLS] , int count) {
int pass , index ;
char temp[COLS];
for ( pass = 0; pass < count; pass++ )
for ( index = 0; index < (count −1); index++ ) if (strcmp(names[index],names[index+1] < 0)
{
strcpy(temp, names[index+1]);
strcpy(names[index+1], names[index]);
strcpy(names[index], temp);
}
}
3.) use isdigit() to check for digits, islower() to check for lowercase, isupper() to check for uppercase
int main() {
char∗ password ;
password = new char[250];
// Read the password
cin >> password;
//Test whether the password is valid
if (isValidPassword(password)) {
cout << ”The password complies with the rules” << endl; }
else
{
cout << ”The password does not comply with the rules” << endl; }
delete [ ] password ; return 0 ;
}
Complete the function to test for a valid password.
bool isValidPassword( const char password[]) {
const int MINLEN = 7;
int lowerCount = 0 , upperCount = 0 , digitCount = 0, index = 0;
bool valid = false ;
while (password[index] != ’\0’) {
if ( isdigit(password[index]) ) digitCount++;
else if (islower(password[index] )) lowerCount++;
else if ( isupper (password[index] )) upperCount++;
index++;
}
4.) declare busName as string
struct Bus{
string routeName ;
int routeLength ; };
5.) provide the initialzation value inside {} seperated by ,
struct Cart {
string material ;
double volume ;
int cap[3]; };
and the following code
int main() {
Cart c = {"tin", 70.3, {1,2,3}}; }
6.) decalre lenght under private and setlenght(), getLength(), getVolume() under public.
class Cube {
private:
int length ;
public:
void setLength(int l) ;
int getLength();
int getVolume();
// access specifier
// access specifier
// setLength function prototype // getLength function prototype //
getVolume function prototype
};
make the funtion getVolume() return lenght*length*length
int getVolume() {
return length*length*length; }
it is not advisable to set another variable for volume, because it is a simple calcuation which can done without seperately allocating memory space for it.