Effacer les filtres
Effacer les filtres

How do I fix my error in line 6?

3 vues (au cours des 30 derniers jours)
Abigail McGahey
Abigail McGahey le 19 Fév 2019
Commenté : Walter Roberson le 19 Fév 2019
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 commentaires
Matt J
Matt J le 19 Fév 2019
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 le 19 Fév 2019
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() ?

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by