-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBillieBob_Num_Sort.m
59 lines (59 loc) · 1.96 KB
/
BillieBob_Num_Sort.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
%% Math 111-01: MATLAB (Summer 2019)
% Trisha Menon (trisha0menon@gmail.com)
%
%
% Lecture: Chapter 6.5 - Subfunctions and Nested Function
% Example #2: BillieBob_Num_Sort Program
%
%
% The purpose of this program is to perform the following steps:
% 1) Receive an arbitrary number of numeric input values from the Command
% Window;
% 2) Sort the numeric data in ascending order using a separate sorting
% function; and
% 3) Return the sorted numeric data to the Command Window.
%
% The inputs to this program will be the numeric values keyed in via the
% Command Window by the user. The outputs from this program will be the
% sorted data values returned via the Command Window to the user.
%
% The program is divided into 3 major steps:
% 1) Read the input data into an array.
% 2) Sort the data in ascending order.
% 3) Write the sorted data.
%
% Variables Definitions:
% user_array - Input numeric data array (from end user via Command
% Window)
% ii - Index variable during sort iteration
% nvals - Number of input values from user numeric array
% sorted_array - Sorted numeric data array
%% --- START OF MATH PROGRAM ---
%
clear, clc
format compact
format bank
%
% Prompt User for Size of Numeric Array to be Sorted, and Allocate...
nvals = input('Enter the total number of values to be sorted: ');
user_array = zeros(1,nvals); %Pre-allocate the numeric array
%
% Prompt User for Numeric Values to be Sorted, and Assign to Array...
for ii = 1:nvals
promptstring = ['Enter value No. ', int2str(ii), ': '];
user_array(ii) = input(promptstring);
end
%
% *** Function Call to Sort Numeric Data ***
%
sorted_array = BB_Sorter(user_array);
%
% *** Return to Main Program ***
%
% Display the Sorted Results
fprintf('\nThe Sorted Results...\n'); % Leader output announcing results
%
for ii = 1:nvals
fprintf(' %8.4f\n', sorted_array(ii)); % Display each numeric
end
% --- END OF PROGRAM ---