DOC PREVIEW
TAMU CSCE 110 - 16-oneDimensionalArrays
Type Miscellaneous
Pages 32

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ArraysTypical (although simplified) ProblemWhy Bother With Composite Types?Why Bother With Composite Types? (2)With Bother With Composite Types? (3)What’s NeededData TypesDeclaring ArraysAccessing Data In The ArrayAssigning Data To The ArrayAssigning Data To The Array (2)Assigning Data To The Array (3)Accessing The Data In The ArrayAccessing The Data In The Array (2)Revised Version Using An ArrayClass Example Using An Array (2)Passing Arrays As ParametersPassing Arrays As Parameters (2)Passing Arrays As Parameters: An ExamplePassing Arrays As Parameters: An Example (2)Passing Arrays As Parameters: An Example (3)Passing Arrays As Parameters: An Example (4)Returning Arrays From FunctionsSegmentation Faults And The Array BoundsSlide 25Slide 26Slide 27The String TypeBenefits Of The String TypeMarking The End Of The ArrayThe Contents Of The ListsStrings Are A Built-In Type1J. Michael MooreArraysCSCE 110From James Tam’s materialJ. Michael MooreTypical (although simplified) ProblemWrite a program that will track student grades in a class. The grades are saved as percentages. The program should allow the user to enter the grade for each student. Then it will display the grades for the whole class along with the average.J. Michael MooreWhy Bother With Composite Types?const CLASS_SIZE = 5;begin var stu1 : real; var stu2 : real; var stu3 : real; var stu4 : real; var stu5 : real; var total : real; var average : real;J. Michael MooreWhy Bother With Composite Types? (2) write('Enter grade for student number 1: '); readln(stu1); write('Enter grade for student number 2: '); readln(stu2); write('Enter grade for student number 3: '); readln(stu3); write('Enter grade for student number 4: '); readln(stu4); write('Enter grade for student number 5: '); readln(stu5); total := stu1 + stu2 + stu3 + stu4 + stu5; average := total / CLASS_SIZE; writeln('The average grade is ', average:6:2, '%');J. Michael MooreWith Bother With Composite Types? (3)(* Printing the grades for the class. *) writeln('Student1: ', stu1:6:2); writeln('Student2: ', stu2:6:2); writeln('Student3: ', stu3:6:2); writeln('Student4: ', stu4:6:2); writeln('Student5: ', stu5:6:2);end.NO!J. Michael MooreWhat’s Needed•A ___________ variable that is a collection of another type.•The ___________ variable can be manipulated and passed throughout the program as a __________ entity.•At the same time each ___________ can be accessed ________________.•An array!J. Michael MooreData Typessimplestructuredpointerordinal realpredefinedbooleancharintegerprogrammer-definedenumerated subrangearray recordset filepredefined programmer-definedtext__________________J. Michael MooreDeclaring ArraysAs with any other variable, you must first create an array in memory by declaring an instance.Format: name: array [low index..high index] of element type;Example:constCLASS_SIZE = 5;: :var classGrades : array [1..CLASS_SIZE] of real;classGrades [1] [2] [3] [4] [5]J. Michael Moore If you are accessing ____________________, you need to indicate _______________ that you wish to access.•Done via the array index e.g., “classGrades[2]”To manipulate an array you need to first indicate which array is being accessed•Done via the name of the array e.g., “classGrades”Accessing Data In The ArrayclassGrades [1] [2] [3] [4] [5]classGrades [1] [2] [3] [4] [5]Using only the name of the array refers to ________________________________Use the array name and the index refers to ___________________________________J. Michael MooreAssigning Data To The ArrayFormat:(_____________________) (_____________________)nameOfArray := value; nameOfArray[index] := value;Examples (assignment via the assignment operator): (_____________________) (_____________________)firstArray := secondArray; classGrades[1] := 100;J. Michael MooreAssigning Data To The Array (2)Examples (assigning values via read or readln):(_________________________)write('Input grade for student 1: '); readln(classGrades[1]);(_________________________)for i: = 1 to CLASS_SIZE dobeginwrite('Input grade for student # ', i, ': ');readln(classGrades[i]);end;J. Michael MooreAssigning Data To The Array (3)Example: (Whole array – all elements: ____________________ arrays only) var charArray : array [1..SIZE] of char; readln(charArray);Important note: arrays cannot be ________________________ to read or readln (exception: one-dimensional character arrays can be passed)J. Michael MooreAccessing The Data In The ArrayExamples (displaying information): (_________________________) writeln('Grade for student 1: ',classGrades[1]:6:2); (_________________________)for i := 1 to CLASS_SIZE dowriteln('Grade for student # ', i:2, ': ', classGrades[i]:6:2);J. Michael MooreAccessing The Data In The Array (2)Example: (Whole array – all elements: Character arrays only)var charArray : array [1..SIZE] of char;write(charArray);Important note: arrays cannot be _______________________ to write or writeln (exception: one-dimensional character arrays can be passed)J. Michael MooreRevised Version Using An Arrayconst CLASS_SIZE = 5;begin var classGrades : array [1..CLASS_SIZE] of real; var i : integer; var total : real; var average : real;J. Michael MooreClass Example Using An Array (2)total := 0; for i := 1 to CLASS_SIZE do begin write('Enter grade for student # ', i, ': '); readln (classGrades[i]); total := total + classGrades[i]; end;average := total / CLASS_SIZE;writeln;writeln('The average grade is ', average:6:2, '%');for i := 1 to CLASS_SIZE do writeln('Grade for student # ', i, ' is ', classGrades[i]:6:2, '%');J. Michael MoorePassing Arrays As Parameters1. Declare a type for the array.e.g. constCLASS_SIZE = 5;type Grades = array [1..CLASS_SIZE] of real;•Declaring a type does not create an instance-A type only describes the attributes of a new kind of variable that can be created and used (e.g. a _________________).-_________________________________.J. Michael MoorePassing Arrays As Parameters (2)2. Declare an instance of this type.e.g., var lecture01 : Grades;•______________________________!3. Pass the instance to functions/procedures as you would any other parameter.(Function/procedure call)displayGrades (lecture01, average);(Function/procedure definition)procedure displayGrades (lecture01 : Grades; average : real);J. Michael


View Full Document

TAMU CSCE 110 - 16-oneDimensionalArrays

Type: Miscellaneous
Pages: 32
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download 16-oneDimensionalArrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view 16-oneDimensionalArrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view 16-oneDimensionalArrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?