Simonefleek / Zybooks

Random Number
0 stars 0 forks source link

7.48 Copy of Lowest Score Drop. Learning Arrays #19

Open Simonefleek opened 5 months ago

Simonefleek commented 5 months ago

Write a program that reads a list of integer scores.The input begins with an integer indicating the number of scores that follow. Store these scores (not including the first integer) in an int array of size equal to the first integer.

This program calculates the average (no decimals) of test scores, where the lowest score in the group is dropped. The following methods are to be written: Step_1 (1 point): static char getLetterGrade(score):takes a test score as argument and returns the letter grade (as char not String) based on the following: Score Letter Grade Above 90 A 80-89 B 70-79 C 60-69 D Below 60 F

Example: Input: 5 89 75 99 79 85 Output: 89 = B 75 = C 99 = A 79 = C 85 = B

Step_2 (1 point): static int findLowest(int[ ] arr): returns the lowest score Example: Input: 5 89 75 99 75 85 Output: 89 = B 75 = C 99 = A 75 = C 85 = B Lowest is 75

Step_3 (1 point): static int sumOfHighest(int[ ] arr): returns the sum of the highest scores. It calls method findLowest(int[ ] arr) to drop the lowest. Example: Input: 89 75 99 79 85 Output: 89 = B 75 = C 99 = A 79 = C 85 = B Lowest is 75 Sum of highest is 352

Step_4 (1 points): static int calculateAverage(int [ ] arr): Calculate and returns the average (as int without decimal) of the highest scores after the lowest is dropped.

Example: Input: 5 89 75 99 79 85 Output: 89 = B 75 = C 99 = A 79 = C 85 = B Lowest is 75 Sum of highest is 352 Average of highest is 88 B

Input: 5 96 65 58 61 60 Output: 96 = A 65 = D 58 = F 61 = D 60 = D Lowest is 58 Sum of highest is 282 Average of highest is 70 C

Simonefleek commented 5 months ago

import java.util.Scanner;

public class Main { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int arraySize; arraySize = scnr.nextInt(); int scores[] = new int[arraySize]; // Read the scores and print them with their letter grade

  System.out.println("Lowest is " + findLowest(scores));
  System.out.println("Sum of highest: " + sumOfHighest(scores));
  int average = calculateAverage(scores) ;
  System.out.println("Average of highest is " + average + " " + getLetterGrade(average));

} public static int findLowest(int[] arr) { / Type your code here. / } public static int sumOfHighest(int[] arr) { / Type your code here. /
}

public static int calculateAverage(int[] arr) { / Type your code here. / }

public static char getLetterGrade(int score) { / Type your code here. / }

}

Simonefleek commented 5 months ago

A typical variable stores one data item, like the number 59 or the character 'a'. Instead, sometimes a list of data items should be stored. Ex: A program recording points scored in each quarter of a basketball game needs a list of 4 numbers. Requiring a programmer to declare 4 variables is annoying; 200 variables would be ridiculous. An array is a special variable having one name, but storing a list of data items, with each item being directly accessible. Some languages use a construct similar to an array called a vector. Each item in an array is known as an elementintsPerQuarter 0 [22] 1[19] How many points in 4th quarter? pointsPerQuarter[3] is 28 2[12] 3[28]

How many points in 4th quarter? pointsPerQuarter[3] is 28

Static Figure: Variable numPlayers stores the number 12 and the variable pointsPerQuarter is an array that stores 4 items.

Step 1: A variable usually stores just one data item.

Step 2: Some variables should store a list of data items, like variable pointsPerQuarter that stores 4 items. Elements 22, 19, 12, and 28 are stored in pointsPerQuarter. Element 22 is stored at location number 0, element 19 is stored at location number 1, element 12 is stored at location number 2, and element 28 is stored at location number 3.

Step 3: Each element is accessible, like the element numbered 3. The answer to the question "How many points in 4th quarter?" is 28 because element 28 is the fourth item in pointsPerQuarter and is located at location number 3.

Simonefleek commented 5 months ago

you might think of a normal variable as a truck, and an array variable as a train. A truck has just one car for carrying "data", but a train has many cars, each of which can carry data.

Figure 7.1.1: A normal variable is like a truck, whereas an array variable is like a train n an array, each element's location number is called the index, myArray[2] has index 2. An array's key feature is that the index enables direct access to any element, as in myArray[2]; different languages may use different syntax, like myArray(3) or myVector.at(3). In many languages, indices start with 0 rather than 1, so an array with 4 elements has indices 0, 1, 2, and 3.

Array peoplePerDay has 365 elements, one for each day of the year. Valid accesses are peoplePerDay[0], [1], ..., [364].

1) Which assigns element 0 with the value 250?

Correct

Assigns element 0 with 250.

Which assigns element 1 with the value 99?

Correct

Assigns element 1 with 99

What is the value of peoplePerDay[8]? peoplePerDay[9] = 5; peoplePerDay[8] = peoplePerDay[9] - 3;

Correct

peoplePerDay[9] is 5. So peoplePerDay[9] - 3 is 5 - 3, which is 2.

Assume N is initially 1. What is the value of peoplePerDay[2]? peoplePerDay[N] = 15; N = N + 1; peoplePerDay[N] = peoplePerDay[N - 1] * 3;

Correct

peoplePerDay[1] is assigned with 15. Then, peoplePerDay[2] is assigned with peoplePerDay[1] 3, which is 15 3, so 45.

Simonefleek commented 5 months ago

Assign the second element in scoresList with 77.

Check

Show answer Correct

scoresList[1] = 77; When indices start at 0, the second element is at index 1.

3) Assign the last element with 77.

Check

Show answer Correct

scoresList[9] = 77; When indices start at 0, an N-element array's last element has index N - 1. With 10 elements, the last index is 9.

4) If that array instead has 100 elements, what is the last element's index?

Check

Show answer Correct

99 The last index is at N - 1, where N is the number of elements, because the first element has index 0.

5) If the array's last index was 499, how many elements does the array have?

Check

Show answer Correct

500 Because numbering starts at 0, the last index is N - 1, where N is the number of elements.

Simonefleek commented 5 months ago

A programmer commonly needs to maintain a list of items, just as people often maintain lists of items like a grocery list or a course roster. An array is an ordered list of items of a given data type. Each item in an array is called an element.

Construct 7.2.1: Array reference variable declaration and array allocation. dataType[] arrayName = new dataType[numElements];

Feedback? The array declaration uses [ ] symbols after the data type to indicate that the variable is an array reference. An array reference variable can refer to arrays of various sizes. The new keyword creates space in memory to store the array with the specific number of elements. The array reference variable is assigned to refer to that newly allocated array. Ex: int[] gameScores = new int[4]; declares an array reference variable gameScores, allocates an array of four integers, and assigns gameScores to refer to the allocated array.

Terminology note: [ ] are brackets. { } are braces. In an array access, the number in brackets is called the index of the corresponding element. The first array element is at index 0.

int[] yearsArr = new int[4];

yearsArr[0] = 1999; yearsArr[1] = 2012; yearsArr[2] = 2025; 1) How many elements in memory does the array declaration create?

What value is stored in yearsArr[1]?

2012

what is the value of currYear = yeasrArr[2]

2025

with what value does currYear = yearsArr[3] assign currYear?

0

their is no value for yearsArr[3]

however their are 3

recall that the array declaration was int [] yearArr = new int [4]; a valid assignment?

no this is not valid assignment because int 4 doesn't exist .

what is a proper way to access the first element in array int[] ?

Always staring from 0 Comment - Answer = yearsArr[0]

What are the contents of the array if the above code is followed by the statement: yearsArr[0] = yearsArr[2]?

2025,2012,2025,0

What is the index of the last element of the following array int[] priceArr = new int [100];

Answer = 99

Simonefleek commented 5 months ago

A powerful aspect of arrays is that the index is an expression. Ex: userNums[i] uses the value held in the int variable i as the index. As such, an array is useful to easily lookup the Nth item in a list.

An array's index must be an integer type. The array index cannot be a floating-point type, even if the value is 0.0, 1.0, etc.

The program below allows a user to print the age of the Nth oldest known person to have ever lived. The program quickly accesses the Nth oldest person's age using oldestPeople[nthPerson - 1]. Note that the index is nthPerson - 1 rather than just nthPerson because an array's indices start at 0, so the 1st age is at index 0, the 2nd at index 1, etc.

Figure 7.2.1: Array's nth element can be directly accessed using [n-1]: Oldest people program.

import java.util.Scanner; public class OldestPeople { public station void main(String[] args) { Scanner scar = new Scanner(System.in); int[] oldestPeeople = new int [5]; int nthperson; oldestPeople[0] = 122; oldestPeople[1[ = 119; oldestPeople[2] = 117; oldestPeople[3] = 117; oldestPeople[4] = 116;

         System.out.println("Enter N (1-5): ");
         nthPerson = scnr.nextInt();

       if((nthPerson >= 1) && (nthPerson <= 5)) {
          System.out.print("The " + nthPerson + "the oldest person lived ");
 System.out.println(oldestPeople[nthPerson - 1] + "years.");