Write a MATLAB program to do the following: 1- Allows the user to enter unlimited number...

Free

60.1K

Verified Solution

Question

Mechanical Engineering

Write a MATLAB program to do the following:

1- Allows the user to enter unlimited number of data set.

2- Allows the user to exit the program at any time by enteringzero.

3- Calculates and displays the following statistics for theentered data set:

a- Count of positive, negative, and total numbers.

b- Maximum and Minimum numbers.

c- Sum and Average of positive numbers.

d- Sum and Average of negative numbers.

e- Overall Sum and overall average of all numbers.

4- The output of each item should be displayed in two lines: Onefor the item title and the other for the results.

The output should be similar to the following:

Statistics of the data set:

---------------------------

Count of numbers: Positive = nn, Negative = nn, Total = nn

Maximum and Minimum numbers: Maximum number = n.nn, Minimumnumber = -n.nn

Answer & Explanation Solved by verified expert
4.2 Ratings (785 Votes)

clear all;clc;

cnt_pos=0; %counting positive numbers
cnt_neg=0; %counting negative numbers
sum_pos=0; %summing positive numbers
sum_neg=0; %summing negative numbers
i=1;
num=input('Enter the number: ');
while(num~=0)
if num>0
cnt_pos=cnt_pos+1;
sum_pos=sum_pos+num;
else
cnt_neg=cnt_neg+1;
sum_neg=sum_neg+num;
end;
arr(i)=num;
i=i+1;
num=input('\nEnter the number: ');
end;
avg_pos=sum_pos/cnt_pos; %averaging positive numbers
avg_neg=sum_neg/cnt_neg; %averaging negative numbers
maximum=max(arr); %finding maximum of all numbers
minimum=min(arr); %finding minimum of all numbers
fprintf('Statistics of the data set:\n---------------------------\n');
fprintf('\nCount of numbers: Positive= %d, Negative= %d, Total= %d', cnt_pos,cnt_neg,length(arr));
fprintf('\nMaximum and Minimum Numbers: Maximum Number= %1.1f, Minimum Number= %1.1f',maximum,minimum);
fprintf('\nSum and average of positive numbers: Sum= %1.1f, Average= %1.1f',sum_pos,avg_pos);
fprintf('\nSum and average of negative numbers: Sum= %1.1f, Average= %1.1f \n',sum_neg,avg_neg);


Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students