Given two input vectors: name - user last names age - corresponding age of the person.Return the name of the oldest person in the output var old_name.Why am I getting error?

조회 수: 11 (최근 30일)
Given two input vectors:
  • name - user last names
  • age - corresponding age of the person
Return the name of the oldest person in the output variable old_name.
  댓글 수: 6
Devleena Sahoo
Devleena Sahoo 2022년 6월 24일
function old_name = find_max_age(name,age)
old_name = max_age(age)
disp(name)
end
1
2
3
4
Dyuman Joshi
Dyuman Joshi 2022년 6월 24일
1 - There is no function as max_age.
2 - Age is an array of numbers. You have to find the maximum age and return the corresponding name. Suppose, the 3rd element in the age array is the largest, so it is asking you to submit the 3rd element from the name array.
3 - The question is not asking you to display the name. It is asking you to return the name (character arraystring)

댓글을 달려면 로그인하십시오.

답변 (4개)

Maharnab
Maharnab 2022년 11월 9일
function old_name = find_max_age(name,age)
old_name=name(find(age==max(age)));
end

Aniruddh Maini
Aniruddh Maini 2022년 6월 30일
Hi Devleena, following function can be used to find the name of oldest person.
function oldest_name = findOldest(name,age)
len = length(name);
Max_age = -1;
Max_index = -1;
for i = 1:len
if(age(1,i)>Max_age)
Max_age = age(1,i);
Max_index = i;
end
end
oldest_name = name(1,Max_index);
end
Hope it helps !

Iman
Iman 2023년 1월 2일
close all;
clear all;
clc;
function oldest_name = findOldest(name,age)
len = length(name);
Max_age = -1;
Max_index = -1;
for i = 1:len
if(age(1,i)>Max_age)
Max_age = age(1,i);
Max_index = i;
end
end
oldest_name = name(1,Max_index);
end
  댓글 수: 1
Iman
Iman 2023년 1월 2일
편집: Iman 2023년 1월 2일
I don't know if it is correct or wrong. But when I run the program error occur. Please guide me that how to do this question.
2 inputs will be taken
i.what is your name?
ii.what is your age?
Display name and age in matlab.

댓글을 달려면 로그인하십시오.


OM
OM 2023년 1월 10일
편집: OM 2023년 1월 10일
According to what i understood, the name of maximum aged person is asked to find. so, the code can be like this
old_name = name(find(age==max(age)))

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by