Header

divider
Links
• Home
• Contact Me
• 
Aeries Portal
• Royal High School
• Simi Valley USD

divider

AP Statistics

• Course Calendar
• Chapter Notes
• Practice Tests
• Statistics Links

divider
AP Comp-Sci
 Course Content
• Programs
• Practice Tests
• Comp-Sci Links

divider

CP Statistics
• Course Calendar
• Chapter Notes
• Practice Tests
• Statistics Links


divider

Algebra
• Table of Contents
Geometry
• Table of Contents

Math Power
• Table of Contents

AP Computer Science Unit 4 Programs
Data Structures: Arrays, Strings and ArrayLists

 

Working with 1D Arrays
Jump to:

(1)   Write a program that generates 10 random integers from 1 to 100 and stores them in a single array. Then display a list of the odd and even integers, with results shown as shown below.

Sample Output

Odds: 11  75  43  89

Evens: 26  32  68  52  84  40

(2)  Write a program that reads the test scores from the file scores.txt and then displays the scores that are A's (90 and up) on one line, B's on the next line (80-89), etc... all the way to F's.

Output should look like this:

A's: 97 91 92
B's: 89 85 88 82
C's: 77 71 77
D's: 64 68
F's: 55

 

(3)   Write a program to read a list of names out of the file namefile.txt into a String array. The number of names read is then displayed on the screen. The user is asked to enter a number within that range (1-n) so that they can view the name of the student in that position. Note: name 1 corresponds to position 0 in the array. If the number is in the appropriate range, the program will display the name in that position. If not, it will use a while loop to continue asking for a number that is in the correct range. See the example below. Resulting name should match the sample.

20 names read from file

Please enter a number from 1 to 20: 25
Incorrect input. Try again.
Please enter a number from 1 to 20: 0
Incorrect input. Try again.
Please enter a number from 1 to 20: 11

Name #11 is Munday

(4)   Write a program that tests the randomness of the computer’s random number generator. Generate 10,000 integers in the range 0 to 9 and display a list of the counts for each number at the end. Since there is a 1 in 10 chance for each number, there should be about 1000 of each when done.

         Sample Output

0’s: 1007
1’s: 1024
2’s: 989
etc...

(5) Roll a pair of dice and find their sum 250 times. Count the frequency of each sum in an array and then display a bar graph of the results, like that shown below:

 2: ********
 3: **************
 4: ********************
 5: *******************************
 6: **************************************************
 7: **********************************************************
 8: ********************************************
 9: **************************
10: ******************
11: ************
12: ******

(6)   A teacher wants to select up to 10 students from a class of size N (N>=20, as chosen by the user) to take part in a survey. In order to eliminate bias on the part of the teacher, a computer program will be used to randomly select the students. Each student will be assigned a unique integer from 1 to N and the program will generate the numbers of the students randomly. Each student may be chosen only once (i.e., no repeats) and a list of the students’ numbers will be printed to the screen. Begin the program by asking the teacher how many students are in the class. Then ask for the number of students to generate (must be from 1 to 10). Finally, display the random student numbers.

Example Run of the program:

How many students are in the class? 32
How many students to randomly select? 5

Students selected are:   13  25  7  8  30

 

Home  •  About Me  •  Aeries Portal  •  Contact Me
© DanShuster.com