edit add2sortedlist.m
a = add2sortedlist([1, 3, 8, 10], -55)
At index 1 is 1 before
At index 2 is 3 before
At index 3 is 8 before
At index 4 is 10 before
{Undefined function or variable "n".
Error in add2sortedlist (line 30)
ar = [inputarray(1:n), inputnum,
inputarray(n+1:length(inputarray))];
}
a = add2sortedlist([1, 3, 8, 10], -55)
At index 1 is 1 before
At index 2 is 3 before
At index 3 is 8 before
At index 4 is 10 before
a =
-55 1 3 8 10
inputarray = [ 5:9]
inputarray =
5 6 7 8 9
n = 0
n =
0
inputarray(1:n)
ans =
Empty matrix: 1-by-0
n = 1;
inputarray(1:n)
ans =
5
a = add2sortedlist([1, 3, 8, 10], -55)
At index 1 is 1 before
At index 2 is 3 before
At index 3 is 8 before
At index 4 is 10 before
a =
1 -55 3 8 10
a = add2sortedlist([1, 3, 8, 10], -55)
At index 1 is 1 before
At index 2 is 3 before
At index 3 is 8 before
At index 4 is 10 before
a =
-55 1 3 8 10
whos
Name Size Bytes Class Attributes
a 1x5 40 double
ans 1x1 8 double
inputarray 1x5 40 double
n 1x1 8 double
inputarray
inputarray =
5 6 7 8 9
bob = [1, 3, 8, 10]
bob =
1 3 8 10
a = add2sortedlist(bob, -55)
At index 1 is 1 before
At index 2 is 3 before
At index 3 is 8 before
At index 4 is 10 before
a =
-55 1 3 8 10
for i=1:5
for i = -47:20:-15
disp(i);
end
end
-47
-27
-47
-27
-47
-27
-47
-27
-47
-27
for k=1:5
for i = -47:20:-15
disp(i);
end
end
-47
-27
-47
-27
-47
-27
-47
-27
-47
-27
a = add2sortedlist(bob, -55)
At index 1 is 1 before
At index 2 is 3 before
At index 3 is 8 before
At index 4 is 10 before
a =
-55 1 3 8 10
a = add2sortedlist(bob, 55)
At index 1 is 1 after
At index 2 is 3 after
At index 3 is 8 after
At index 4 is 10 after
a =
1 3 8 10 55
a = add2sortedlist(bob, 5)
At index 1 is 1 after
At index 2 is 3 after
At index 3 is 8 before
At index 4 is 10 before
a =
5 1 3 8 10
a = add2sortedlist(bob, 5)
At index 1 is 1 after
At index 2 is 3 after
At index 3 is 8 before
At index 4 is 10 before
a =
1 3 5 8 10
a = add2sortedlist(bob, 55)
At index 1 is 1 after
At index 2 is 3 after
At index 3 is 8 after
At index 4 is 10 after
a =
1 3 8 10 55
a = add2sortedlist(bob, -55)
At index 1 is 1 before
At index 2 is 3 before
At index 3 is 8 before
At index 4 is 10 before
a =
-55 1 3 8 10
a = add2sortedlist(bob, 2)
At index 1 is 1 after
At index 2 is 3 before
At index 3 is 8 before
At index 4 is 10 before
a =
1 2 3 8 10
a = add2sortedlist(bob, 9)
At index 1 is 1 after
At index 2 is 3 after
At index 3 is 8 after
At index 4 is 10 before
a =
1 3 8 9 10
type add2sortedlist.m
%
% add2sortedlist.m
%
% function to take a given array, and add a given number to it,
% in sorted order.
%
% a = add2sortedlist([1, 7, 10], 5);
% a would then become [1, 5, 7, 10]
%
%assuming array is sorted
function ar= add2sortedlist(inputarray, inputnum)
% Initialize our variables
ar = []; % return array
counter = 1; % array position
n = 0; % position that inputnum should go
% Examine each number compared to inputnum
while(counter<= length(inputarray))
% update n as needed
if (inputarray(counter) < inputnum)
% update n
location = 'after';
n = counter;
else
location = 'before';
end
% consider commenting out the 2 lines below.
s = sprintf('At index %d is %d %s', counter,inputarray(counter), ...
location);
disp(s)
counter = counter + 1;
end
% disp(n);
% Create a new array with inputnum in the appropriate spot.
ar = [inputarray(1:n), inputnum, inputarray(n+1:length(inputarray))];
a = add2sortedlist(bob, 9)
At index 1 is 1 after
At index 2 is 3 after
At index 3 is 8 after
At index 4 is 10 before
a =
1 3 8 9 10
clear
clc
edit exampletry.m
type exampletry.m
%
% Script to give an example of the try command
%
try
% code that may fail
disp('test out a non-existent function.');
a = ewkjlfglkqjwfjklqw(4);
disp('back from the function that doesn''t exist.');
catch
% what to do if the code failed
disp('You do not have that function, so here is 4 + 1 instead');
a = 4 + 1;
end
exampletry
test out a non-existent function.
You do not have that function, so here is 4 + 1 instead
a
a =
5
a = ewkjlfglkqjwfjklqw(4);
{Undefined function 'ewkjlfglkqjwfjklqw' for input arguments of
type 'double'.
}
type exampletry.m
%
% Script to give an example of the try command
%
try
% code that may fail
disp('test out a non-existent function.');
%a = ewkjlfglkqjwfjklqw(4);
a = sin(4);
disp('back from the function that doesn''t exist.');
catch
% what to do if the code failed
disp('You do not have that function, so here is 4 + 1 instead');
a = 4 + 1;
end
exampletry
test out a non-existent function.
back from the function that doesn't exist.
type exampletry.m
%
% Script to give an example of the try command
%
try
% code that may fail
disp('test out a non-existent function.');
%a = ewkjlfglkqjwfjklqw(4);
a = sin(4);
disp('back from the function that doesn''t exist.');
catch
% what to do if the code failed
disp('You do not have that function, so here is 4 + 1 instead');
a = 4 + 1;
a = ewkjlfglkqjwfjklqw(4);
end
exampletry
test out a non-existent function.
back from the function that doesn't exist.
type exampletry.m
%
% Script to give an example of the try command
%
try
% code that may fail
disp('test out a non-existent function.');
a = ewkjlfglkqjwfjklqw(4);
%a = sin(4);
disp('back from the function that doesn''t exist.');
catch
% what to do if the code failed
disp('You do not have that function, so here is 4 + 1 instead');
a = 4 + 1;
a = ewkjlfglkqjwfjklqw(4);
end
exampletry
test out a non-existent function.
You do not have that function, so here is 4 + 1 instead
{Undefined function 'ewkjlfglkqjwfjklqw' for input arguments of
type 'double'.
Error in exampletry (line 15)
a = ewkjlfglkqjwfjklqw(4);
}
a
a =
5
type exampletry.m
%
% Script to give an example of the try command
%
try
% code that may fail
disp('test out a non-existent function.');
a = ewkjlfglkqjwfjklqw(4);
%a = sin(4);
disp('back from the function that doesn''t exist.');
catch
% what to do if the code failed
disp('You do not have that function, so here is 4 + 1 instead');
a = 4 + 1;
a = ewkjlfglkqjwfjklqw(4);
disp('continuing');
end
exampletry
test out a non-existent function.
You do not have that function, so here is 4 + 1 instead
{Undefined function 'ewkjlfglkqjwfjklqw' for input arguments of
type 'double'.
Error in exampletry (line 15)
a = ewkjlfglkqjwfjklqw(4);
}
type exampletry.m
%
% Script to give an example of the try command
%
try
% code that may fail
disp('test out a non-existent function.');
a = ewkjlfglkqjwfjklqw(4);
%a = sin(4);
disp('back from the function that doesn''t exist.');
catch
% what to do if the code failed
disp('You do not have that function, so here is 4 + 1 instead');
a = 4 + 1;
% a = ewkjlfglkqjwfjklqw(4);
error('quitting since there is a problem');
end
exampletry
test out a non-existent function.
You do not have that function, so here is 4 + 1 instead
{Error using exampletry (line 16)
quitting since there is a problem
}
clear
clc
diary off