Відповідь:
C++ code:
#include <iostream>
#include <cstdlib>
#include <ctime>
void randarr(int **arr,const int n,const int m){
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
arr[i][j] = rand() % 30 - 10;
}
void printarr(int *arr,const int size){
for(int i = 0; i < size; i++){
std::cout << arr[i] << "\t";
std::cout << std::endl;
void printarr(int **arr,const int n,const int m){
std::cout << arr[i][j] << "\t";
int minarr(int **arr,const int n, const int m){
int min = arr[0][0],index_i = 0,index_j = 0;
if(arr[i][j] % 3 == 0 && arr[i][j] < min){
min = arr[i][j];
index_i = i;
index_j = j;
std::cout << "arr[" << index_i << "][" << index_j << "] = " << min << std::endl;
return min;
int counter(int **arr,const int n, const int m){
int counter_size = 0;
if(arr[i][j] != 0){
counter_size++;
return counter_size;
int *sum(int **arr,const int n, const int m){
int counter_size = counter(arr,n,m),ind = 0;
int *return_arr = new int[counter_size];
return_arr[ind] = arr[i][j];
ind++;
return return_arr;
int main(){
srand(time(NULL));
int n,m;
std::cin >> n >> m;
int **arr = new int*[n];
arr[i] = new int[m];
randarr(arr,n,m);
printarr(arr,n,m);
std::cout << "min: " << minarr(arr,n,m) << std::endl;
int count = counter(arr,n,m);
int *array = sum(arr,n,m);
printarr(array,count);
delete[] arr[i];
delete[] arr;
return 0;
Пояснення:
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Відповідь:
C++ code:
#include <iostream>
#include <cstdlib>
#include <ctime>
void randarr(int **arr,const int n,const int m){
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
arr[i][j] = rand() % 30 - 10;
}
}
}
void printarr(int *arr,const int size){
for(int i = 0; i < size; i++){
std::cout << arr[i] << "\t";
}
std::cout << std::endl;
}
void printarr(int **arr,const int n,const int m){
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
std::cout << arr[i][j] << "\t";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
int minarr(int **arr,const int n, const int m){
int min = arr[0][0],index_i = 0,index_j = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(arr[i][j] % 3 == 0 && arr[i][j] < min){
min = arr[i][j];
index_i = i;
index_j = j;
}
}
}
std::cout << "arr[" << index_i << "][" << index_j << "] = " << min << std::endl;
return min;
}
int counter(int **arr,const int n, const int m){
int counter_size = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(arr[i][j] != 0){
counter_size++;
}
}
}
return counter_size;
}
int *sum(int **arr,const int n, const int m){
int counter_size = counter(arr,n,m),ind = 0;
int *return_arr = new int[counter_size];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(arr[i][j] != 0){
return_arr[ind] = arr[i][j];
ind++;
}
}
}
return return_arr;
}
int main(){
srand(time(NULL));
int n,m;
std::cin >> n >> m;
int **arr = new int*[n];
for(int i = 0; i < n; i++){
arr[i] = new int[m];
}
randarr(arr,n,m);
printarr(arr,n,m);
std::cout << "min: " << minarr(arr,n,m) << std::endl;
int count = counter(arr,n,m);
int *array = sum(arr,n,m);
printarr(array,count);
for(int i = 0; i < n; i++){
delete[] arr[i];
}
delete[] arr;
return 0;
}
Пояснення: