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

1 visualización (últimos 30 días)
Nebras Alsad
Nebras Alsad el 8 de Jun. de 2020
Comentada: Rena Berman el 12 de Oct. de 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 comentarios
Rik
Rik el 8 de Jun. de 2020
Editada: Rik el 8 de Jun. de 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 el 12 de Oct. de 2020
(Answers Dev) Restored edit

Iniciar sesión para comentar.

Respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by