I want to know how to do flowchart and write program for my problem

4 次查看(过去 30 天)
I want to write a program that will read numbers from a file and get the sum of positive numbers, sum of negative numbers and sum of all numbers using FOR and IF function.
Example of numbers in file:(12 3 -4.4 0 9.9 34.45 0 -23.09)
  1 个评论
Jon
Jon 2020-11-12
This sounds like it may be a homework problem. This website is a good place to go once you have some specific MATLAB programming problems. Assuming it is homework, you need to get far enough on your own to write some initial code and then you will get lots of help with problems you may be having with your code. If you don't even know enough to get started I would suggest first working with your professor or TA and classmates to get that kind of help. If you don't know enough about MATLAB to write any code then please complete the MATLAB On Ramp training https://www.mathworks.com/learn/tutorials/matlab-onramp.html

请先登录,再进行评论。

回答(1 个)

Monisha Nalluru
Monisha Nalluru 2020-11-16
Use readtable, readmatrix functions inoreder to read data from file.
Once the data is stored in table or matrix, use for loop inorder to check each number and based on condition calculate the sum
As an example
a=[1,-20,30,14,-40,60,80];
sum_positive=0;
sum_negative=0;
for i=1:length(a) % iterate over the elements in a
if a(i)<0 % check whether number is less than zero
sum_negative=sum_negative+a(i); % calculating sum of all negative number
else
sum_positive=sum_positive+a(i); % calculating sum of all positive number
end
end
disp(sum_negative)
disp(sum_positive)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by