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

2 ビュー (過去 30 日間)
Nebras Alsad
Nebras Alsad 2020 年 6 月 8 日
コメント済み: Rena Berman 2020 年 10 月 12 日
% 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);
Rena Berman
Rena Berman 2020 年 10 月 12 日
(Answers Dev) Restored edit

サインインしてコメントする。

回答 (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 件のコメント
Nebras Alsad
Nebras Alsad 2020 年 6 月 8 日
does the same thing with the name
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 Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by