Introduction to the Array Please use speaker notes for additional information! This presentation will introduce some of the concepts of arrays. Note that in COBOL, arrays are sometimes called tables so in the followup slide presentation that gives examples in COBOL, the array is called a table.
deptNo deptName 1 dept 1 is MENS Assume that I have a file that contains a department number along with other data. The layout of the file is: empIdno, empName, deptNo and Salary. Assume also that I have a report where I want to print out the department name. To solve this problem, I am going to use an array. deptNo deptName 1 dept 1 is MENS 2 dept 2 is WOMENS 3 dept 3 is GIRLS 4 dept 4 is BOYS This shows the basic information that I have to work with. It is laying out the problem.
This solution works, but if there were a lot of departments, it would go on forever. An array provides a better solution. Pseudocode: if deptNo = 1 deptName = “MENS” else if deptNo = 2 deptName = “WOMENS” if deptNo = 3 deptName = “GIRLS” if deptNo = 4 deptName = “BOYS” endif One solution is to use IF statements. This works fine when you only have 4 departments, but it would be very code heavy if you had 100 departments.
Second: MENS WOMENS GIRLS BOYS deptName (1) deptName (2) deptName (3) The solution is to use a pointer to point to the different elements as first use of deptName, second use of deptName, third use of deptName and fourth use of deptName. By using the pointer, we make the name unique. The pointer is usually called an index or subscript in programming. MENS WOMENS GIRLS BOYS deptName (1) deptName (2) deptName (3) deptName (4) First: When I establish an array, I set up a table or list of the elements I want in my array and I give each element the same name. In this case, I am calling each one deptName. The problem is that in programming we know that each variable needs a unique name. This presents a problem. Now we know that: deptName(1) = “MENS” deptName(2) = “WOMENS” deptName(3) = “GIRLS” deptName(4) = “BOYS” The pointer is usually called an index or subscript as noted above. Also, in some languages the index/subscript/pointer starts with 1, in other languages it starts with 0 and with other languages you can choose whether you want it to start with 1 or 0.
move deptName (deptNo) to the line To apply this to our problem, the data coming in has the following data: empIdno empName deptNo salary MENS WOMENS GIRLS BOYS deptName (1) deptName (2) deptName (3) deptName (4) Remember our problem is to display or print the department name on our report. If we use deptNo as the pointer we can accomplish this goal: move deptName (deptNo) to the line The syntax for the statement to move things varies from language to language. However, in most languages you have the data in the array followed by the index/pointer in with ( ) or [ ] or { }.
empName deptNo salary 11111Mary Smith 24000000 Before we apply this to our problem, lets look at the data on the file: empIdno empName deptNo salary 11111Mary Smith 24000000 12121Jennifer Ames 34000000 12345Stephen Daniels 44000000 13456Carl Hersey 14000000 14567Linda Anderson 23500000 17890John Alden 14500000 18900Peter Lincoln 44000000 21212Ann Reynolds 33500000 22222Donald Costa 13500000 23456Joyce Raymond 23500000 24567David Jones 13000000 This slide shows the layout of the data file. The first field is empIdno, the second field is empName, the third field is deptNo and the fourth field is salary. Hopefully by color coding the fields, the data stands out.
1 2 3 4 MENS WOMENS GIRLS BOYS ARRAY: deptName 11111Mary Smith 24000000 INPUT 12121Jennifer Ames 34000000 12345Stephen Daniels 44000000 13456Carl Hersey 14000000 14567Linda Anderson 23500000 17890John Alden 14500000 18900Peter Lincoln 44000000 21212Ann Reynolds 33500000 22222Donald Costa 13500000 23456Joyce Raymond 23500000 24567David Jones 13000000 ARRAY: deptName 1 2 3 4 MENS WOMENS GIRLS BOYS PROCESSING: move deptName (deptNum) to deptName-PR. OUTPUT: 11111 Mary Smith WOMENS $40,000.00 12121 Jennifer Ames GIRLS $40,000.00 12345 Stephen Daniels BOYS $40,000.00 13456 Carl Hersey MENS $40,000.00 14567 Linda Anderson WOMENS $35,000.00 17890 John Alden MENS $45,000.00 18900 Peter Lincoln BOYS $40,000.00 21212 Ann Reynolds GIRLS $35,000.00 22222 Donald Costa MENS $35,000.00 23456 Joyce Raymond WOMENS $35,000.00 24567 David Jones MENS $30,000.00 The first input record has department 2. I use that deptNum to go to the array and extract the deptName that deptNum is pointing to. That means WOMENS is extracted and moved to the print line. On the second input record, the department is 3. This takes the third element from the array, GIRLS and moves it to the line. On the third input record, the department is 4. This takes the fourth element from the array, BOYS and moves it to the line. On the fourth input record, the department is 1. This takes the first element from the array, MENS and moves it to the line. See the COBOL table presentation for an example in COBOL.