it is matlab course, could you explain the coding and the concepts of it a. Writ
ID: 3737039 • Letter: I
Question
it is matlab course, could you explain the coding and the concepts of it
a. Write a function, called digits_function that is able to calculate the number of digits of an input and then adds all digits to find their sum. The input of this function is N and the outputs are number_digits and sum_digits. The input N should be requested in the main function and should be greater than 4 digits. Assume all digits are over 0.
b. Write a function, called average_function that calculate and prints the average of four numbers. The inputs of this function are a, b and c and d. If one of the numbers is 0 or over 10, it should be excluded from the average calculation.
Example: if input b=12, the average is and so on.
Note: This function does not have an output since its prints the average inside the function!
c. Write a function, called prime_function that receives one input x, and checks whether the number is prime or not. The input of this function is x and the output is result_prime. A string of ‘prime’ or ‘not prime’ should be printed in the main function.
d. Now after you created the three functions, you have to write one main function that can call the above functions according to the user’s choice. Write a menu that allows the user to select from the following options
1. To use the digits function you have to enter 1.
2. To use the average function you have to enter 2.
3. To use the prime function you have to enter 3.
4. To Exit the program you have to enter 4.
Once the user select one of the above choices you have to read the value entered by the user and then to call the respective function. If the user entered a number that is not equal to 1 or 2 or 3 or 4, the program should ask the user to re-enter again.
Then, you have to use “input” to enter the values of the variables (inputs of the functions).
Note: The input should be in the main function.
For the digits_function, you have to enter only positive number and for the prime_function you have to enter greater than 1 values. Otherwise, and error message should appear along with a request for a new input.
(User entered values are indicated in bold)
----------------------------------------------------------------------------
For DIGITS COUNT function enter 1
For AVERAGE function enter 2
For PRIME function enter 3
To EXIT enter 4
Your choice: 1
Enter a number to calculate digits and sum: 45659
The number of digits is: 5
And the sum of digits is: 29
------------------------------------------------------
----------------------------------------------------------
For DIGITS COUNT function enter 1
For AVERAGE function enter 2
For PRIME function enter 3
To EXIT enter 4
Your choice: 2
Enter the first input: 4
Enter the second input: 1
Enter the third input: 0
Enter the fourth input: 10
The average is: 5.000000
-----------------------------------------------------
--------------------------------------------------
For DIGITS COUNT function enter 1
For AVERAGE function enter 2
For PRIME function enter 3
To EXIT enter 4
Your choice: 3
Enter a number to check if it is prime: 7
7 is a prime number
--------------------------------------------------------------------------
For DIGITS COUNT function enter 1
For AVERAGE function enter 2
For PRIME function enter 3
To EXIT enter 4
Your choice: 1
Enter a number to calculate digits and sum: 45659
The number of digits is: 5
And the sum of digits is: 29
------------------------------------------------------
Explanation / Answer
%-----------------------if you have any doubt comment below-----------------------------------------
%--dont ferget to THUMBS UP----------------------------------------------
%------------------main function------------------------------------------------------------------
clc;
clear all;
flag=1;
while(flag)
disp('For DIGITS COUNT function enter 1');
disp('For AVERAGE function enter 2');
disp('For PRIME function enter 3');
disp('To EXIT enter 4');
prompt = 'your choice: ';
choice = input(prompt);
switch (choice)
case 1
prompt = 'Enter a positive number to calculate digits and sum: ';
N = input(prompt);
nd=numel(num2str(N)); %numel is used focalulating number of element in string and num2str convert number to string
while(N<0 || nd<5)
if(N<0)
prompt = 'Number entered should be positive, re-enter number ';
N = input(prompt);
end
if(nd<5)
prompt = 'Number entered should have more than 4 digits, re-enter number ';
N = input(prompt);
end
nd=numel(num2str(N));
end
[ number_digits,sum_digits ] = digits_function( N );
fprintf('The number of digits is: = %d ',number_digits);
fprintf('The sum of digits is: = %d ',sum_digits);
case 2
prompt = 'Enter the first input ';
a = input(prompt);
prompt = 'Enter the second input ';
b = input(prompt);
prompt = 'Enter the third input ';
c = input(prompt);
prompt = 'Enter the forth input ';
d = input(prompt);
average_function( a,b,c,d);
case 3
prompt = 'Enter a number to check if it is prime: ';
x = input(prompt);
while(x<1 || x==1)
prompt = 'Number entered should be greater than 1, re-enter number ';
x = input(prompt);
end
[ result_prime ] = prime_function( x );
if(result_prime==1)
fprintf('%d is a prime number ',x);
else
fprintf('%d is not a prime number ',x);
end
case 4
flag=0;
break;
otherwise
disp('invalid option, re-enter your option');
fprintf(' ');
end
end
%------------------------digit function-----------------------------------------------------------------------------
function [ number_digits,sum_digits ] = digits_function( N )
nd=numel(num2str(N)); %numel is used focalulating number of element in string and num2str convert number to string
sd=0;
b=num2str(N); %num2str convert number to string;
for i=1:nd
str = str2double(b(i)); %string2double convert stinrg to double so that we can add it
sd=sd+str; %sd=sd+b(i) this we cant write as value of b(i) is considered to be ASCII value of int
end
number_digits=nd;
sum_digits=sd;
end
%---------------------------------average function-----------------------------------
function average_function( a,b,c,d)
sum=a+b+c+d; %first we add all 4 variable then minus then if they fall in fallowing four cases
count=4;
if (a==0 || a>10) %given condition value should not ne 0 or greater than 10
count=count-1;
sum=sum-a;
end
if (b==0 || b>10 )
count=count-1;
sum=sum-b;
end
if (c==0 || c>10)
count=count-1;
sum=sum-c;
end
if (d==0 || d>10)
count=count-1;
sum=sum-d;
end
if(count~=0)
avg=sum/count;
end
fprintf('The average is: %d ',avg);
end
%------------------------------prime function--------------------------
function [ result_prime ] = prime_function( x )
result_prime=isprime(x); %isprime return wether function is prime or not
%it return 1 if number is prime and return 0 if not prime
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.