can any one help my with this quation

1 view (last 30 days)
mohammad alsadi
mohammad alsadi on 15 Dec 2020
Commented: mohammad alsadi on 15 Dec 2020
Write a program that handles a list of numbers where it has the following functions
- Function that finds their average and sum
Function that finds their maximum and minimum
- Function that search for a given number in that list.
- Function that count the occurrence of a given number in that list.
Then the program should do the following:
- Read the list
-Find the average and the sum of that list
- Find the minimum and the maximum of that list
- Count the occurrence of a given number in that list.
  4 Comments
Image Analyst
Image Analyst on 15 Dec 2020
endfor is not a statement. Just use end.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 15 Dec 2020
In the meantime, some hints to help you on your homework: Look up the help on these functions: find(), sum(), mean(), min(), max(). I think that's all you need. You might also want to use fprintf() to output something to the command window, or msgbox() if you want to pop up a message.
  2 Comments
Image Analyst
Image Analyst on 15 Dec 2020
Regarding your code above, you don't need to ask for the list 3 or 4 times, you can ask for it once and operate on everything from then on. Here is simplified code (though most programmers would simplify it even more):
N = input('Enter the size of the list: ');
for k = 1:N
userPrompt = sprintf('Enter number %d of %d ', k, N);
x(k) = input(userPrompt);
end
x % Show in command window.
SUM =0;
for k=1:N
SUM = SUM + x(k);
end
MAX = max(x);
MIN = min(x);
fprintf('Average : %f\n',SUM/N);
fprintf('Max = %f\nMin = %f\n',MAX,MIN);
Y=input('Enter a number to search for: ');
Count = 0;
for k = 1:N
if x(k) == Y
Count = Count+1;
end
end
fprintf('Count = %d.\n',Count);
if(Count >= 1)
fprintf('Found %f %d times.\n', Y, Count);
else
fprintf('%f was not found', Y);
end
Like you can use sum() like I said to count instead of that for loop you have.
count = sum(x == Y)

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!