- Edited
Matlab/Octaviz:
function pascal_index = pascal_triangle(row)
switch nargin
case 0
error('This function acquires one input argument of type natural scalar.');
case 1
if (isscalar(row)&&isnumeric(row)&&(row>=0))
if ~isequal(int8(row),row)
fprintf('Intput is not a natural number.\n');
fprintf('It is converted to: %d \n\n',int8(row));
end
disp(horzcat('The entires on Pascal triangle until the',' ',...
num2str(int8(row)),'th row is:'));
else
error('This function acquires an input of type natural scalar.');
end
otherwise
error('Too many input arguments.');
end
pascal_index = {}; % Reserves the values of the entries on Pascal's triangle
for i = 1:row+1
pascal_index{i} = [];
for j = 1:i
pascal_index{i}(j) = factorial(i-1)/(factorial(j-1)*factorial(i-j));
end
disp(pascal_index{i});
end
fprintf('\n');
Examples:>> pascal_mag = pascal_triangle(10);
The entires on Pascal triangle until the 10th row is:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
>> pascal_mag2 = pascal_triangle();
??? Error using ==> pascal_triangle at 5
This function acquires one input argument of type natural scalar.
>> pascal_mag3 = pascal_triangle(11,14,15);
??? Error using ==> pascal_triangle
Too many input arguments.
>> pascal_mag4 = pascal_triangle(11.2);
Intput is not a natural number.
It is converted to: 11
The entires on Pascal triangle until the 11th row is:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
>> pascal_mag5 = pascal_triangle('a');
??? Error using ==> pascal_triangle at 15
This function acquires an input of type natural scalar.