The basics of programming in c++ for beginners

Tasks: Two-dimensional arrays C ++

To begin to address the tasks, proposed in this article, you need to know that is two-dimensional (multidimensional) arrays . Tasks are arranged from simple to complex. So that, If you're new to programming, start with the first, to better deal with the other.

1. Declare a two-dimensional array, fill integers and show on the screen.

2) Объявить двумерный массив и заполнить его построчно с клавиатуры. После заполнения – показать заполненную матрицу на экран и посчитать сумму элементов отдельно в каждом столбце и каждой строке.

3) Fill two-dimensional array of random numbers 10  to 100. Calculate the sum of the elements separately in each line and determine the line number,  in which this sum is maximal.

Your questions, которые возникли при решении задач, leave comments on this article.

117 thoughts on “Tasks: Two-dimensional arrays C ++

  1. I 2 otherwise invented:
    #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “Russian”);
    const int STROKA = 3;
    const int STOLBEC = 4;
    you massive[DISCIPLINE][STOLBEC];
    int sum_stroki=0, sum_stolbca;
    cout << "Вводите элементы массива: " << endl;
    //input array
    for (int i = 0; i < DISCIPLINE; i )
    {
    cout << i+1<<"-я строка: " << endl;
    for (int j = 0; j > solid[i][j];
    }
    }
    //Output array
    for (int i = 0; i < DISCIPLINE; i )
    {
    for (int j = 0; j < STOLBEC; j )
    {
    cout << solid[i][j]<<" ";
    }
    cout << endl;
    }
    //Output sums lines
    for (int i = 0; i < DISCIPLINE; i )
    {
    for (int j = 0; j < STOLBEC; j )
    {
    sum_stroki + = massiv[i][j];
    }
    cout << "Сумма " << i + 1 << "-ой строки: " << sum_stroki << endl;

    }
    cout << endl;
    //Output column sums
    for (int j = 0; j < STOLBEC; j )
    {
    sum_stolbca = 0;
    for (int i = 0; i < DISCIPLINE; i )
    {
    sum_stolbca + = massiv[i][j];
    }
    cout << "Сумма " << j + 1 << "-го столбца: " << sum_stolbca << endl;
    }
    }

  2. Here's my version 3 tasks:
    #include
    #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “Russian”);
    srand(time(NULL));
    const int STROKA = 6;
    const int STOLBEC = 6;
    you massive[DISCIPLINE][STOLBEC];
    you massiv_sum_strok[DISCIPLINE];
    int sum_stroki,max_sum, max_number=0;
    for (int i = 0; i < DISCIPLINE; i )
    {
    for (int j = 0; j < STOLBEC; j )
    {
    solid[i][j] = rand() % 90 + 10;
    }
    }
    for (int i = 0; i < DISCIPLINE; i )
    {
    for (int j = 0; j < STOLBEC; j )
    {
    cout << solid[i][j] << " ";
    }
    cout << endl;
    }
    for (int i = 0; i < DISCIPLINE; i )
    {
    sum_stroki = 0;
    for (int j = 0; j < STOLBEC; j )
    {
    sum_stroki + = massiv[i][j];

    }
    massiv_sum_strok[i] = sum_stroki;
    cout << "Сумма элементов " << i + 1 << "-ой строки: " << sum_stroki << endl;
    }
    max_sum = massiv_sum_strok[0];
    for (int i = 0; i max_sum)
    {
    max_sum = massiv_sum_strok[i];
    max_number = i;
    }
    }
    cout << "Максимальная сумма элементов строки: " << max_sum << endl;
    cout << "Номер строки с максимальной суммой элементов: " << max_number+1;
    }
    sorry that no komentov:)

  3. My answer to the 3rd task .
    #include
    #include
    using namespace std;
    int main() {
    setlocale(LC_ALL,”RUS”);
    const int ROW = 3;
    const int COL = 4;
    int ARR[ROW][COL];
    srand(time(NULL));
    for (int i = 0; i < ROW; i ) {
    for (int g = 0; g < COL; g++) {
    ARR[i][g] = rand() % 91 + 10;

    }
    }
    int ARR1[ROW]{};
    for (int i = 0; i < ROW; i ) {
    int T = 0;
    for (int g = 0; g < COL; g++) {

    T += ARR[i][g];
    ARR1[i] = T;
    }
    }
    int o;
    o = ARR1[0];
    int p;
    p = 0;
    for (int i = 1; i < ROW; i ) {
    if (O < ARR1[i]) {
    o = ARR1[i];
    p = i;
    }
    }
    cout << "В строчке " << p << " сумма всех ее элементов наибольшая ";
    }

  4. ——– 2 ——–
    #include
    #include
    using namespace std;

    int main() {

    const int _lenq = 3;
    const int _boy = 3;
    int _entereNum;
    int tver[_lenq][_boy] = {};
    int Sum = 0;

    cout << "Fill the matrix 3×3 numbers line by line\n";

    for (int i = 0; i != _boy; i ) {
    for (int j = 0; j != _lenq; j ) {
    cout << i+1 << " column " << j+1 <> _entereNum;
    tver[i][j] = _entereNum;
    }
    cout << endl;
    }
    for (int i = 0; i != _boy; i ) {
    cout << "|";
    for (int j = 0; j != _lenq; j ) {
    cout << setw(4) << tver[i][j] << setw(4);
    }
    cout << " |" << endl;
    }
    cout << "The sum or each rows:" << endl;

    for (int i = 0; i != _boy; i ) {
    for (int j = 0; j != _lenq; j ) {
    _sum += tver[i][j];
    }
    cout << "Row " << i << ": " << _sum << endl;
    _sum = 0;
    }

    cout << "The sum or each columns:" << endl;
    for (int j = 0; j != _lenq; j ) {
    for (int i = 0; i != _boy; i ) {
    _sum += tver[i][j];
    }
    cout << "Column " << j << ": " << _sum << endl;
    _sum = 0;
    }
    return 0;
    }

  5. My calculator:

    for(int line = 0; line < LINES; line++)
    {
    for(int column = 0; column < COLUMNS; t++)
    {
    arrayAmmountLines[line] += arrayMain[line][column];
    arrayAmmountColumns[column] += arrayMain[line][column];
    }
    }

  6. task №3

    #include
    #include
    using namespace std;
    int main()
    {
    srand(time(NULL));
    int arr[2][2] = {{10 + rand() % 90 , 10 + rand() % 90 },{10 + rand() % 90 , 10 + rand() % 90 }};

    for(int i = 0; i < 2; i ){
    for(int j = 0; j < 2; j ){
    cout << " " << arr[i][j];
    }
    cout << endl;
    }
    return 0;
    }

  7. 3her task:

    #include
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “ru”);

    srand(time (NULL));
    const int ROW = 4;
    const int COL = 3;

    int sumROW[ROW] = {};// To write amounts in lines
    int maxSum = 0; // To record the maximum amount
    int maxROW = 0;// to write a line with max sum

    int arr[ROW][COL];

    for (int i = 0; i < ROW; i )
    {
    for (int j = 0; j maxSum)
    {
    maxSum = sumROW[i]; // finding the max amount
    maxROW = i+1; // finding the line with the max sum
    }
    }

    cout << "Сумма элементов "<<i + 1 << "-й строки: " << sumROW[i] << endl;
    }
    cout << endl;
    cout << "Максимальная сумма элементов " << maxSum << " в строке "<< maxROW << endl;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *