필터 지우기
필터 지우기

How do I fix my error in line 6?

조회 수: 3 (최근 30일)
Abigail McGahey
Abigail McGahey 2019년 2월 19일
댓글: Walter Roberson 2019년 2월 19일
Download the code sumOfIntegers.m, we can measure how long the function takes to execute for various values of N. We will focus on the main loop in the function. We can add a tic/toc pair around the loop so it becomes:
%...
tic
for j = 1 : N
total = total + a(j); end
toc %.
..
Save the code, and run the function with values of N one thousand and one million. Report and compare the time used to execute the function.
function sumOfIntegers(N)
% Usage: sumOfIntegers(N)
% Add up all the integers from 1 up to N and report
% the result.
n=input('final number:')
a = 1:N
total = 0;
for j = 1:N
total = total + a(j);
end
% ...
tic
for j=1:N
total=total+a(j);
end
toc
% ...
fprintf('Sum of first %u integers = %u \n', N, total);
When i go to run this code, i keep getting an error in line 6 saying i don't have enough input values. I'm not sure how to fix that.
  댓글 수: 2
Matt J
Matt J 2019년 2월 19일
Not me. Here's what I get when I run with N=10.
>> sumOfIntegers(10)
final number:1
n =
1
a =
1 2 3 4 5 6 7 8 9 10
Elapsed time is 0.000039 seconds.
Sum of first 10 integers = 110
Walter Roberson
Walter Roberson 2019년 2월 19일
On the other hand, line 6 is exactly the place you would get an error if you attempted to run the code without passing any input.
Question: why do you bother calling input() when you do not use the value that is input() ?

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by