@m000gg
m000gg

Как вывести сумму всех элементов для каждого столбца, которые оканчиваются тройкой?

У меня есть задача: надо вывести сумму всех элементов для каждого столбца, которые оканчиваются тройкой.

Вот мой код на с++
#include <iostream>
#include <ctime>
#include <vector>



using namespace std;

void Randmass(int**, int, int, int);
void Printmass(int**, int);
vector<int> Col_3(int**,int);


int main()
{
    int n,first_value,last_value;
    cout<<"vvedite diapazn:"<<endl;
    cin>>first_value>>last_value;
    srand(time(0));
    cout<<"vvedite razmer matr:"<<endl;cin>>n;
    int matrix[n][n]={0};
    int *stroki[n];
    for (int i = 0; i<n; i++)stroki[i] =&matrix[i][0];
    Randmass(stroki,n,first_value,last_value);
    cout<<endl;
    Printmass(stroki,n);
    vector<int> v = Col_3(stroki,n);
    cout<< endl;
    for(int i=0;i<n;i++)
    cout<<"summa:"<<i<<"="<<v[i]<<endl;

    
}


void Randmass(int** mass,int sizematr,int first_value, int last_value)
{
    cout<<"initsializavia massiva:"<<endl;
    for (int i = 0; i<sizematr; i++){
        for (int j = 0; j<sizematr; j++){
            mass[i][j] = first_value + rand()% (last_value - first_value + 1);
        }
    }
    
}

void Printmass(int** mass,int sizematr)
{
    cout<<"Pechat massiva:"<<endl;
    for (int i = 0; i<sizematr; i++){
            cout<<endl;
        for (int j = 0; j<sizematr; j++){
            cout<<"\t"<<mass[i][j];
        }
    }
    
}



vector<int> Col_3(int**x, int n)
{
    vector<int> v;
    for (int j = 0; j<n;j++)
    {   v. push_back(0);
        for( int i= 0; i<n; i++)
        {  
           if(x[i][j]%10 == 3){
              v[i] += x[i][j]; 
           }
        }
    }
    return v;
}

Но что- то не очень это получается, что нужно изменить в коде?
  • Вопрос задан
  • 74 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы