2. Write a Matlab program using FOR loop that computes the sum of the even-index
ID: 3807758 • Letter: 2
Question
2. Write a Matlab program using FOR loop that computes the sum of the even-indexed numbers of an array. load Array1. Use the length command to know the elements in the array.
3. Write a Matlab program using FOR loop that computes the largest element of an array. Load Array1, and use Array1 as the sample array to test your program. Use the length command to know the elements in the array.
4. Write a nested FOR loop (using two for loops & two fprintf statements to generate the following pattern:
5. Using a FOR loop write a program that given a vector of numbers computes how many numbers are greater than 10. Use Array2 to check your program.
Array1 =
71 3 28 5 10 82 69 32 95 3 44 38 77 80 19
Array2 =
7 5 10 3 11 4 1 12 15 2
Explanation / Answer
2)
Array1=[71,3,28,5,10,82,69,32,95,3,44,38,77,80,19];
Array2=[7,5,10,3,11,4,1,12,15,2];
evenindexsum=0;
for i=1:length(Array1)
if mod(i,2)==0
evenindexsum=evenindexsum+Array1(i);
end
end
fprintf(' The sum of Even Index Elements of Array1 is: %d',evenindexsum);
3)
Array1=[71,3,28,5,10,82,69,32,95,3,44,38,77,80,19];
Array2=[7,5,10,3,11,4,1,12,15,2];
ma=0;
for i=1:length(Array1)
if Array1(i)>ma
ma=Array1(i);
end
end
fprintf(' The Maximum Element in Array1 is: %d',ma);
5)
Array1=[71,3,28,5,10,82,69,32,95,3,44,38,77,80,19];
Array2=[7,5,10,3,11,4,1,12,15,2];
count=0;
for i=1:length(Array2)
if Array2(i)>10
count=count+1;
end
end
fprintf(' The number of elements greater than 10 in Array2 is : %d',count);
% In 4th question pattern is missing , If you specify the pattern then i will provide code for that one also
% If you any doubt please let me know .. Thank you..
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.