Uploaded by clegaspi2023

AP COMP SCI P CODE

advertisement
3. WRITTEN RESPONSES
3 a.
3.a.i.
I made this program to serve as a way for teachers or any person(s) that require a mean, median, and mode from a data
set of numbers. Rather than doing doing calculations, searching for a calculator on google, or trying to figure it out
manually, this code does it almost instantaneously.
3.a.ii.
The code requests 7 different numbers, and the code finds the mean, median, and mode from whatever 7 numbers the
user inputs
3.a.iii.
The input of the program are 7 numbers. They can be the same in numerical value. After 7 numbers are inputted, the
output of the code calculates and neatly displays the mean, median, and mode for those 7 numbers.
3 b.
3.b.i.
3.b.ii.
3.b.iii.
The name of the list being used is 'num_list'
3.b.iv.
in the program, the data contained in the 'num_list' represent a list of numbers entered by the user. The program uses
this list to calculate various statistics such as the mean, median, and mode of the numbers entered.
3.b.v.
The selected list manages complexity in the program code by storing and organizing user-input data. Without the list, the
program would require separate variables for each input and would be less flexible in handling varying amounts of data.
3 c.
3.c.i.
3.c.ii.
3.c.iii.
The identified procedure, 'bubble_sort" implements the bubble sort algorithm to sort a list of numbers. This procedure
takes a list of numbers as input and returns the sorted list of numbers. The bubble sort algorithm works by repeatedly
swapping adjacent elements that are in the wrong order until the entire list is sorted. This sorting algorithm is a
fundamental algorithm in computer science and is often used as an introductory example of an algorithm in programming
courses. In the context of the program, the 'bubble_sort' procedure contributes to the overall functionality of the program
by providing a way to sort the list of numbers entered by the user. By calling this procedure, we can easily sort the list of
numbers without having to implement the sorting algorithm in our main program code every time.
3.c.iv.
The bubble sort algorithm implemented in the bubble_sort procedure works as follows:
1. Get the length of the input list.
2. Loop through the list for each element, starting from the first element.
3. For each element, loop through the remaining unsorted elements to its right.
4. Compare each pair of adjacent elements and swap them if they are in the wrong order (i.e., the left element is
greater than the right element).
5. Repeat steps 2-4 until the entire list is sorted.
6. Return the sorted list.
This algorithm sorts the input list by repeatedly swapping adjacent elements that are in the wrong order, hence the name
"bubble" sort. The algorithm works by gradually moving the larger elements towards the end of the list while the smaller
elements "bubble" towards the front. The worst-case time complexity of this algorithm is O(n^2), where n is the number of
elements in the list.
3 d.
3.d.i.
First call:
In this call, the bubble_sort procedure is passed a list of five unsorted numbers. The algorithm will execute the swapping
of adjacent elements multiple times until the entire list is sorted. The first iteration of the algorithm will compare the first
and second elements of the list, which are 9 and 5, respectively. Since 9 is greater than 5, the algorithm will swap these
two elements. The list will now be [5, 9, 2, 7, 1]. The second iteration of the algorithm will compare the second and third
elements, which are 9 and 2, respectively. Since 9 is greater than 2, the algorithm will swap these two elements. The list
will now be [5, 2, 9, 7, 1]. The algorithm will continue swapping adjacent elements until the entire list is sorted. The final
sorted list will be [1, 2, 5, 7, 9]
Second call:
unsorted_list = [1, 2, 3, 4, 5]
sorted_list = bubble_sort(unsorted_list)
In this call, the bubble_sort procedure is passed a list of five already sorted numbers. Since the list is already sorted, the
algorithm will only execute the outer loop once and detect that no swaps were made during the inner loop, indicating that
the list is already sorted. The final sorted list will be [1, 2, 3, 4, 5].
3 d.ii.
Condition(s) tested by first call:
calculate_mean(num_list): The condition being tested is the ability of the calculate_mean function to take a list of
numbers and calculate the mean of those numbers by adding them up and dividing by the total number of numbers in the
list.
Condition(s) tested by second call:
N/A
3.d.iii.
Results of the first call:
The result of the first call to the calculate_mean function is the mean (average) of the list of numbers provided as the
argument to the function. This value is returned by the function and is later printed to the console by the print statement:
bash
Copy code
print("Mean:", calculate_mean(num_list))
The result of this call is the mean of the numbers entered by the user.
Results of the second call:
The second call prints the list.
Download