473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Filling Array

1
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     setlocale(0, "Russian");
  9.     cout<<"Введите размер массива: "<<endl;
  10.     int n, a[20][20];
  11.     cin >> n;
  12.     if (n < 2) {
  13.         cout << "Ошибка: размер массива должен превышать 1"<<endl;
  14.     }
  15.     else {
  16.         //поиск k количества используемых простых чисел
  17.  
  18.         int k, c, p = 0, l;
  19.         l = n;
  20.         c = l;
  21.         while (l >= 1) {
  22.             k = c + p;
  23.             c = k;
  24.             p = l - 2;
  25.             n = l - 2;
  26.         }
  27.  
  28.         //Заполнение массива простых чисел
  29.  
  30.         int y[111];
  31.         int z = 2;
  32.         for (int i = 0; i < k; i++) {
  33.             y[i] = z;
  34.             z++;
  35.         }
  36.         int t = y[k - 1];
  37.  
  38.         //заполнение массива наибольшим простым числом t
  39.  
  40.         for (int i = 0; i < n; i++) {
  41.             for (int j = 0; j < n; j++) {
  42.                 a[i][j] = t;
  43.             }
  44.         }
  45.  
  46.         //заполнение правого верхнего треугольника
  47.  
  48.         int q=0;
  49.         for(int i = 0; i < (n - 1) / 2 + 1; i++) {
  50.             for (int j = n - 1; j > n - 2 - i; j--, q++) {
  51.                 a[i][j] = y[q];
  52.  
  53.             }
  54.         }
  55.  
  56.         //заполнение правого нижнего треугольника
  57.  
  58.         for (int i = (n - 1) / 2 + 1; i < n; i++) {
  59.             for (int j = n - 2 - i; j < n ; j++, q++) {
  60.                 a[i][j] = y[q];
  61.  
  62.             }            
  63.         }
  64.  
  65.         //вывод массива
  66.  
  67.         for (int i = 0; i < n; i++) {
  68.             for (int j = 0; j < n; j++) {
  69.                 cout << a[i][j]<<" ";
  70.             }
  71.             cout << endl;
  72.         }
  73.  
  74. }



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

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

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

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
May 11 '20 #1
4 2560
dev7060
636 Expert 512MB
The purpose of the code needs to be elaborated in English. There are so many things in the code making no sense at all.
May 12 '20 #2
Expand|Select|Wrap|Line Numbers
  1. const array1 = [1, 2, 3, 4];
  2.  
  3. // fill with 0 from position 2 until position 4
  4. console.log(array1.fill(0, 2, 4));
  5. // expected output: [1, 2, 0, 0]
  6.  
  7. // fill with 5 from position 1
  8. console.log(array1.fill(5, 1));
  9. // expected output: [1, 5, 5, 5]
  10.  
  11. console.log(array1.fill(6));
  12. // expected output: [6, 6, 6, 6]
May 12 '20 #3
dev7060
636 Expert 512MB
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]
The post is in the C++ category and 2D array is being used by the OP. How is this helping anyway?
May 12 '20 #4
donbock
2,426 Expert 2GB
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.
May 12 '20 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Raptor | last post by:
I'm using a single script to generate a table with <input>s in each row. I fill the array with initial values, then write it out to the table and let the user edit the values. Something like: ...
1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
8
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and...
3
by: MSDousti | last post by:
Hello I want to write a C# (or VB.NET) program which reads the structure of a PE (odinary win32 executable) file. These files have a long header (512 bytes or so). The definition of the header...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
29
by: foker | last post by:
I have this problem where I have 2 text files, one with student name, id#, # of courses and course #, the second file has course name and course number. I want to make a multidimensional array that...
2
by: coolindienc | last post by:
Hello, I have tried to fill array from a file and sort it by first column and then by second column. However, I am getting several errors. Please someone show me where is/are my mistake/s. ...
10
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
2
by: triphoppa | last post by:
I'm having a hard time discovering how to fill an array of structures from a file. This is what I'm trying to do. I am going to open a file from the command line. In the file are a bunch of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.