Відповідь:
C++ code:
#include <iostream>
#include <cstdlib>
#include <ctime>
class Number{
private:
int size;
int *numbers;
public:
Number();
Number(const Number&);
~Number();
void print();
int index_max_sum_digit();
int sum_digit(int);
};
Number::Number(){
srand(time(NULL));
this->size = 25;
numbers = new int[this->size];
for(int i = 0; i < this->size; i++){
numbers[i] = rand() % 899 + 100;
}
Number::Number(const Number& object){
this->size = object.size;
numbers[i] = object.numbers[i];
Number::~Number(){
delete[] numbers;
void Number::print(){
std::cout << numbers[i] << "\t";
std::cout << std::endl;
int Number::sum_digit(int num){
int sum = 0;
while(num){
sum += num % 10;
num /= 10;
return sum;
int Number::index_max_sum_digit(){
int number,sum = 0,index;
number = numbers[i];
int return_sum = sum_digit(number);
if(return_sum > sum){
sum = return_sum;
index = i;
return index;
int main(){
Number obj;
obj.print();
std::cout << "index: " << obj.index_max_sum_digit() << std::endl;
return 0;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Verified answer
Відповідь:
C++ code:
#include <iostream>
#include <cstdlib>
#include <ctime>
class Number{
private:
int size;
int *numbers;
public:
Number();
Number(const Number&);
~Number();
void print();
int index_max_sum_digit();
int sum_digit(int);
};
Number::Number(){
srand(time(NULL));
this->size = 25;
numbers = new int[this->size];
for(int i = 0; i < this->size; i++){
numbers[i] = rand() % 899 + 100;
}
}
Number::Number(const Number& object){
this->size = object.size;
for(int i = 0; i < this->size; i++){
numbers[i] = object.numbers[i];
}
}
Number::~Number(){
delete[] numbers;
}
void Number::print(){
for(int i = 0; i < this->size; i++){
std::cout << numbers[i] << "\t";
}
std::cout << std::endl;
}
int Number::sum_digit(int num){
int sum = 0;
while(num){
sum += num % 10;
num /= 10;
}
return sum;
}
int Number::index_max_sum_digit(){
int number,sum = 0,index;
for(int i = 0; i < this->size; i++){
number = numbers[i];
int return_sum = sum_digit(number);
if(return_sum > sum){
sum = return_sum;
index = i;
}
}
return index;
}
int main(){
Number obj;
obj.print();
std::cout << "index: " << obj.index_max_sum_digit() << std::endl;
return 0;
}