Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can anyone help to answer these following questions? 1.Consider the following member function: int myclass::addition (int a, int b) { int r; r = a

Can anyone help to answer these following questions?

1.Consider the following member function:

int myclass::addition (int a, int b)

{ int r;

r = a + b;

return r;

}

Which of the following could replace the above function with a non-member function and properly call the non-member function?

a. int addition (int a, int b)

{ int r;

r = a + b;

return r;

}

int main()

{

int num1 = 1;

int num2 = 2;

addition(num1, num2);

}

b. int addition (int a, int b)

{ int r;

r = a + b;

return r;

}

int main()

{

myclass foo;

int num1 = 1;

int num2 = 2;

int total = 0;

total = foo.addition(num1, num2);

}

c. int addition (int a, int b)

{ int r;

r = a + b;

return r;

}

int main()

{

int num1 = 1;

int num2 = 2;

int total = 0;

total = addition(num1, num2);

}

----------------------------------------------------------------------------------------------------------------------

2. Provide the required line of code that is missing from this function.

int divide (int a, int b=2)

{

int r;

r=a/b;

//missing statement goes here

}

----------------------------------------------------------------------------------------------------------------------

3. Assume that myList is a previously declared an initialized array. Assume that the variable length holds the total number of elements in the array. You need to modify this statement to reverse the order of the loop:

for (int i = 0; i < length; i++) {

cout << myList[i] << " ";

}

Which of the following accomplishes that goal?

a. for (int i = length - 1; i >=0; i--)

{

cout << myList[i] << " ";

}

b. for (int i = length; i >=0; i--)

{

cout << myList[i] << " ";

}

c. for (int i = length - 1; i <=0; i++)

{

cout << myList[i] << " ";

}

----------------------------------------------------------------------------------------------------------------------

4. A naming collision occurs when two identifiers (variable and/or function names) with the same name are introduced into the same scope.

a. True

b. False

----------------------------------------------------------------------------------------------------------------------

5. Consider the following code:

#include < iostream >

using namespace std;

double division()

{

int a, b;

std::cout << "Enter two numbers: ";

std::cin >> a >> b;

if( b == 0 )

{

throw "Division by zero not allowed!";

}

return (a/b);

}

What does this statement do?

throw "Division by zero not allowed!";

a. Triggers an exception with the message "Division by zero not allowed!";

b. Returns the string "Division by zero not allowed!" to the calling function.

c. Passes the string "Division by zero not allowed!" to the next function being called.

d. Displays the string "Division by zero not allowed!"

----------------------------------------------------------------------------------------------------------------------

6. What can you assume about the grid array?

void battleship::dropBomb(int coord) {

switch (grid[coord]) {

case WATER_UNBOMBED:

grid[coord] = WATER_BOMBED;

break;

case SHIP_UNBOMBED:

grid[coord] = SHIP_BOMBED;

break;

}

return;

}

a. The grid array was declared with an enum data type

b. The grid array was declared with an integer data type

c. The grid array was declared with a string data type

d. The grid array was declared with an abstract data type (i.e. it holds objects of a class)

----------------------------------------------------------------------------------------------------------------------

7. A multidimensional array is a collection of data elements of one or more data types.

a. True

b. False

----------------------------------------------------------------------------------------------------------------------

8. You have an array of structs that is defined as follows.

struct pairT {

std::string key;

int val;

};

Write a single line of code to create (define) an array named myarray that will hold up to 10 pairT objects.

Note: Only use spaces where required.

----------------------------------------------------------------------------------------------------------------------

9. What will happen if the example.txt file is found?

int processFile () {

string line;

ifstream myfile ("example.txt");

if (myfile.is_open())

{

while ( getline (myfile,line) )

{

cout << line << ' ';

}

myfile.close();

return 1;

}

else

return -1;

}

a. Each line of the file is displayed

b. The first line of the file is displayed

c. Each line of the file is counted

d. Each line entered at the keyboard is written to a new file

----------------------------------------------------------------------------------------------------------------------

10. The binary search is fast and reliable whether the dataset is sorted or unsorted.

a. True

b. False

----------------------------------------------------------------------------------------------------------------------

11. Add a line to the main function to sort the people vector by Age using the STL sort function.

#include

#include

#include

#include

using namespace std;

struct Person

{

string name;

int age;

string favoriteColor;

};

bool sortByName(Person &lhs, Person &rhs)

{

return lhs.name < rhs.name;

}

bool sortByAge(Person &lhs, Person &rhs)

{

return lhs.age < rhs.age;

}

bool sortByColor(Person &lhs, Person &rhs)

{

return lhs.favoriteColor < rhs.favoriteColor;

}

int main()

{

vector people(5);

for (int i = 0; i< 5; i++)

{

cout << "Person #" << i + 1 << " name: ";

cin >> people[i].name;

cout << "Person #" << i + 1 << " age: ";

cin >> people[i].age;

cout << "Person #" << i + 1 << " favorite color: ";

cin >> people[i].favoriteColor;

}

//call STL sort function here!

}

Note: Only use spaces where required.

----------------------------------------------------------------------------------------------------------------------

12. You wish to convert this function from using a vector called arr to using an array called arr.

void MyBag::display()

{

for (int i=0; i

cout << arr[i] << " " <

}

}

You create a variable called numused to track the number of elements in the array. Which of the following accomplishes your goal?

a. void MyBag::display()

{

for (int i=0; i

cout << arr[numused] << " " <

}

}

b. void MyBag::display()

{

for (int i=0; i

cout << arr[numused] << " " <

}

}

c. void MyBag::display()

{

for (int i=0; i

cout << arr[i] << " " <

}

}

d. void MyBag::display()

{

for (int i=0; i

cout << numused << " " <

}

}

----------------------------------------------------------------------------------------------------------------------

13. Unlike an array, you should consider passing a vector by reference.

a. True

b. False

----------------------------------------------------------------------------------------------------------------------

14. You need to modify this function so that it only erases the string called sub if it is found. Note that this file has a #include

void Iclear(string sub, string &s)

{

int found;

found = s.find(sub);

s.erase(found, sub.length());

}

Which if clause should you add to this function?

a. void Iclear(string sub, string &s)

{

int found;

found = s.find(sub);

if(found == string::npos)

s.erase(found, sub.length());

}

b. void Iclear(string sub, string &s)

{

int found;

found = s.find(sub);

if(sub != string::npos)

s.erase(found, sub.length());

}

c. void Iclear(string sub, string &s)

{

int found;

found = s.find(sub);

if(found != -1)

s.erase(found, sub.length());

}

d. void Iclear(string sub, string &s)

{

int found;

found = s.find(sub);

if(found != string::npos)

s.erase(found, sub.length());

}

----------------------------------------------------------------------------------------------------------------------

15. Which of the following are valid multi-dimensional array statements? (Select all that apply)

a. int anArray[][] =

{

{ 1, 2, 3, 4 },

{ 5, 6, 7, 8 }

};

b. int anArray[3][5] =

{

{ 1, 2, 3, 4, 5, },

{ 6, 7, 8, 9, 10, },

{ 11, 12, 13, 14, 15 }

};

c. int anArray[][5] =

{

{ 1, 2, 3, 4, 5, },

{ 6, 7, 8, 9, 10, },

{ 11, 12, 13, 14, 15 }

};

d. anArray[2][3] = 7;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

5. Identify the logical fallacies, deceptive forms of reasoning

Answered: 1 week ago

Question

6. Choose an appropriate organizational strategy for your speech

Answered: 1 week ago