Suggestions for Input type from user

3 次查看(过去 30 天)
I am having issues with the input from the user in a pre-defined format and converting them into required digits, that are to be checked for divisibility of 11.
Cells are used to fetch the data from user. But, the code is failing when the number of test cases are more than 1.
Error - Index exceeds the number of array elements
Could anyone advice me whats the appropriate way to store the input from user.
Elevenagram from Google Kickstart, Round H, 2019 -
It is a well known fact that a number is divisible by 11 if and only if the alternating sum of its digits is equal to 0 modulo 11. For example, 8174958 is a multiple of 11, since 8 - 1 + 7 - 4 + 9 - 5 + 8 = 22.
Given a number that consists of digits from 1-9, can you rearrange the digits to create a number that is divisible by 11?
Since the number might be quite large, you are given integers A1, A2, ..., A9. There are Ai digits i in the number, for all i.
Input
The first line of the input gives the number of test cases, T. T lines follow. Each line contains the nine integers A1, A2, ..., A9.
Input Example -
2 %No. of Test Cases
0 0 2 0 0 1 0 0 0 % the digits are 336
0 0 0 0 0 0 0 0 12 % the digits are 999999999999 = 9 - 12's
Code ( To Convert the user input(s) into actual number(s) only)
n = input ('Enter Test cases : ');
for i=1:n
iter{i} = input('Enter Values :\n','s');
end
for i=1:n
iter{i} = str2num(iter{i});
end
digit = {[1:9]};
% 0 3 0 0 0 0 0 0 0; 1 2 3 4 5 6 7 8 9;
% gives 222 - actual number to be checked later for divisiblity of 11
len_iter=length(iter);
len_iter_digit=length(iter{1});
len_digit = length(digit{1});
ele = zeros(1,len_digit);
for j =1: len_iter
for i = 1:len_iter_digit
ele(i) = sum( 10.^((1:iter{j}(i))-1));
ele(i) = ele(i)*digit{j}(i);
end
end
  2 个评论
Rik
Rik 2020-9-10
What is your code trying to do? Are you trying to convert the input to the actual number?
Akshay Patil
Akshay Patil 2020-9-10
Yes, in this code - I am trying to convert the input into actual number, but facing the issues when I have more than 1 test cases.

请先登录,再进行评论。

采纳的回答

Rik
Rik 2020-9-10
Your first problem can be avoided by using digit{1}(i) instead, since the numbers 1-9 don't change between values.
Then you will realize that ele will only store the last iteration. Can you see why?
After that you will notice that you have a vector, not a number. I would suggest creating the number first as a character array.
iter={[0 3 0 0 0 0 0 0 0],[1 2 3 4 5 6 7 8 9]};
ele=cell(size(iter));
digit_chars='123456789';digit_chars=num2cell(digit_chars);
for n=1:numel(iter)
tmp=digit_chars;
for digit=1:9
%now use repmat
end
%now convert the tmp variable to a char array
%(e.g. with a direct function, or by using {:} to create a comma separated list)
%finally covert the char array to a double
end
  3 个评论
Rik
Rik 2020-9-11
Why should I give you advice again if you ignored my previous advice? Anyway, I'll try again.
My suggestion was to go from this
[0 3 0 1 0 0 0 0 0 0]
to this
{'','222','','4','','','','',''}
to this
'2224'
to this
2224
None of that requires str2num (read the documentation: it recommends using str2double instead). The first step is the easiest with repmat (since we created a cell with the contents {'1','2','3','4','5','6','7','8','9'}).
Another word of advice: if you implement something, remove all non-applicable comments and don't leave any commented code (unless you explain why it is still there). Currently the comments I wrote will only confuse readers, since the code does something completely different.
Akshay Patil
Akshay Patil 2020-9-11
Thank you Rik for explaining again, and will try to implement as you suggested.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by