Index exceeds the number of array of elements error

1 view (last 30 days)
Patrick Hunter on 23 Oct 2020
Answered: KSSV on 23 Oct 2020
When I try to enter values the error " Index the number of array elements" appears. I do not know what is wrong please help
clear;
clc;
max = input('Enter limit for vector: ');
askingPrice = input('Enter the asking price: ');
n = 1;
count1 = 0; %starts count for how many are in each category
count2 = 0;
count3 = 0;
count4 = 0;
count5 = 0;
totalSum = 0;
while (n <= max)
x(2,n) = input('Enter diameters:');
n = n + 1;
end
if (x(1,n) < 8)
count1 = count1 + 1;
totalSum = totalSum + 150;
elseif (x(n) >= 8 && x(n) <= 10)
count2 = count2 + 1;
totalSum = totalSum + 200;
elseif (x(n)> 10 && x(n) <= 12)
count3 = count3 + 1;
totalSum = totalSum + 500;
elseif (x(n) > 12 && x(n) >= 15)
count4 = count4 + 1;
totalSum = totalSum + 700;
elseif (x(n) > 15)
count5 = count5 + 1;
totalSum = totalSum + 1000;
end
profit = totalSum - askingPrice;
disp(['Profit is #',profit]);
disp(['Jumbo amount: ', count5]);
disp(['Extra Large amount: ', count4]);
disp(['Large amount: ', count3]);
disp(['Medium amount: ', count2]);
disp(['Small amount: ', count1]);

KSSV on 23 Oct 2020
clear;
clc;
max = input('Enter limit for vector: ');
askingPrice = input('Enter the asking price: ');
n = 1;
count1 = 0; %starts count for how many are in each category
count2 = 0;
count3 = 0;
count4 = 0;
count5 = 0;
totalSum = 0;
n = max ;
x = zeros(1,n) ;
for i = 1:max
x(i) = input('Enter diameters:');
end
if (x(1,n) < 8)
count1 = count1 + 1;
totalSum = totalSum + 150;
elseif (x(n) >= 8 && x(n) <= 10)
count2 = count2 + 1;
totalSum = totalSum + 200;
elseif (x(n)> 10 && x(n) <= 12)
count3 = count3 + 1;
totalSum = totalSum + 500;
elseif (x(n) > 12 && x(n) >= 15)
count4 = count4 + 1;
totalSum = totalSum + 700;
elseif (x(n) > 15)
count5 = count5 + 1;
totalSum = totalSum + 1000;
end
profit = totalSum - askingPrice;
fprintf('Profit is %f\n',profit);
fprintf('Jumbo amount: %f\n', count5);
fprintf('Extra Large amount: %f\n', count4);
fprintf('Large amount: %f\n', count3);
fprintf('Medium amount: %f\n', count2);
fprintf('Small amount: %f\n', count1);