C++ Assignment Didn’t get how to do this parts please help. (Please label parts,
ID: 3844113 • Letter: C
Question
C++ Assignment
Didn’t get how to do this parts please help. (Please label parts, thank you). (@chegg users please ask your own question, dont copy from this unless its after 2017)
1) Describe briefly what exceptions are and what their purpose is. What is the try block? What is the catch block? What happens with the flow of control if the catch block does not catch a thrown exception? Give a simple example (code)
2) Write one line of code for each line below. What is the template type in each case?
- Create a vector to store student IDs
- Create a vector to store student names
- Create a vector to store Point2D points (class Point2D in assignment 6)
- Create a set to store unique (no duplicates) student names
- Create a set to store unique (no duplicates) student IDs
- Create a map that maps student IDs to their score
- Create a map that maps student IDs to their name
- Create a map that maps student names to the name of their lab partner
Using the above, write a function combine which takes two vectors of student names, group1 and group2 and returns a vector containing all the students in both groups.
Call your function from the main with two groups you create and then print the result returned from the function.
Explanation / Answer
Exception
An exception is a problem that arises during the execution or while running of a program.A C++ exception is a response to an exceptional circumstance that arises while a program is running.
For example dicide by zero
z = x/0;
warning: division by zero [-Wdiv-by-zero].
Exceptions provides control on the program,C++ exception handling is built upon three keywords: try, catch, and throw.
throw: A program throws an exception when a problem shows up. This is done using a throw keyword.
Example: here we need to write the messages when we are getting exception
catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.
Example: Here we need to catch the exception and send messages to the programmer so that he can undestood.
try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.
Example: Here we need to wrie the logic of the code where we are going to get the exception.
try
{
// protected code
}
catch( ExceptionName e )
{
// catch block for First exception
}
catch( ExceptionName e1)
{
// catch block for second exception
}
Sample example for Exception
#include <iostream>
using namespace std;
double division(int a, int b) {
if( b == 0 ) {
throw "Division by zero Exceptiion!";
}
return (a/b);
}
int main () {
int dividend = 50;
int divisor = 0;
double res = 0;
try {
res = division(dividend, divisor);
cout << res << endl;
}catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
another example
#include <iostream>
#include <string>
using namespace std;
using std::string;
std::string division(int age) {
if( age<18 ) {
throw "Exception ! ..Sorry you are minor no access!";
}
return "hello ur major";
}
int main () {
int x = 50;
int y = 0;
//double z = 0;
try {
std::string s=division(x);
cout << s << endl;
}catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
Flow of try catch block
If an exception occurs in try block then the control of execution is passed to the catch block from try block. The exception is caught up by the corresponding catch block. A single try block can have multiple catch statements associated with it, but each catch block can be defined for only one exception class. The program can also contain nested try-catch-finally blocks.
After the execution of all the try blocks, the code inside the finally block executes. It is not mandatory to include a finally block at all, but if you do, it will run regardless of whether an exception was thrown and handled by the try and catch blocks.
Create a vector to store student IDs
vector<int> vec;
// push 5 values into the vector
for(i = 0; i < 5; i++){
vec.push_back(i);
}
// display extended size of vec
cout << "extended vector size = " << vec.size() << endl;
// access 5 values from the vector
for(i = 0; i < 5; i++){
cout << "value of vec [" << i << "] = " << vec[i] << endl;
}
- Create a vector to store student names
vector<string> vec;
// push 5 strings names into the vector
for(i = 0; i < 5; i++){
string word="santu";
word=word+i;
vec.push_back(word);
}
// display extended size of vec
cout << "extended vector size = " << vec.size() << endl;
// access 5 values from the vector
for(i = 0; i < 5; i++){
cout << "value of vec [" << i << "] = " << vec[i] << endl;
}
- Create a vector to store Point2D points (class Point2D in assignment 6)
cv::vector<cv::Point> pointList;
Adding new point is easy:
pointList.push_back(newPoint); // newPoint is your cv::Point object
You can access to member elements in your list like this:
for (int n = 0; n < pointList.size(); n++)
{
cv::Point myPoint = pointList[n];
}
- Create a set to store unique (no duplicates) student names
#include <iostream>
#include <set>
using namespace std;
int main(void) {
// Default constructor
std::set<char> t_set;
t_set.insert('a');
t_set.insert('a');
t_set.insert('e');
t_set.insert('i');
t_set.insert('o');
t_set.insert('u');
int size = t_set.size();
for (std::set<char>::iterator it=t_set.begin(); it!=t_set.end(); ++it)
std::cout << ' ' << *it;
std::cout << ' ';
std::cout << "Contents of set container t_set = " << size;
return 0;
}
- Create a set to store unique (no duplicates) student IDs
#include <iostream>
#include <set>
using namespace std;
int main(void) {
// Default constructor
std::set<int> t_set;
t_set.insert(1);
t_set.insert(1);
t_set.insert(3);
t_set.insert(5);
t_set.insert(5);
t_set.insert(6);
int size = t_set.size();
for (std::set<int>::iterator it=t_set.begin(); it!=t_set.end(); ++it)
std::cout << ' ' << *it;
std::cout << ' ';
std::cout << "Contents of set container t_set = " << size;
return 0;
}
- Create a map that maps student IDs to their score
map<int,int> m{ {3129,600} , {3218,300} , {3220,500} };
- Create a map that maps student IDs to their name
map<string,int> map1;
map1["santu"]=100;
map1["dillu"]=200;
map1["ramesh"]=300;
map1["suresh"]=400;
- Create a map that maps student names to the name of their lab partner
map<string,int> map1;
map1["santu"]="tanu";
map1["dillu"]="sheela";
map1["ramesh"]="preeti";
map1["suresh"]="jessi";
write a function combine which takes two vectors of student names, group1 and group2 and returns a vector containing all the students in both groups.
Call your function from the main with two groups you create and then print the result returned from the function.
#include <vector>
#include <utility>
#include <iterator>
namespace detail
{
template <typename R>
void do_concatenation(R& accum) {}
template <typename R, typename First, typename... More>
void do_concatenation(R& accum, First const& first, More const&... more)
{
using std::begin;
using std::end;
std::copy(begin(first), end(first), std::inserter(accum, end(accum)));
do_concatenation(accum, more...);
}
}
template <typename Result, typename... Containers>
Result concatenate(Containers const&... containers)
{
Result accum;
detail::do_concatenation(accum, containers...);
return accum;
}
template <typename First, typename... More>
std::vector<typename First::value_type> to_vector(First const& first, More const&... containers)
{
return concatenate<std::vector<typename First::value_type>>(first, containers...);
}
/// demo
#include <set>
#include <list>
#include <iostream>
#include <map>
#include <string>
int main()
{
//auto x = to_vector(std::vector<int> { 1,2,3 }, std::list<int> { 9,8,11 }, std::set<int> { 42 });
//for (auto i : x)
// std::cout << i << " ";
//std::cout << std::endl;
// fun with maps:
auto y = concatenate<std::map<long, std::string> >(
std::map<int, const char*> { { 3219, "santu" }, { 3223, "dillu" } },
std::map<unsigned, std::string> { { 3220, "surya" }, { 3224, "ravi" } }
);
for (auto kvp : y)
std::cout << "(" << kvp.first << ", " << kvp.second << ")";
}
output
(3219, santu)(3223, dillu)(3220, surya),(3224,ravi)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.