How do I store user input values from a for loop into a matrix for summation.

I have to create a code that asks the user the ammount of an item, then create a for loop from the user inputted ammount, in the for loop ask the user the cost of the product, then add all the for loop instances together.
ammBooks = input('How many textbooks will you be purchesing today? ');
while ammBooks <= 0 || mod(ammBooks,1) ~= 0 || ammBooks > 10
fprintf('If you are purchasing books today please input a number larger than zero.\n')
ammBooks = input('How many textbooks will you be purchesing today? ');
while ammBooks > 10
fprintf('You may only purchase less than ten books per transaction.\n')
ammBooks = input('How many textbooks will you be purchesing today? ');
end
while mod(ammBooks,1) ~= 0
fprintf('You are unable to purchase a non whole number ammount of books.\n')
ammBooks = input('How many textbooks will you be purchesing today? ');
end
end
bookmatrix = zeros(1, ammBooks)
if ammBooks > 1
for diff = 1: ammBooks
bookCost = input('how much does your book cost? ')
for x = 1:size(bookmatrix,1) %loop over 100 rows
bookmatrix(x,:) = (1,ammBooks) %insert 10 random numbers
end
end
end
bookmatrix

1 Comment

The code I inserted is just what i have currently which is the last thing I attempted, this wasnt my original code that created the correct size matrix and filled the zeros however it counted up to the user definned variable instead of putting that variable into the matrix.

Sign in to comment.

 Accepted Answer

How about this?
ammBooks = input('How many textbooks will you be purchesing today? ');
while ammBooks <= 0 || mod(ammBooks,1) ~= 0 || ammBooks > 10
while ammBooks <= 0
fprintf('If you are purchasing books today please input a number larger than zero.\n')
ammBooks = input('How many textbooks will you be purchesing today? ');
end
while ammBooks > 10
fprintf('You may only purchase less than ten books per transaction.\n')
ammBooks = input('How many textbooks will you be purchesing today? ');
end
while mod(ammBooks,1) ~= 0
fprintf('You are unable to purchase a non whole number ammount of books.\n')
ammBooks = input('How many textbooks will you be purchesing today? ');
end
end
bookmatrix = zeros(1, ammBooks);
% if ammBooks > 1
for ii = 1: ammBooks
bookmatrix(ii) = input('how much does your book cost? ');
% for x = 1:size(bookmatrix,1) %loop over 100 rows
% bookmatrix(x,:) = (1,ammBooks) %insert 10 random numbers
% end
end
% end
total_cost = sum(bookmatrix);
fprintf('Total cost of books is %.2f.\n',total_cost)

3 Comments

Thank you for the quick response, that definitly helps.
ahh i see my mistake. I was atempting to put the variable into a matrix wihtout considering creating the input with the for loop. thanks again.
I'm glad that my answer makes sense. If you have any questions about it, let me know. Otherwise, please mark it as "Accepted". Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 24 Feb 2022

Commented:

on 25 Feb 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!