Tip 1: How to enter data into an array

Arrays are one of the simplest forms of structured data storage similar programs.Access to an array element containing specific information, there is the easiest way - through its serial number or code.At the beginning of the array is made of his data filling the declared type.Putting information on the index can be done in several ways.
The programming language Pascal, to enter data into the array, you need to directly address each element and assign it a value.Passage through the array is typically performed using a cycle.To fill a one-dimensional array, set a cyclic handler (for, while, or the like.), When dealing with two-dimensional array must be given two cycles (one another).Code Sample fill-dimensional array:
VaR mas: array [1..30] of integer;
i: integer;

begin for i: = 1 t î 20 DO
mas [i]: = 5;
in each of the 20 elements of the array will be written the number of mas 5.
To enter the values ​​in the two-dimensional array, use the following code:
VaR mass: array [1..5, 1..10] of integer;
i, j: integer;

begin for i: = 1 t î 5 DO
for i: = 1 t î 10 DO
mass [i, j]: = 6;
In this case, all the rows and columns of the array matrix are filled with mass number 6.
essentially similar procedure with differing syntax is in the programming language Basic:
DIM mass (5,6)
FOR i= 1 to m
FOR j = 1 to n
mass (i, j) = j;
where each array element is entered its serial number in a row (the second cycle of the index j).
However, most often in the array you want to enter the data read from the keyboard or from an external file.In this case, a cyclic handler is used is not an assignment operator, and one of the functions performed by, among other things, in the array write operation.An example of making an array of user input from the keyboard (the programming language C ++):

int array [4] [8];// two-dimensional array
for (int i = 0; i {
for (int j = 0; j cin & gt; & gt; array [i] [j]; // write to the array entered from the keyboard values ​​
When working with files read information and store it in a cell array also occurs in succession. The key is to comply with a given dimension and line types read values ​​from external media and elements to be filled in the array. An example of data input to the array of the file (the languageC):
char mass [3] [10];
char per;
int j = 0, t = 0;
FILE * pF = fopen ("File.txt", "rt");
while (t == 0)
fscanf (pF, "% c \ n", & amp; per); // read character values ​​from the file
for (int i = 0; i {
mass [i] [j] = per;// write the value of char in the array element
if (feof (pF))
t = 1;
j ++;

Tip 2: How to enter

array Arrays are a simple and effective form of orderly storage.They are used in almost every computer program.In most cases, the information therein is formed in the process of application.But sometimes you need to enter the array, receiving data from a particular source.
How to enter an array
you need
  • - a text editor or IDE;
  • - compiler C ++.
Type array of requesting data from the user.Calculate or request a number of elements that must be entered.Create an array of the desired size.Add to the desired location code of the program loop to iterate over all the elements.In a series of request data for each item, if necessary, checking the correctness of the data entry vvoda.Dlya can be used a variety of means.Using the scanf and wscanf C library is a classic way.However, these functions are not safe.Buffer overrun protection fault may occur.Streams C ++ provide a convenient and safe entering, but also not without drawbacks.The simplest example of filling the array using the standard input stream object might look like this: int aNumbers [10];for (int i = 0; i
Enter an array of the file. Use the function formatted input (fscanf, fwscanf) objects streams (eg, ifstream) to implement simple algorithms for reading. Track entry errors using methodsbad, fail, good, rdstate in the case of flows. A simple example of reading data from a file might look like this: int aNumbers [10]; std :: ifstream oFileStream ("filename.txt"); if (! oFileStream.fail ()){for (int i = 0; (i & gt; aNumbers [i];} else std :: cout
Enter the array directly in the program code in the form of static data. Use literal array to initialize the appropriate variables. For example, an int array of indeterminate length, is a static member of the class, and declared as: class CMyClass {... static const int m_anMyArray []; ...}; must be initialized as follows: const int CMyClass :: m_anMyArray []= {10, 20, 30, 40}; Similarly, the method can be entered arrays of structures of any complexity in the source code of programs.
  • how to enter the array
Board useful?
Yes No
Add a comment to the article
Characters left: 500
New advice from KakProsto
  • How to disable file plot.log in AutoCAD
  • What if bored at the computer
  • Elbrus - better, faster, more perfect
  • What is personal data and how to protect
  • Articles
Similar articles
  • How to create a RAID-array How to create a RAID-array
  • How to set up RAID-array Asconfigure the RAID-array
  • How to determine the maximum element in an array How to determine the maximum element in the array
  • How to make an array How to make an array
  • How to assign a value to the array How to assign a value to an array


Related Posts