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

4 views (last 30 days)
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 Comment
Jon
Jon on 12 Nov 2020
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

Sign in to comment.

Answers (1)

Monisha Nalluru
Monisha Nalluru on 16 Nov 2020
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)

Categories

Find more on Get Started with MATLAB 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!