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 display 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.

I will answer any of your questions, которые возникли при решении задач, в комментариях к этой статье.

To support our site – click on the piggy bank and choose any convenient way.

Newsletter of programming:

Tasks: Two-dimensional arrays C ++
4.3 (86.67%) 12 votes

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

  1. In the hammer throw competes n sportcmenov. Each of them made m rolls. The winner of the competition the athlete, whose best result is maximum on all rolls. In this way, program to find the maximum value of the element in the array, and its indexes (ie athlete number and the number of attempts).
    Input data
    The program takes as input two integers n and m, is the number of rows and columns in an array. Further, in the input stream is n rows by m numbers, It is an array of elements.

    Output
    The program prints the value of the maximum element, then the line number and column number, in which it occurs. If the array has several maximal elements, it is necessary to bring the minimum line number, wherein an element is found, and if this line of several elements, it is necessary to bring the minimum column number. Remember, that all rows and columns are numbered from 0.

    examples
    input data
    3 3
    3 1 2
    1 3 4
    3 3 3
    output
    4
    1 2

    1. And what do you expect?
      That someone will decide for you your homework while you walk widely?

      So for such a pleasure to pay money!

    2. #include “stdafx.h”
      #include
      #include
      #include
      #include // setw(indent)

      using namespace std;

      int main() {

      setlocale(0, “”);
      srand(time(NULL));

      const int ROWSIZE = 3;
      const int COLSIZE = 3;

      int arr[ROWSIZE][COLSIZE] = {};
      // filling the array with keyboard
      /*
      for (int rowNum = 0; rowNum < ROWSIZE; rowNum ) {
      for (int colNum = 0; Coin < COLSIZE; Coin ++) {
      cout << "Пожалуйста, enter the initial values ​​of the array: ";
      cout << rowNum + 1 << "-я строка ";
      cout << Coin + 1 <> arr[rowNum][Coin];

      }
      }
      */
      // Fill the array with random numbers
      for (int rowNum = 0; rowNum < ROWSIZE; rowNum ) {
      cout << "Строка " << rowNum + 1 << ": ";
      for (int colNum = 0; Coin < COLSIZE; Coin ++) {
      arr[rowNum][Coin] = 1 + rand() % 9;
      cout << setw(2) << arr[rowNum][Coin];
      }
      cout << endl;
      }
      // We find the maximum number of, and if there are several we find the very first row and column, where it occurs
      int row[ROWSIZE]{};
      int col[COLSIZE]{};
      int temp = 0;
      int temp1 = 0;

      for (int r = 0; r < ROWSIZE; r++) {
      for (int c = 0; c < COLSIZE; c++) {
      if (col[0] < arr[c][r]) {
      col[0] = arr[c][r];
      temp = r;
      temp1 = c;
      }
      else c;
      }

      }
      cout << "Максимальное: " << col[0] << " COL: " << temp1 + 1 << " ROW: " << temp + 1 << endl;

      system("pause");
      return 0;
      };

  2. Explain, please!!! why the sum of the columns displayed wrong??

    #include
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “rus”);
    srand(time(NULL));
    const int RowCount = 10;
    int array[RowCount][RowCount] = {};
    int SumRow[RowCount] = {}, SumColumn[RowCount] = {};

    for (int i = 0; i < RowCount; i ) {

    for (int j = 0; j < RowCount; j ) {
    array[i][j] = i*j;
    cout << setw(4) << array[i][j] << " ";
    SumRow[i] += array[i][j];
    SumColumn[i] += array[j][i];

    }
    cout << endl;
    }
    for (int i = 0; i < 10; i ) {
    cout << "Сумма в " << i + 1 << " string = " << SumRow[i] << " ";
    cout << "Сумма в " << i + 1 << " column = " << SumColumn[i] << endl;
    }

    system("pause");
    return 0;
    }

    1. String “SumColumn[i] += array[j][i];” adds more blank cell array with random numbers.

Leave a Reply

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