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
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!