Filling Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Usupa
    New Member
    • May 2020
    • 1

    Filling Array

    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    int main()
    {
        setlocale(0, "Russian");
        cout<<"Введите размер массива: "<<endl;
        int n, a[20][20];
        cin >> n;
        if (n < 2) {
            cout << "Ошибка: размер массива должен превышать 1"<<endl;
        }
        else {
            //поиск k количества используемых простых чисел
    
            int k, c, p = 0, l;
            l = n;
            c = l;
            while (l >= 1) {
                k = c + p;
                c = k;
                p = l - 2;
                n = l - 2;
            }
    
            //Заполнение массива простых чисел
    
            int y[111];
            int z = 2;
            for (int i = 0; i < k; i++) {
                y[i] = z;
                z++;
            }
            int t = y[k - 1];
    
            //заполнение массива наибольшим простым числом t
    
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    a[i][j] = t;
                }
            }
    
            //заполнение правого верхнего треугольника
    
            int q=0;
            for(int i = 0; i < (n - 1) / 2 + 1; i++) {
                for (int j = n - 1; j > n - 2 - i; j--, q++) {
                    a[i][j] = y[q];
                    
                }
            }
          
            //заполнение правого нижнего треугольника
    
            for (int i = (n - 1) / 2 + 1; i < n; i++) {
                for (int j = n - 2 - i; j < n ; j++, q++) {
                    a[i][j] = y[q];
                    
                }            
            }
    
            //вывод массива
    
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    cout << a[i][j]<<" ";
                }
                cout << endl;
            }
    
    }



    Сорри за частичную нечитаемос ть и огромное количество переменных , я еще школьник))
    Что не так с кодом? Должен заполнять двумерный массив так, чтобы правая "четверть" заполнялас ь числами по порядку, начиная с двойки, а все остальное заполнялос ь наибольшим из чисел. (в оригинале заполнять надо не просто числами, а простыми, но это не главное)

    Буду благодарен за помощь

    -------------------

    Sorry for the partial unreadability and a huge number of variables, I'm still a schoolboy))
    What is wrong with the code? Must fill in a two-dimensional array so that the right “quarter” is filled with numbers in order, starting from two, and everything else is filled with the largest of the numbers. (in the original it is necessary to fill in not just numbers, but simple, but this is not the main thing)

    I will be grateful for the help
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 655

    #2
    The purpose of the code needs to be elaborated in English. There are so many things in the code making no sense at all.

    Comment

    • lewish95
      New Member
      • Mar 2020
      • 33

      #3
      Code:
      const array1 = [1, 2, 3, 4];
      
      // fill with 0 from position 2 until position 4
      console.log(array1.fill(0, 2, 4));
      // expected output: [1, 2, 0, 0]
      
      // fill with 5 from position 1
      console.log(array1.fill(5, 1));
      // expected output: [1, 5, 5, 5]
      
      console.log(array1.fill(6));
      // expected output: [6, 6, 6, 6]
      Last edited by gits; May 13 '20, 12:40 AM. Reason: added code tags

      Comment

      • dev7060
        Recognized Expert Contributor
        • Mar 2017
        • 655

        #4
        const array1 = [1, 2, 3, 4];

        // fill with 0 from position 2 until position 4
        console.log(arr ay1.fill(0, 2, 4));
        // expected output: [1, 2, 0, 0]

        // fill with 5 from position 1
        console.log(arr ay1.fill(5, 1));
        // expected output: [1, 5, 5, 5]

        console.log(arr ay1.fill(6));
        // expected output: [6, 6, 6, 6]
        The post is in the C++ category and 2D array is being used by the OP. How is this helping anyway?

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          What is going wrong with your code?
          • Compiler error?
          • Run-time fault?
          • Unexpected result?
          • Something else?

          It helps if you tell us the result you got and how it differs from the result you expect.

          Comment

          Working...