Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to check if two candidates can run for president; the program ha

ID: 3802388 • Letter: W

Question

Write a program to check if two candidates can run for president; the program has the functions main, input candidate, elections: Function main calls the function input candidate. Function input candidate asks about the data... candidate: Name, Age, Natural Born Citizenship, Residency time; input candidate returns the candidate information as a struct data to the function main; Function main calls the function elections, sending the candidate information a struct data; Inside elections, use an if... else statement, to show the message: The Candidate "name" can or can't run for president.

Explanation / Answer

main.m

function [ ] = main( )
data=input_candidate();
elections(data);
end

input_candidate.m

function [ data ] = input_candidate( )
field1 = 'name';
value1 = input('Enter Name : ','s');
field2 = 'age';
value2 = input('Enter Age : ');
field3 = 'Natural_Born_Citizenship';
value3 = input('Enter Natural Born Citizenship : ','s');
field4 = 'Residency_time';
value4 = input('Enter Residency Time : ');

data = struct(field1,value1,field2,value2,field3,value3,field4,value4);
end

elections.m

function [ ] = elections( data )
if(strcmp(data.Natural_Born_Citizenship,'USA')==0 || strcmp(data.Natural_Born_Citizenship,'usa')==0)
fprintf('%s can run for President ',data.name);
else
fprintf('%s can not run for President ',data.name);
end
end

Result:

>> main
Enter Name : Peter
Enter Age : 45
Enter Natural Born Citizenship : USA
Enter Residency Time : 12
Peter can run for President
>>
>> main
Enter Name : John
Enter Age : 42
Enter Natural Born Citizenship : Iran
Enter Residency Time : 5
John can run for President

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote