Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 2 Arrays of Records.

Similar presentations


Presentation on theme: "Computer Science 2 Arrays of Records."— Presentation transcript:

1 Computer Science 2 Arrays of Records

2 First Record Program Input an unknown number of Team Names, School names, and Total scores. (While loop) Output: The winning team. That is the team with the highest score. (If there is a tie, just show the first team with the top score.) (Note, you don’t need an array for this program.) Push: Same input as above, and show the schools with the top five scores. (You do need an array and a sort for this one.)

3 Goals Review making records Figure out how to make an array of records
Write a fantastic program with arrays, records and candy bars!

4 Record Review Error Potential Error:Putting a ; here
Program RecordsExample; type StudType = record name:string; age:integer; gpa:real; end; var Student1,Student2:Studtype; begin writeln(‘Please enter your name’); readln(Student1.name); writeln(‘Please enter your age’); readln(Student1.age); writeln(‘Please enter another name’); readln(Student2.name); writeln(‘Please enter another age’); readln(Student2.age); if (Student1.age) > (Student2.age) then writeln(Student1.name,’ is older’) else writeln(Student1.name, ‘ is not older ‘); Writeln(Student1); end. Record Review Potential Error:Putting a ; here Potential Error:Forgetting the ‘end.’ Error

5 On Notepad, word, … As the class goes on we will add to this.
Write the declarations (Type and var) for a variable that needs to hold… Name, school, Valentine card type (Small or Large), cost, type of candy (Candy bar, skittles, …). On Notepad, word, … As the class goes on we will add to this.

6 How would you… Store a large group of records? How can you set it up?
How can you access it? How can you search it?

7 Array of Records Sample
Draw a picture for the variables in this program. program arrayofrecsample; type rectype = record name:string; age:integer; end; arrayType = array[1..10] of rectype; var names:arrayType; temp:rectype; count, count2:integer;

8 Record how this code changes the variable.
More of the sample Record how this code changes the variable. begin names[1].name:='Fred'; names[1].age := 72; Writeln('Please enter a name'); readln(names[2].name); writeln('Please enter their age'); readln(names[2].age); temp.name:='Betty'; temp.age := 29; names[3]:= temp;

9 You select the info to enter for the readlns.
Using a for do loop You select the info to enter for the readlns. for count := 4 to 6 do begin writeln('Please enter your name'); readln(names[count].name); writeln('Please enter the age'); readln(names[count].age); End;

10 What about an unknown number (within limits)?
count :=0; writeln('Please enter a name or ZZZ to quit'); readln(temp.name); while (temp.name <> 'ZZZ')and (count <10) do begin inc(count);//Same as count:=count+1; writeln('Please enter the age'); readln(temp.age); names[count]:= temp; // Copies the entire record end;

11 Showing the data from a for loop
writeln('Name':10, 'Age':10); for count2:= 1 to count do begin writeln(names[count2].name:10, names[count2].age:10); end; //Or with names[count2] do writeln(name:10, age:10); end.

12 Write the declarations
Store 5 Name, school, Valentine card, cost, type of candy. Use the record you declared at the beginning of class.

13 Dry Run!! program Wrecko; type rectype = record a:integer; b:real;
end; arraytype = array[1..5] of rectype; var data: arraytype; count,x,y:integer; begin for count := 1 to 5 do data[count].a:= count; data[count].b:= count *3; for count:= 1 to 5 do data[count].b:= data[count].b +data[count].a; for count:= 5 downto 1 do writeln(data[count].a:5, data[count].b:5:2); for count:= 1 to 4 do begin data[count].a := data[count+1] .a; data[count+1].b:=data[count].b; end; for count:= 1 to 5 do writeln(data[count].a,data[count].b:5:2) end.

14 Program Get 5 Names, schools, type of Valentines cards, costs, types of candy. Show all the information in a chart. Let the user enter a name and the program will return the type of candy. (Tell them if the person is not in the book) Modify this to be set up as a menu. (Show all, Find person, Find School’s people, total sales, quit) Let the user enter the name of the school, and it will show all of the students from that school. Write the code to find the total of all of the sales. Pushes: Improve user friendliness of the program. Include an ‘Add another person’ option to the menu. Calculate how many of each candy you will need to order. Let it support holding an unknown number of records (but less than 100) Write the program using procedures.


Download ppt "Computer Science 2 Arrays of Records."

Similar presentations


Ads by Google