Download presentation
Presentation is loading. Please wait.
1
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.
2
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 dept 1 is MENS dept 2 is WOMENS dept 3 is GIRLS dept 4 is BOYS This shows the basic information that I have to work with. It is laying out the problem.
3
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.
4
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.
5
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 { }.
6
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 12121Jennifer Ames 12345Stephen Daniels 13456Carl Hersey 14567Linda Anderson 17890John Alden 18900Peter Lincoln 21212Ann Reynolds 22222Donald Costa 23456Joyce Raymond 24567David Jones 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.
7
1 2 3 4 MENS WOMENS GIRLS BOYS ARRAY: deptName
11111Mary Smith INPUT 12121Jennifer Ames 12345Stephen Daniels 13456Carl Hersey 14567Linda Anderson 17890John Alden 18900Peter Lincoln 21212Ann Reynolds 22222Donald Costa 23456Joyce Raymond 24567David Jones ARRAY: deptName 1 2 3 4 MENS WOMENS GIRLS BOYS PROCESSING: move deptName (deptNum) to deptName-PR. OUTPUT: Mary Smith WOMENS $40,000.00 Jennifer Ames GIRLS $40,000.00 Stephen Daniels BOYS $40,000.00 Carl Hersey MENS $40,000.00 Linda Anderson WOMENS $35,000.00 John Alden MENS $45,000.00 Peter Lincoln BOYS $40,000.00 Ann Reynolds GIRLS $35,000.00 Donald Costa MENS $35,000.00 Joyce Raymond WOMENS $35,000.00 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.