Answer the question
In order to leave comments, you need to log in
How to overload the += operator for two arrays?
The task is as follows: overload the operations < (comparing two lists by the sum of elements), += (adding an element to the end of the list), sort the array of instances of the class of lists in descending order of sums using the sorting algorithm by choice, add the elements of the list with the smallest sum to the list with the largest sum amount.
Problem: I can't implement the += overload for two arrays.
#include <iostream>
#include <string.h>
using namespace std;
class Array {
private:
int* a, * b; // указатель на массивы
unsigned int size1, size2; // размеры массивов
int k1, k2, n;
int sum1, sum2;
public:
Array(); // конструктор по умолчанию
Array(int s, int c); // конструктор с аргументом(размерами массивов)
~Array(); // деструктор
void setarray(); // метод ввода массивов с клавиатуры
void getarray(); // вывод массивов
void delet(); // удаление элемента за номером
void add(); // вставка элемента
void sort(); // сортировка массивов
Array operator < (Array sum) {
return Array(this->sum1 < sum.sum2, this->sum2 < sum.sum1);
};
/*
? Array operator += (Array &v1, Array &v2) {
? return Array(v1.a = v1.a + v2.b, v2.b = v2.b + v1.a);
? };
*/
};
Array::Array(){
size1 = 0;
a = new int[size1];
for (size_t i = 0; i != size1; i++) {
a[i] = 0;
}
size2 = 0;
a = new int[size2];
for (size_t i = 0; i != size1; i++) {
b[i] = 0;
}
}
Array::Array(int s, int c) {
if (s > 0) {
size1 = s;
size2 = c;
a = new int[size1];
b = new int[size2];
for (size_t i = 0; i != size1; i++) {
a[i] = 0;
}
for (size_t i = 0; i != size2; i++) {
b[i] = 0;
}
}
}
Array::~Array() {
delete[]a;
delete[]b;
}
void Array::setarray() {
cout << "Enter 1 massive: ";
for (size_t i = 0; i != size1; i++) {
cin >> a[i];
}
cout << "Enter 2 massive: ";
for (size_t i = 0; i != size2; i++) {
cin >> b[i];
}
sum1 = 0;
sum2 = 0;
for (int i = 0; i < size1; i++){
sum1 = sum1 + a[i];
}
for (int i = 0; i < size2; i++){
sum2 = sum2 + b[i];
}
Array sum(sum1, sum2);
if (sum1 < sum2) {
cout << endl << "Sum of elements of 2 massive bigger" << endl;
}
else {
cout << endl << "Sum of elements of 1 massive bigger" << endl;
}
}
void Array::getarray() {
for (size_t i = 0; i != size1; i++) {
cout << a[i] << " ";
}
for (size_t i = 0; i != size2; i++) {
cout << b[i] << " ";
}
}
void Array::delet() {
cout << "Enter the number of item of 1 massive that nedeed to delete: ";
cin >> k1;
for (int i = 0; i < size1 - 1; i++)
if (i >= k1 - 1)
{
a[i] = a[i + 1];
}
else;
cout << "New massive: ";
for (int i = 0; i < size1 - 1; i++)
{
cout << a[i] << " ";
}
cout << endl;
cout << "Enter the number of item of 2 massive that nedeed to delete: ";
cin >> k2;
for (int i = 0; i < size2 - 1; i++)
if (i >= k2 - 1)
{
b[i] = b[i + 1];
}
else;
cout << "New massive: ";
for (int i = 0; i < size2 - 1; i++)
{
cout << b[i] << " ";
}
};
void Array::add()
{
cout << "Enter the item that nedeed to add in 1 massive: ";
cin >> k1;
cout << "Enter nmber of item of 1 massive after which you wanna put the number: ";
cin >> n;
for (int i = size1 - 1; i > n; i--)
a[i] = a[i - 1];
a[n] = k1;
cout << "New massive: ";
for (int i = 0; i < size1; i++)
cout << a[i] << " ";
cout << "\n";
cout << "Enter the item that nedeed to add in 2 massive: ";
cin >> k2;
cout << "Enter nmber of item of 2 massive after which you wanna put the number: ";
cin >> n;
for (int i = size2 - 1; i > n; i--)
b[i] = b[i - 1];
b[n] = k2;
cout << "New massive: ";
for (int i = 0; i < size2; i++)
cout << b[i] << " ";
}
void Array::sort() {
cout << "Sorted 1 massive: ";
int temp1;
for (int i = 0; i < size1 - 1; i++) {
for (int j = 0; j < size1 - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp1 = a[j];
a[j] = a[j + 1];
a[j + 1] = temp1;
}
}
}
for (int i = 0; i < size1; i++) {
cout << a[i] << " ";
}
cout << "\n";
cout << "Sorted 2 massive: ";
int temp2;
for (int i = 0; i < size2 - 1; i++) {
for (int j = 0; j < size2 - i - 1; j++) {
if (b[j] > b[j + 1]) {
temp2 = b[j];
b[j] = b[j + 1];
b[j + 1] = temp2;
}
}
}
for (int i = 0; i < size2; i++) {
cout << b[i] << " ";
}
}
int main() {
int size1;
int size2;
cout << "Enter count of numbers of 1 massive: ";
cin >> size1;
cout << "Enter count of numbers of 2 massive: ";
cin >> size2;
Array arr(size1, size2);
arr.setarray();
cout << "-----------------------------------------" << endl;
cout << endl;
arr.delet();
cout << endl;
cout << "-----------------------------------------" << endl;
cout << endl;
arr.add();
cout << endl;
cout << "-----------------------------------------" << endl;
arr.sort();
cout << endl;
cout << "-----------------------------------------" << endl;
return 0;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question