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