i want code with for and if and disp
1 view (last 30 days)
Show older comments
Write a program that reads N numbers from the user and prints the average of the elements greater than 6
0 Comments
Accepted Answer
Piyush Kumar
on 24 Sep 2024
N = input('Enter the number of elements: ');
sum = 0;
count = 0;
for i = 1:N
num = input('Enter the number: ');
if num > 6
sum = sum + num;
count = count + 1;
end
end
average = sum / count;
disp(['The average of elements greater than 6 is: ', num2str(average)]);
2 Comments
John D'Errico
on 24 Sep 2024
Edited: John D'Errico
on 25 Sep 2024
Please don't do homework assignments. for students who make no effort. This does not help the student. It teaches them nothing more than how to find a someone with nothing better to do with their time than to do their work for them.
It does not help the forum, as it teaches the student to continue posting ther homework assignments while making no effort.
It actively hurts the forum, as it then teaches other students to do the same, and then get upset when they don't get immediate help.
If you want to help the student, consider guiding them, giving them hints. To the extent a student does make an effort, then give more direct help. Make your answer proportionate to the effort they have made.
Piyush Kumar
on 25 Sep 2024
I appreciate your suggestion to guide students rather than providing direct answers. This approach indeed benefits both the students and the forum by encouraging learning and engagement. I have noted this point.
Best Regards,
Piyush Kumar
More Answers (1)
Umar
on 24 Sep 2024
Hi @Jasmin,
You are looking to create a MATLAB program that performs specific tasks: reading a series of numbers from the user and calculating the average of those numbers that exceed a value of 6. This involves user input, conditional filtering, and basic arithmetic operations. I will guide you through the process because if you learn the basics, you should be able to create this program yourself. Here are steps to accomplish your goal.
The first step in your program will involve prompting the user to input a specified number N of elements. This can be done using a loop or an array where the user can enter multiple values. You will need to store these numbers in a suitable data structure, such as an array or vector, which allows for easy manipulation and analysis. Once you have all the numbers stored, the next step is to filter out those that are greater than 6. This involves iterating through your data structure and applying a condition that checks each number. After identifying the numbers greater than 6, you will need to calculate their average. This requires summing these filtered numbers and dividing by the count of how many numbers met this condition. Finally, you will present the calculated average to the user in a clear and concise manner. Please consider implementing error handling to manage non-numeric inputs or cases where no numbers exceed 6, which could lead to division by zero when calculating the average. Make sure that prompts for user input are clear so users understand how many numbers they should enter and what type of input is expected. Also, to help you further familiarize yourself with relevant MATLAB functions such as mean(),logical indexing for filtering arrays, and built-in functions for handling user input efficiently.
Following these steps should help you get started with your program. If you have further questions or need clarification on any specific aspect, feel free to ask!
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!