Problem 3 upload the text file Homework 3 to MATLAB and in one script write the
ID: 3817173 • Letter: P
Question
Problem 3 upload the text file Homework 3 to MATLAB and in one script write the code that does the following: a) Separate the data into 3 variables by column: Student Number (1-50), Age, and Gender Male) b) Determine how many of the students in the file (out of 50) that are in the target age range for OH WOW! (ages 2-12) c) Display the results in the command window: "There are xx students that are in OH WOW's target age range" d) create a histogram of the ages of students in the data file (give a meaningful title and labels) e) Determine how many of the students in the file are male and how many are female f) Display the results in the command window: "There are xx female and YY male students in the data set." g) Create a pie chart for the gender distribution in the data set (give a meaningful title) h) Ask the user to enter a number between 1-50 for the student Student Number. Error check to ensure that the value entered does fall within the expected range (1-50), and if it does not then ask them to reenter a value (make sure that the user could enter incorrect values multiple times without making the program stop)Explanation / Answer
fileID = fopen('Homework5Prob3','r'); %open file specified in question
sizeData = [3 Inf]; %size to read
%Inf is infinite, indicating that the file is infinitely long
Data = fscanf(fileID, "%d %d %d",sizeData); %read data in a variable
StuNum = Data (1, :); %seprate the data into differnt variable row wise
Age = Data(2, :);
Gender = Data(3, :);
count = sum(Age >= 2 & Age <= 12); %logically count students within given age group
printf("there are %d students that are in OH WOW's target age range ", count); %print
hist(Age) %plot histogram
title('Histogram of Age of students'); %add title and labels
xlabel('Age');
ylabel('Count');
countMale = sum (Gender==0); %count male students logically
countFemale = sum(Gender==1); %similarly count female
printf("There are %d females and %d male students in the data set ", countFemale, countMale); %print
pie([countMale countFemale], {'Male', 'Female'}) %draw pie chart
choice = input('Enter Student number: '); %ask for input
while choice <1 || choice>50 %check validity
printf("Invaid Student number. Range is [1-50] ");
choice = input('Enter Student number: '); %if invalid, re-enter
end
printf("Sudent number %d is ",choice); %print student number
if Gender(choice) == 0 %print gender
printf("male");
else
printf("female");
end
printf(" and %d years old ",Age(choice)); %and finally print age
Hey champ. I commented the code as far as possible just to make your life easy. Let me know in comment section below if you like the code, or even if you are facing issues with the code. I shall try my best to solve your query.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.