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. #include
    using namespace std;

    int main()
    {
    const int ROW = 3;
    const int COL = 3;
    setlocale(LC_ALL, “RU”);
    int arr[ROW][COL];
    cout << "Заполните двумерный массив случайными числами" << endl;
    for (int i = 0; i < ROW; i ) {
    for (int j = 0; j > arr[i][j];
    }
    }
    int sumROW = 0, sumCOL = 0;
    cout << endl;
    for (int i = 0; i < ROW; i ) {
    for (int j = 0; j < COL; j ) {
    cout << arr[i][j]<< "\t";
    }
    cout<<endl;
    }
    for (int i = 0; i < ROW; i ) {
    for (int j = 0; j < COL; j ) {
    sumROW = sumROW + arr[i][j];
    }
    cout << "Сумма элементов в строке : " << i + 1 << " " << sumROW;
    sumROW = 0;
    cout << endl;
    }
    for (int i = 0; i < ROW; i ) {
    for (int j = 0; j < COL; j ) {
    sumCOL = sumCOL + arr[j][i];
    }
    cout << "Сумма элементов в столбце : " << i + 1 << " " << sumCOL ;
    sumCOL = 0;
    cout << endl;
    }
    }

  2. #include
    #include

    using namespace std;

    int main()
    {
    srand(time(NULL));
    const int ROW = 10;
    const int COL = 10;
    int sumROW = 0;
    int sumROWMax = 0;
    int numberROW = 1;

    setlocale(LC_ALL, “RU”);
    int arr[ROW][COL];
    for (int i = 0; i < ROW; i ) {
    for (int j = 0; j < COL; j ) {
    arr[i][j] = rand() % 100 + 10;
    }
    }
    for (int i = 0; i < ROW; i ) {
    for (int j = 0; j < COL; j ) {
    cout << arr[i][j] << "\t";
    }
    cout << endl;
    }
    for (int i = 0; i < ROW; i )
    {
    for (int j = 0; j < COL; j )
    {
    sumROW = sumROW + arr[i][j];
    }
    cout << "Сумма элементов в строке : " << i + 1 << " " << sumROW << endl;
    sumROW = 0;
    }
    cout << endl;

    for (int i = 0; i < ROW; i )
    {
    for (int j = 0; j sumROWMax)
    {
    sumROWMax = sumROW;
    numberROW = i;
    }
    sumROW = 0;
    }
    cout << "Максимальная сумма элементов в строке "<< numberROW + 1 << ". line sum : " << sumROWMax << endl;

    }

  3. Can I still 3-4 tasks on this topic? To the author of RESPECT for such training material, the topics are very clearly and simply stated

  4. #include
    #define DEBUG
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “ru”);
    int ROWS, COLS;
    cout <> ROWS;
    cout <> COLS;
    int** a = new int*[ROWS];
    for (int i = 0; i < ROWS; i )
    a[i] = new int[COLS];

    for (int i = 0; i < ROWS; i ) // input of matrix elements
    for (int j = 0; j < COLS; j )
    a[i][j] = rand() % 101 + 10;
    cout<< endl;
    #ifdef DEBUG
    for (int i = 0; i < ROWS; i )
    {
    for (int j = 0; j < COLS; j )
    cout << a[i][j] << " ";
    cout << endl;
    } //matrix output
    #endif
    int* sum_in_row = new int[ROWS]; // array initialization
    for (int i = 0; i < ROWS; i )
    sum_in_row[i] = 0;
    for (int i = 0; i < ROWS; i ) // counting the sums of row elements
    {
    int sum = 0;
    for (int j = 0; j < COLS; j )
    sum_in_row[i] += a[i][j];
    }

    for (int i = 0; i < ROWS; i )
    cout << "сумма строки " << i + 1 << "=" << sum_in_row[i] << endl; // output of row sums

    int num_max_sum = 0;
    int max_sum = 0;
    int h=0;
    for (int i = 0; i max_sum ? max_sum = sum_in_row[i], num_max_sum = i:h++;

    cout << "номер строки с наибольшей суммой: " << num_max_sum+1;

    for (int i = 0; i < ROWS; i ) // clearing memory
    delete [] a[i];
    delete [] a;
    delete[] sum_in_row;
    }

  5. #include
    using namespace std;
    #include
    #include “mainCu.h”
    int main()
    {
    setlocale(LC_ALL, “rus”);
    srand(time(NULL));
    int row;
    cin >> row;
    int col;
    cin >> col;
    int** arr = new int* [row];
    int* row_arr_sum = new int[row];
    int* col_arr_sum = new int[col];
    int row_sum = 0, col_sum = 0;
    for (short i = 0; i < row; i )
    row_arr_sum[i] = 0;
    for (short i = 0; i < row; i )
    col_arr_sum[i] = 0;
    for (short i = 0; i < row; i ) {
    arr[i] = new int[row];
    for (short j = 0; j < col; j )
    arr[i][j] = rand() % 10;
    }
    for (short i = 0; i < row; i ) {
    for (short j = 0; j < col; j )
    cout <<"|"<< arr[i][j] << "|";
    cout << endl;
    }cout << endl;
    for (short i = 0; i < row; i ) {
    for (short j = 0; j < col; j ) {
    row_arr_sum[i] += arr[i][j];
    col_arr_sum[i] += arr[i][j];
    }
    }
    cout << "сумма по строкам: " << endl;
    for (short i = 0; i < row; i ) {
    row_sum += row_arr_sum[i];
    cout << "строка(" << i+1 << "):"<< row_arr_sum[i]<<endl;
    } cout << "сумма по столбцам: " << endl;
    for (short i = 0; i < row; i ){
    col_sum += row_arr_sum[i];
    cout << "столбец(" << i + 1 << "):" << col_arr_sum[i] <<endl;}
    cout << endl << col_sum << endl << row_sum << endl;

    }

    1. //sorry, I made a couple of mistakes.. fixed here

      #include
      using namespace std;
      #include
      #include “mainCu.h”
      int main()
      {
      setlocale(LC_ALL, “rus”);
      srand(time(NULL));
      int row;
      cin >> row;
      int col;
      cin >> col;
      int** arr = new int* [row];
      int* row_arr_sum = new int[row];
      int* col_arr_sum = new int[col];

      for (short i = 0; i < row; i )
      row_arr_sum[i] = 0;
      for (short i = 0; i < row; i )
      col_arr_sum[i] = 0;
      for (short i = 0; i < row; i ) {
      arr[i] = new int[row];
      for (short j = 0; j < col; j )
      arr[i][j] = rand() % 10;
      }
      for (short i = 0; i < row; i ) {
      for (short j = 0; j < col; j )
      cout <<"|"<< arr[i][j] << "|";
      cout << endl;
      }cout << endl;
      for (short i = 0; i < row; i ) {
      for (short j = 0; j < col; j ){
      row_arr_sum[i] += arr[i][j];
      col_arr_sum[i] += arr[j][i];
      }
      }
      cout << "сумма по строкам: " << endl;
      for (short i = 0; i < row; i ) {
      cout << "строка(" << i+1 << "):"<< row_arr_sum[i]<<endl;
      } cout << "сумма по столбцам: " << endl;
      for (short i = 0; i < row; i ){
      cout << "столбец(" << i + 1 << "):" << col_arr_sum[i] <<endl;}
      int max_count = 0;
      int min_count =0;
      int max_row_sum = row_arr_sum[0], min_row_sum = row_arr_sum[0];
      for (short i= 0; i <row; i )
      {

      if (max_row_sum row_arr_sum[i]) {

      min_row_sum = row_arr_sum[i];
      min_count = i;
      }
      }
      cout << "максимальная сумма в строке №" << max_count + 1 << " = " << max_row_sum << endl
      << "Минимальная сумма в строке №" << min_count + 1 << " = " << min_row_sum << endl;

      }

  6. #include
    #include

    using namespace std;

    int main()
    {
    srand(time(NULL));

    const int ROWS = 10;
    const int COLS = 10;
    int arr[ROWS][COLS]{};

    for (int i = 0; i < ROWS; i )//Filling array arr[][]
    {
    for (int j = 0; j < COLS; j )
    {
    arr[i][j] = rand() % 90 + 10;
    }
    }

    int arr_sum[ROWS]{};//We create an array in which we will store the sum // of the elements of each row of the array arr[][]
    for (int i = 0; i < ROWS; i )
    {
    for (int j = 0; j < COLS; j )
    {
    arr_sum[i] = arr_sum[i] + arr[i][j];//Assign array element to i //arr_sum[] sum of all elements in i row of array arr[][] (and so on )
    }
    }

    int max_index = 0;//variable that will store the index //of the maximum array element max_sum[]
    for (int i = 1; i < ROWS; i )
    {
    if (arr_sum[max_index] < arr_sum[i])
    {
    max_index = i;
    }
    }

    /*Since the size of the array arr_sum[] equal to the number of rows
    * arr array[][] then the index of the maximum element of the array arr_sum[]
    * matches the row index of the arr array[][] with the largest sum of elements
    */

    cout << "Index max row – " << max_index;

    return 0;
    }

  7. Why can you replace

    columnSum[rowNum] += ourMatrix[columnNum][rowNum];

    on

    columnSum[columnNum] += ourMatrix[rowNum][columnNum];

    and the result will be the same?

  8. multiplication table

    #include

    int main()
    {
    int i=0, j=0;
    short mas2d[10][10] = {};
    //array filling
    for (i = 0; i < 10; i ) {
    for (j = 0; j < 10; j ) {
    mas2d[i][j] = i * j;
    }

    }

    //displaying an array

    for (i = 0; i <10; i )
    {
    std::cout << std::endl ;
    {
    for (j = 0; j <10; j )
    std::cout << mas2d[i][j] << "\t";

    }
    }
    }

Leave a Reply

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