can you help me to find out whats wrong here??

1 次查看(过去 30 天)
% Question 6)
name = "nebras";
id = 1702227;
% calculating avregae of your id digit
S = 0;
for i = 1:length(id)
S = (S + str2num(id(i)));
end
avg = S / length(id);
% Displaying result
fprintf("Name is %s\n" , name);fprintf("Average of digits of id is %f \n" , avg);% Question 6)
name = "nebras";
id = 1702227;
% calculating avregae of your id digit
S = 0;
for i = 1:length(id)
S = (S + str2num(id(i)));
end
avg = S / length(id);
% Displaying result
fprintf("Name is %s\n" , name);fprintf("Average of digits of id is %f \n" , avg);
  2 个评论
Rik
Rik 2020-6-8
编辑:Rik 2020-6-8
Question content deleted by Nebras Alsad:
% Question 6)
name = "nebras";
id = 1702227;
% calculating avregae of your id digit
S = 0;
for i = 1:length(id)
S = (S + str2num(id(i)));
end
avg = S / length(id);
% Displaying result
fprintf("Name is %s\n" , name);fprintf("Average of digits of id is %f \n" , avg);% Question 6)
name = "nebras";
id = 1702227;
% calculating avregae of your id digit
S = 0;
for i = 1:length(id)
S = (S + str2num(id(i)));
end
avg = S / length(id);
% Displaying result
fprintf("Name is %s\n" , name);fprintf("Average of digits of id is %f \n" , avg);

请先登录,再进行评论。

回答(1 个)

Rik
Rik 2020-6-8
If you want to do this, you will probably have to convert to a char array and then convert to a double array.
%because this is your homework I will not give a complete solution, only hints
num=1234;
num_as_txt=sprintf(___,num);
num_as_txt(1) %see what this returns
double(num_as_txt(1)) %does this return what you expect? how should you change the entire vector?
As a last note: you don't need to use a loop to calculate the average, you can simply use the mean function (and you should not use str2num, use str2double instead when you need it).
  8 个评论
Rik
Rik 2020-6-8
Yes, because you made it a scalar string by using the double quotes, instead of a char array by using single quotes.
And why did you delete your question content? Are you afraid you teacher will catch you cheating? Then you should have considered that before posting your question on a public forum. This is extremely rude.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by