Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class LengthsNotEqualException : exception {
- private:
- string message;
- public:
- LengthsNotEqualException(string message) {
- this->message = message;
- }
- const char* what() const noexcept {
- return message.c_str();
- }
- };
- class Container {
- private:
- int lenght;
- int* arr;
- public:
- Container(int lenght) {
- this->lenght = lenght;
- arr = new int[lenght];
- }
- friend istream& operator >> (istream& cin, Container& container) {
- for (int i = 0; i < container.lenght; i++)
- cin >> container.arr[i];
- return cin;
- }
- friend int dot_product(Container& a, Container& b) {
- if (a.lenght != b.lenght)
- throw LengthsNotEqualException("The lengths of two containers are not equal");
- int c = 0;
- for (int i = 0; i < a.lenght; i++)
- c += a.arr[i] * b.arr[i];
- return c;
- }
- };
- int main()
- {
- int size;
- cin >> size;
- Container a(size);
- cin >> a;
- cin >> size;
- Container b(size);
- cin >> b;
- try {
- cout << dot_product(a, b);
- }
- catch (LengthsNotEqualException & exception){
- cout << exception.what();
- }
- catch (exception & exception) {
- cout << exception.what();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment