70 likes | 171 Views
Introduction to the Array. Please use speaker notes for additional information!. 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.
E N D
Introduction to the Array Please use speaker notes for additional information!
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 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” else if deptNo = 3 deptName = “GIRLS” else if deptNo = 4 deptName = “BOYS” endif endif endif endif
Second: 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”
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
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
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: movedeptName (deptNum) todeptName-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