Massive in the area



  • I've got a two-strong mass. I need to assign the meaning of the accidental element of the mass to any condition. Massive is big, and if you write arr[rand()][rand()]The program is a long guess. With each step, the number of elements that meet the condition decreases (and at the end, when there is one element that once again responds to the condition, the programme is, for a very long time, guessing it).

    To realize this problem, I have created a new set of elements that are elements of an previously created body.

    arr1[size] = {arr2[0][0], arr2[0][1], arr2[1][0], arr2[1][1]};
    

    So, I need the element to be assigned. arr1[0]the same value was assigned to the mass arr2[0][0]arr1[0]arr2[0][1]♪ We need all the elements to change at random, except those on the diagonals. And I'm getting it.

    arr1[0] = 5

    arr2[0][0] = not exactly 5

    How? Or is there another option? Dynamics haven't been through yet.

    #include <iostream>
    #include <time.h>
    

    using namespace std;

    void main() {
    srand(time(0));
    const int SIZE = 5;
    char array1[SIZE][SIZE] = { 0 };

    // Заполняем массив первоначальными значениями
    for (int i = 0; i &lt; SIZE; i++) {
        for (int j = 0; j &lt; SIZE; j++) {
            if (i == j || i == SIZE - j - 1) // Если диагональ
            {
                array1[i][j] = '0';
            }
            else
                array1[i][j] = ' ';
        }
        cout &lt;&lt; endl;
    }
    
    // Максимальное количество элементов, что соответствуют размеру 5
    char array2[SIZE*3+1] = { 0 }; 
    int k = 0;
    cout &lt;&lt; "Array1\n";
    for (int i = 0; i &lt; SIZE; i++) {
        for (int j = 0; j &lt; SIZE; j++) {
            cout &lt;&lt; array1[i][j];
            if (i == j || i == SIZE - j - 1)
            {
                array2[k] = array1[i][j];
                ++k;
            }
        }
        cout &lt;&lt; endl;
    }
    cout &lt;&lt; endl;
    cout &lt;&lt; "Array2\n";
    int rand_num = rand() % k;
    array2[rand_num] = 'X';
    for (int i = 0; i &lt; k ; i++) {
        cout &lt;&lt; array2[i] &lt;&lt; "  ";
    }
    
    cout &lt;&lt; endl;
    cout &lt;&lt; "Change array1\n\n";
    
    // Тут ничего не меняется, а мне нужно
    // чтобы элементу массива array[i][j] присвоилось значение Х
    for (int i = 0; i &lt; SIZE; i++) {
        for (int j = 0; j &lt; SIZE; j++) {
            cout &lt;&lt; array1[i][j];
        }
        cout &lt;&lt; endl;
    }
    

    }



  • It's just that you copy the meanings of the elements of one body into another, and then it's different things that don't connect.

    As part of your approach, you just need to keep your indexes on the elements of your first set:

    #include <iostream>
    #include <time.h>
    

    using namespace std;

    void main() {
    srand(time(0));
    const int SIZE = 5;
    char array1[SIZE][SIZE] = { 0 };
    //заполняем массив первоначальными значениями
    for (int i = 0; i < SIZE; i++){
    for (int j = 0; j < SIZE; j++){
    if (i == j || i == SIZE - j - 1) //если диагональ
    {
    array1[i][j] = '0';
    }
    else
    array1[i][j] = ' ';
    }
    cout << endl;
    }
    char * array2[SIZE*3+1] = { 0 }; // максимальное количество элементов, что соответствуют размеру 5
    int k = 0;
    cout << "Array1\n";
    for (int i = 0; i < SIZE; i++){
    for (int j = 0; j < SIZE; j++){
    cout << array1[i][j];
    if (i == j || i == SIZE - j - 1)
    {
    array2[k] = &array1[i][j];
    ++k;
    }
    }
    cout << endl;
    }
    cout << endl;
    cout << "Array2\n";
    int rand_num = rand() % k;
    *array2[rand_num] = 'X';
    for (int i = 0; i < k ; i++){
    cout << *array2[i] << " ";
    }
    cout << endl;
    cout << "Change array1\n\n";
    //тут ничего не меняется, а мне нужно чтобы в элемент массива array[i][j] отобразилось значение Х
    for (int i = 0; i < SIZE; i++){
    for (int j = 0; j < SIZE; j++){
    cout << array1[i][j];
    }
    cout << endl;
    }
    }


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2