## Exercise 7 - Arrays

Repository

Read in a list of students’ marks in a computer science class, and calculate the class mean (average), to the nearest hundredth (2 decimal places). Save as A7Q1

Write a program to read in a series of heights of people and output all those that are above average in height for the group. You will first have to ask how many people you are going to be reading in to create your array. Once you have this, you can begin reading in the heights. Save your program as A7Q2.

Write a program to create and populate an array, A, that contains only two integer elements. Moreover, the program will ensure the elements are placed in ascending order, once the array has been populated. For example suppose A is initialized as A[0] := 5 and A[1] := 3. The program will then interchange the values so that the resulting modified array is A[0] := 3 and A[1] := 5. The sorted values are then output to the screen. Save as A7Q3

Write a program that will read in a series of 10 marks. Once read in, the program will sort the array from lowest to highest mark. This sort must be done on your own (i.e. do not use the built in sort method provided by Java). Save your program as A7Q4. The easiest method to do this is called BubbleSorting. The pseudocode, something that is code like that describes how something will work, is the following:

The idea is we start at a spot and compare every element after that spot. If we have a number that is smaller, we swap the numbers until we hit the end of the array. Once we have done this, we are guaranteed to have the smallest element in that spot. We then move forward to the next spot in our array.

Modify question 4 from above to allow the user to decide how many marks to enter into the array. After the array has been sorted, also tell the user what mark is the median mark in that array. Remember, the median mark is the half way point in the array. If there are 2 points which make up the middle (i.e. in an array of even length), we take the average of those two numbers and call that the media. Name your program A7Q5.

Create a program that will ask a user for the size of a class. It will then read in all of the marks for the class into an array. Once this is done, the program should computer the lowest mark, the highest mark, and the average mark for the class that has been entered. These 3 things should be output to the user in a nice way. Name your program A7Q6.

Create a program that will determine all of the prime numbers between 2 and 1000. You will use a process called "The Sieve of Eratosthenes"

A Sieve of Eratosthenes works in the following manner:

Name your program A7Q7

**Question 1**Read in a list of students’ marks in a computer science class, and calculate the class mean (average), to the nearest hundredth (2 decimal places). Save as A7Q1

How many students in the class? 3

Enter the marks:

76

68

83

The class average is 75.67 %**Sample session:**How many students in the class? 3

Enter the marks:

76

68

83

The class average is 75.67 %

**Question 2**Write a program to read in a series of heights of people and output all those that are above average in height for the group. You will first have to ask how many people you are going to be reading in to create your array. Once you have this, you can begin reading in the heights. Save your program as A7Q2.

**Question 3**Write a program to create and populate an array, A, that contains only two integer elements. Moreover, the program will ensure the elements are placed in ascending order, once the array has been populated. For example suppose A is initialized as A[0] := 5 and A[1] := 3. The program will then interchange the values so that the resulting modified array is A[0] := 3 and A[1] := 5. The sorted values are then output to the screen. Save as A7Q3

**DO NOT**use the sort method inside of the array class.

Enter two integers: 13 9

The integers in ascending order are: 9 13

Enter two integers: -4 -2

The integers in ascending order are: -4 -2

Enter two integers: 17 18

The integers in ascending order are: 17 18**Sample session 1:**Enter two integers: 13 9

The integers in ascending order are: 9 13

**Sample session 2:**Enter two integers: -4 -2

The integers in ascending order are: -4 -2

**Sample session 3:**Enter two integers: 17 18

The integers in ascending order are: 17 18

**Question 4:**Write a program that will read in a series of 10 marks. Once read in, the program will sort the array from lowest to highest mark. This sort must be done on your own (i.e. do not use the built in sort method provided by Java). Save your program as A7Q4. The easiest method to do this is called BubbleSorting. The pseudocode, something that is code like that describes how something will work, is the following:

*repeat from start to end-1 as x:**repeat from x+1 to end as y:**if spot[x] > spot[y]:**swap spot[x] with spot[y]*The idea is we start at a spot and compare every element after that spot. If we have a number that is smaller, we swap the numbers until we hit the end of the array. Once we have done this, we are guaranteed to have the smallest element in that spot. We then move forward to the next spot in our array.

**Question 5:**Modify question 4 from above to allow the user to decide how many marks to enter into the array. After the array has been sorted, also tell the user what mark is the median mark in that array. Remember, the median mark is the half way point in the array. If there are 2 points which make up the middle (i.e. in an array of even length), we take the average of those two numbers and call that the media. Name your program A7Q5.

**STOP! STOP! STOP!**

You only have to do questions 1 to 5! If you want to try more, go ahead but Mr. Lamont will only be marking questions 1 to 5 :)You only have to do questions 1 to 5! If you want to try more, go ahead but Mr. Lamont will only be marking questions 1 to 5 :)

**Question 6:**Create a program that will ask a user for the size of a class. It will then read in all of the marks for the class into an array. Once this is done, the program should computer the lowest mark, the highest mark, and the average mark for the class that has been entered. These 3 things should be output to the user in a nice way. Name your program A7Q6.

**Question 7:**Create a program that will determine all of the prime numbers between 2 and 1000. You will use a process called "The Sieve of Eratosthenes"

A Sieve of Eratosthenes works in the following manner:

- Create a list of consecutive integers from 2 to n: (2, 3, 4, ..., n). This is the sieve.
- Initially, let p equal 2, the first prime number.
- Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list. These will be multiples of p: 2p, 3p, 4p, etc.; note that some of them may have already been marked.
- Find the first number greater than p in the list that is not marked. If there was no such number,
**stop**. Otherwise, let p now equal this number (which is the next prime), and repeat from step 3.

Name your program A7Q7