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

조회 수: 2 (최근 30일)
Fe99
Fe99 2020년 11월 12일
답변: Monisha Nalluru 2020년 11월 16일
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에서 Detection에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by