0% found this document useful (0 votes)
187 views6 pages

What Will Happen When You Attempt To Compile and Run The Following Code

The code attempts to compile and run C++ code snippets with potential errors. For the first code snippet provided: - It will compile successfully and output "2, 0," as there are no errors in the code. For the second longer code snippet: - It will result in a compilation error at LINE III, as the compare struct is not callable and cannot be used as the third parameter for count. For the multiple choice question, compiling and running the provided code would result in: - A compilation error at LINE I, as stable_sort requires the compare function to only accept const references to elements, not values. - It would not result in a runtime error at LINE II, as

Uploaded by

gm22 mermaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
187 views6 pages

What Will Happen When You Attempt To Compile and Run The Following Code

The code attempts to compile and run C++ code snippets with potential errors. For the first code snippet provided: - It will compile successfully and output "2, 0," as there are no errors in the code. For the second longer code snippet: - It will result in a compilation error at LINE III, as the compare struct is not callable and cannot be used as the third parameter for count. For the multiple choice question, compiling and running the provided code would result in: - A compilation error at LINE I, as stable_sort requires the compare function to only accept const references to elements, not values. - It would not result in a runtime error at LINE II, as

Uploaded by

gm22 mermaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

What will happen when you attempt to compile and run the following code?

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

void

printer (int i)

cout << i << ", ";

int

main()

int mynumbers1[] = {3, 9, 0, 2};

int mynumbers2[] = {6, 2, 4, 5};

vector <int> v1(2);

sort (mynumbers2, mynumbers2 + 4);

sort (mynumbers1, mynumbers1 + 4); //LINE I

set_intersection (mynumbers1, mynumbers1 + 3, mynumbers2, mynumbers2 + 2, v1.begin()); //LINE II

for_each (v1.begin(), v1.end(), printer);

return 0;

the program outputs 2,

runtime error at LINE II

the program outputs 2, 2,

compilation error at LINE I

the program outputs 2, 0,

compilation error at LINE II

What will happen when you attempt to compile and run the following code?
#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

struct compare

bool operator () (int a)

return (a > 0);

} //LINE I

operator int () const

return (4);

} //LINE II

};

int

main()

int mynumbers[] = {8, 9, 7, 6, 4, 1, 4, 4, 9, 7, 2};

vector <int> v (mynumbers, mynumbers + 11);

int count = std::count(v.begin(), v.end(), compare()); //LINE III

cout << count << endl;

return 0;

compilation error in LINE I

the program outputs 4

compilation error in LINE II

the program outputs 3

compilation error in LINE III


runtime error at LINE III

the program outputs 1

What will happen when you attempt to compile and run the following code assuming that you will enter
following sequence: 6 5 7<enter> ?

#include <iostream>

#include <string>

using namespace std;

int

main()

string s;

cin >> s; //LINE I

cout <<s<<", "<<s<<", "<<endl; //LINE II

return 0;

compilation error in LINE II

compilation error in LINE I

the program outputs 6, 6,

the program outputs 6 5 7, 6 5 7,

the program outputs 6, 5, 7, 6, 5, 7,

What happens if you try to compile and run this program?

#include <iostream>

#include <set>

#include <vector>

using namespace std;

int

main()

int mynumbers[] = {8, 9, 7, 6, 4, 1,6};


multiset <int> s1 (mynumbers, mynumbers + 6); //LINE I

s1.insert (s1.find(9), 5); //LINE II

for(multiset <int>::iterator i = s1.begin(); i != s1.end(); i++)

cout << *i <<", ";

return 0;

compilation error in LINE I

program outputs: 1, 4, 5, 6, 6, 7, 8, 9,

runtime error at LINE I

program outputs: 1, 4, 5, 6, 7, 8, 9,

program outputs: 1, 4, 5, 6, 7, 8,

compilation error in LINE II

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <set>

#include <vector>

using namespace std;

int

main()

int mynumbers[] = { 8, 9, 7, 6, 4, 1, 2 };

vector <int> v (mynumbers, mynumbers + 6);

multiset <int> s1 (v.begin() , v.end());

s1.insert (v.begin(), v.end());

s1.insert (v.begin(), v.end()); //LINE I

pair <multiset <int>::iterator, multiset <int>::iterator> range;

range = s1.equal_range(6);

while(range.first != range.second)
{

cout << *range.first <<", "; //LINE II

range.first++;

return 0;

runtime error at LINE I

compilation error in LINE I

compilation error in LINE II

the program outputs 6, 6, 6, 6,

the program outputs 6, 6, 6,

the program outputs 6, 4, 1, 2,

runtime error at LINE II

What happen when you attempt to compile and run the following code? Choose all that apply:

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

void

printer(double i)

cout <<i<<", ";

bool

compare (double a, double b)

return int (a) < int (b);

}
int

main()

double mynumbers[] = { 1.11, 3.13, 2.12, 5.15, 6.16};

vector <double> v1 (mynumbers, mynumbers + 5);

stable_sort (v1.begin(), v1.end(), compare); //LINE I

remove (v1.begin(), v1.end(), 5.15); //LINE II

for_each(v1.begin(), v1.end(), printer);

return 0;

You might also like