1 / 7

Introduction to the Array

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.

roman
Download Presentation

Introduction to the Array

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to the Array Please use speaker notes for additional information!

  2. 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

  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” else if deptNo = 3 deptName = “GIRLS” else if deptNo = 4 deptName = “BOYS” endif endif endif endif

  4. 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”

  5. 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

  6. 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

  7. 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

More Related