How do you store a bunch of similar stuff?

Slides:



Advertisements
Similar presentations
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Advertisements

Introducing Arrays We will often need to store collections of information –a list of names to sort –a list of values to compute averages, standard deviation,
Programming, an introduction to Pascal
Computer Science 1 How do you store a bunch of similar stuff?
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Computer Science 2 Arrays of Records. First Record Program Input an unknown number of Team Names, School names, and Total scores. (While loop) –Output:
Lecture 5 of Computer Science II
Arrays 1. One dimensional arrays - Review 2. Using arrays
Complex data types Complex data types: a data type made of a complex of smaller pieces. Pascal has four very commonly used complex data types: strings,
New Structure Recall “average.cpp” program
Chapter 8 Arrays Objectives
Multi-dimensional Array
CIS 170 Education for Service-- snaptutorial.com.
CIS 170 Teaching Effectively-- snaptutorial.com
Computer Science Dynamics.
Java Fix a program that has if Online time for Monday’s Program
How do you store a bunch of similar stuff?
Chapter 8 Arrays Objectives
Computer Science II First With files.
Computer Science II First With files.
int [] scores = new int [10];
Truth tables: Ways to organize results of Boolean expressions.
Review Dry Run Taking Names Online time Math is Good
Computer Science 2 Arrays of Records.
Computer Science 1 Get out your notebook
Computer Science 2 Review the Bubble Sort
Continue on the Array of Records program
Computer Science 2 Arrays of Records.
Module 4 Loops.
Computer Science 2 Getting an unknown # of …. Into an array.
Dry run Fix Random Numbers
Program Options: 10 Minutes online
Truth tables: Ways to organize results of Boolean expressions.
Java Fix a program that has if Online time for Monday’s Program
Starting Out with Programming Logic & Design
Computer Science Dynamics.
int [] scores = new int [10];
Computer Science Sorting.
Computer Science 1 1/19/2011 On the record (paper) write the following
Computer Science 2 Arrays of Records.
How do you store a bunch of similar stuff?
Computer Science 2 Tally Arrays 2/4/2016.
CS 2 Records 2/22/2018.
Truth tables: Ways to organize results of Boolean expressions.
Chapter 8 Arrays Objectives
Game Over Module 4 Lesson 2.
AP Java 9/21/2018.
Computer Science 1 Online time for Graphics Program Random review
How can you make a guessing game?
Computer Science II Second With files.
Sorting Develop a sorting algorithm
Continue with Life, Magic and Catch -up
Computer Science 2 More Trees.
Computer Science 1 while
Computer Science 1 For..do loop Dry Run Take notes on the For loop
How can you make a guessing game?
Computer Science
Welcome Back CS 2 2/4/2013 On the record (paper) write the following
Computer Science I: Get out your notes.
Continue on the Valentines program
int [] scores = new int [10];
Computer Science 1 while
Computer Science 1 Get out your notebook
Computer Science 1 Get out your notebook
Dry Run Fix it Write a program
Algorithms For use in Unit 2 Exam.
How do you do the following?
Tic-Tac-Toe Game Engine
Computer Science II First With files.
Presentation transcript:

How do you store a bunch of similar stuff? Computer Science 1 How do you store a bunch of similar stuff?

Goals Understand when to use arrays. Be able to read a program that uses one dimensional arrays. Be able to write the code to create arrays. Write a program that uses arrays.

What do you think this does? Program arraynyday; Type Numtype = array[1..5] of integer; Var Re:numtype; bound, cord, mind:integer; Begin For bound:= 1 to 5 do Re[bound]:= 3*bound; For cord:= 5 downto 2 do Re[cord]:= re[cord]+ cord; For mind:= 1 to 5 do Writeln(re[mind]); For bound:= 1 to 4 do Re[bound]:=re[bound + 1]; Re[5]:=re[2]; End. What do you think this does?

Storing stuff Keep track of all of the actions on the ATM Save names for future reference. Save a bunch of numbers to put in order The position of game pieces in checkers The colors for a chunk of the screen.

Arrays to the rescue Pebbles Sue Fred Wilma Betty Dino Bam Bam Barney What is it? A type of variable that can hold several values of the same type. The names of 8 friends. Your 10 best swimming times. The total points for the 12 players on the basketball team. 3 x 3 tic-tac-toe board 37 account balances.

When should you use an array? When you have a large group of similar information that you are going to use more than once. When you are sorting

Types of uses General storage Tallying Sorting Save 20 names Allows you to be able to look at the information again. Tallying Counting how often events occur. Statistics Sorting Putting stuff in order.

Steps for making an array Define it In the TYPE section Declare it In the var section Use it In the code section

Define it Program sample; Uses Const Type ScoresType = array[1..15] of integer; CapitalLettersType = array[‘A’..’Z’] of string; TicTacToeType = array[1..3, 1..3] of char’ SeatingType = array[‘A’..’M’, 1..50] of string;

Declare it Var scores: Scorestype; capitalLetters : CapitalLettersType; ticTacToe: TicTacToeType; seating:SeatingType; count:integer;

Use it Begin Scores[1] := 0; Writeln(‘Please enter a score’); Readln(scores[2]); How can you get all 10 scores with using 10 writeln/readlns?

Other stuff you can do Scores[2] := scores[3]; Scores[count]:=scores[scores[2]]; {?} Scores[count-3] := scores[count-4]; If scores[count] > 10 then Writeln(scores[count]);

What do you recall about… Arrays Type [1..5] [1..5,1..5] Defining arrays Declaring Arrays Using Arrays

Good array errors Writeln(scores); {You need a loop to show all of the values} scoresType[2] := 20; Scores[27]:= 8; {Out of bounds} Scores:=6; {No address given}

Dry run the following Program arraysample;{Dry run the following} Type Arraytype = array[1..5] of integer; Var Numbers:Arraytype; Count:integer; Begin For count:= 5 downto 1 do Numbers[count]:= count; Numbers[2]:= numbers[3] + 5; Numbers[3]:= numbers[2+2]; Numbers[5]:=2; For count:= 1 to 5 do Writeln(Numbers[count]); {This next one is weird} numbers[numbers[5]] := numbers[3]*5; for count:= 1 to 5 do writlen(numbers[count]); readln; end. Dry run the following

Your turn… Write the type and var section to create arrays to store the following 10 names 15 test scores

Try code Write the code to … Input 10 names into the array created previously Output all of the names Input 15 test scores Output all of the test scores Find and show the highest test score

Program options Enter 10 names and show the names in reverse order. Input: 10 scores Output: The number of scores within 10 points of the average of the 10 scores. (You’ll need to calculate the average before going back and seeing which scores are within 10 points of the average.) Create a random compliment generator using an array to store the compliments. Using a while loop (so the user can be complimented often) have the computer display one of at least 5 random compliments each time the user would like to be complimented.