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

MatLab Programming Write a script that will create a (random) vector of ages (re

ID: 3801679 • Letter: M

Question

MatLab Programming

Write a script that will create a (random) vector of ages (real numbers, not integers). The script should contain the following prompts: -the user will provide the number of elements in the vector. -the user will provide the age of the oldest person in the group. For example: Enter the number of persons in the group: 50 Enter the age of the oldest person: 37 Save the vector containing the ages in a ".dat file" called ages_vector.dat Write a script that will read the ages_vector.dat file created in the previous problem. -the script will print the number of persons in the group -the script will compute and print the "average age"

Explanation / Answer

Here is the code for the first problem:

% Script that will create a (random) vector of ages (real numbers, not

% integers). The script should containt the following prmpts:

% -The user will provide the number of elements in the vector.

% -The user will provide the age of the oldest person in the group.

numOfPeople = input('Enter the number of persons in the group: ');

oldestPerson = input('Enter the age of the oldest person: ');

ages = 1 + (1+oldestPerson)*rand(numOfPeople,1);

filePointer = fopen('ages_vector.dat','w');

for i = 1 : numOfPeople

fprintf(filePointer, '%.1f ', ages(i));

end

fprintf(filePointer, ' ');