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.

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

  1. errors found but do not know how to fix tested and in some cases an array of clogged garbage after the string “s[i] += a[i][j];” loop

  2. In general, I figured. I wrote a program in visual studio, and then if you do not fill the array in it waste is stored. And if you fill the array with zeros, and then do the same thing that turns out as required

  3. #include
    #include
    #include
    #include
    #include
    using namespace std;

    int main ()
    {
    const int arr=3;
    int more[arr][arr]={};
    int sum[arr]={};
    int maxi=0;
    srand(time(NULL));

    for (int i=0;i<arr;i )
    {
    for(int j=0;j<arr;j )
    {

    but[i][j]=10+rand()%100;

    cout<<setw (10)<< but[i][j]<<setw (10);

    }

    cout<<endl;

    }
    for (int i=0;i<arr;i )
    {
    for(int j=0;j<arr;j )
    {
    sum[i]+but =[i][j];
    }
    cout<<setw (10)<<"summa stroki "<<i+1<<" | " <<sum[i]<<endl;

    }
    for (int i=1;iSum[maxi])
    {
    maxi = i;
    }

    }

    cout<<maxi;
    return 0;
    }

  4. I can not bring the amount of lines in the third task, filled with an array of shows, and then as though neither of which net.Vot code: #include
    #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, "rus");
    const int First = 2;
    const int Second = 5;
    int Array[First][Second] = {};
    int sumArray[First]{};
    srand(time(NULL));

    for (int i = 0; i < First; i++)
    {
    for (int a = 0; a < Second; a++)
    {
    Array[i][a] = 10+rand() % 90;
    cout << Array[i][a] << " ";
    sumArray[i] +=Array[i][a];
    }
    cout << endl<<endl;
    }

    for (int i = 0; i << First; i++)
    {
    cout << "Сумма " << i + 1 << "-й строки:" << sumArray[i] << endl;
    }

    return(0);
    }

    With that, and the second problem is the same problem, the amount of lines is not displayed, the remaining commands are not tested yet ,I decided to deal with this

    1. The cout can not display a whole array (sumArray[i] ) one operator, but you can only display element by element.

      Two-dimensional arrays can be displayed (in the same way as filling) Only nested loop.

      1. But in addressing the third task the amount of rows displayed in exactly the same item by item through a cycle in fact, or that I do not understand
        for (int i = 0; i < MatrixSize; i++)
        {
        cout << i + 1 << "-я строка: " << rowSum[i] << endl;
        }

      2. A problem in the second,This rowSum terms[rowNum] += ourMatrix[rowNum][columnNum];
        columnSum[rowNum] += ourMatrix[columnNum][rowNum];added to the amount entered,I do not understand why rowsum[rownum]for example, He knows,cout that you want to display then the sum of three numbers (the amount of lines)???

      1. even kill, I still can not understand ,what is wrong =((because I have it too one-dimensional
        int sumArray[First]{};

    2. I'm so tired of your comments, that copied exactly from the screen and fulfilled (Linux, GCC compiler):

      [olej@dell comments]$ g++ c1.cc -o c1
      c1.cc: В функции «int main()»:
      c1.cc:13:24: предупреждение: extended initializer lists only available with -std=c++11 or -std=gnu++11
      int sumArray[First]{};
      ^
      [olej@dell comments]$ ./c1
      47 17 72 68 39

      59 71 76 49 55

      1. Thank you for your patience and for, they tried to help me, but the error is was very stupid, due to my not care :
        for (int i = 0; i << First; i++)
        Once the sign '<'

    3. good error … funny. Because i << First – it is quite valid expression, giving the value of i shifted to the left on the First binary positions (i.e.. i * 2^First).
      A 2nd cycle parameter for this value is compared with 0 on the same / not equal (lie / truth).
      ;-)

      1. It is not perceived as an error. When using the initialization with braces, permitted to discard the symbol =
        But it is better to write clearly

    1. Because srand() – this is primary initializing a random number generator, which should only be called 1 times, at the beginning. And maybe never called – then the sensor is initialized by default.

  5. I can not find the error.. I set the number of rows and columns of 10x10, and displayed on the screen 5x5.

  6. Hello! I can not understand, why an equal number of rows and columns of a two-dimensional array of count amount of rows and columns program executed correctly, and if not equal (for example, 3 line and 5 columns, or vice versa) – not correctly? This applies only to count the amount of column values.
    How to solve this problem?

Leave a Reply

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