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

1 view (last 30 days)
% 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 Comments
Rik
Rik on 8 Jun 2020
Edited: Rik on 8 Jun 2020
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);

Sign in to comment.

Answers (1)

Rik
Rik on 8 Jun 2020
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 Comments
Rik
Rik on 8 Jun 2020
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.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!