How do I store user input values from a for loop into a matrix for summation.
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!