Download presentation
Presentation is loading. Please wait.
1
Computer Science 2 Tally Arrays 2/4/2016
2
Learning Objectives Students will be able to use arrays to keep track of votes for an election
3
Dry run practice. tot:=0; for count: = 1 to 3 do num[count]: = 0 ;
Begin tot:= count mod 3 + 1; inc(num[tot]); End; For count:= 1 to 3 do writeln(num[count]);
4
Tally Array notes Types of uses for an array
General: Storing lots of stuff Tallying: Storing votes for lots of events Sorting: Putting stuff in order
5
Election time We’ll be writing the code to run a small election between thee candidates. It will get votes and show the results of the election. Can this be done without using arrays?
6
Getting Ready to Code Hands on Pseudo Code Dry Run Code
7
while (vote<>4) do begin case Vote of 1:Cand1:=Cand1+1;
end; Writeln(‘Please vote for’); Writeln(‘1-Wilma’); writeln(‘2-Fred’); writeln(‘3-Barney’); writeln(‘4-quit’); readln(Vote); end; {Of the while} writeln(‘Wilma has ‘,Cand1); writeln(‘Fred has ‘,Cand2); writeln(‘Barney has ‘,Cand3); end. Program NoArrayElection; var Cand1, Cand2, Cand3:integer; Vote:integer; begin Cand1:=0;{These hold the number of} Cand2:=0;{ votes for the candidates} Cand3:=0; Writeln(‘Please vote for’); Writeln(‘1-Wilma’); writeln(‘2-Fred’); writeln(‘3-Barney’); writeln(‘4-quit’); readln(Vote); What if there are more candidates? Translating this to using arrays
8
We need to save the votes for the four candidates.
What type of an array would you use? Declare it TYPE VAR
9
Program Options Write a program that will roll a pair of six-sided dice 1000 times and count and display how often each roll occurs. Also show which roll occurs the most often and which occurs the least often. Push: Compare the results to what should happen statistically. Push: Display the results in a graph. Write the program for the election as in the warm ups and tell the winner of the election. Push: Let the use choose the names of the candidates Push: Store the names of the candidates in another array. Push: Let the user pick the names of the candidates, and how many candidates (up to 10) Write a program to keep track of scoring during a basketball game. Use the player number to find the address in the array (To keep it simple, just use player numbers 1..10). And the person can score 1, 2, or 3 points on each basket. Show the results (the points scored by each player at the end.) Push: Keep track of the names of the players also Push: Show the name of the player that scored the most points.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.