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

1 Ansicht (letzte 30 Tage)
Nebras Alsad
Nebras Alsad am 8 Jun. 2020
Kommentiert: Rena Berman am 12 Okt. 2020
% 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 Kommentare
Rik
Rik am 8 Jun. 2020
Bearbeitet: Rik am 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);
Rena Berman
Rena Berman am 12 Okt. 2020
(Answers Dev) Restored edit

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Rik
Rik am 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 Kommentare
Nebras Alsad
Nebras Alsad am 8 Jun. 2020
does the same thing with the name
Rik
Rik am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by