Write a program in matlab to check if two candidates can run for president. The
ID: 3780220 • Letter: W
Question
Write a program in matlab to check if two candidates can run for president. The program has the functions main_president, fc_input, and elections.
1. Function main_president calls the function fc_input.
2. Function fc_input asks about the data for the candidate: Name, Age, Natural_Born_Citizenship, and Residency_time
3. Function fc_input returns the candidate information as a struct data to the function main_president. 4. Function main calls the function elections(Age must be =>35, born in U.S. Y/N, and years of residency =>14 years) sending the candidate information as struct data.
4. Inside elections, use an if … else statement, to show the message: The Candidate “name” can or can’t run for president.
Explanation / Answer
main_president:
function [ ] = main_president( )
inputData = fc_input();
election(inputData);
end
fc_input:
function [ s ] = fc_input( )
Name = input('Enter name');
Age = input('Enter Age');
Natural_Born_Citizenship = input('Enter Natural Born Citizenship');
Residency_time = input('Enter Residency time');
s = struct('Name',Name,'Age',Age,'Natural_Born_Citizenship',Natural_Born_Citizenship,'Residency_time',Residency_time);
end
elections:
function [ ] = election( inputData )
if(inputData.Age>=35 && strcmp(inputData.Natural_Born_Citizenship,'U.S.') && inputData.Residency_time >=14)
fprintf('The Candidate %s can run for president. ',inputData.Name);
else
fprintf('The Candidate %s can not run for president. ',inputData.Name);
end
end
Result:
Enter name'Barac Obama'
Enter Age45
Enter Natural Born Citizenship'U.S.'
Enter Residency time20
The Candidate Barac Obama can run for president.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.