Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Page 013 11-200 Chapter 6 Write a program that asks the user to input a vector o

ID: 3888759 • Letter: P

Question

Page 013 11-200 Chapter 6 Write a program that asks the user to input a vector of integers of arbitrary length. Then, using a for-end loop the program eliminates all the negative elements. The program displays the vector that was entered and the modi- fied vector, and a message that says how many elements were eliminated. Execute the program and when the program ask the user to input a vector type randi ([-15 20],1,25). This creates a 25-element vector with random integers between -15 and 20 9. 14. The value of can be estimated from: 32 n+D) Write a program (using a loop) that determines for a given n. Run the pro- gram with n = 10, n = 100, and n = 1,000. Compare the result with pi. (Use format long.) 10 1IT.. AATI A D.

Explanation / Answer

MATLAB code to Question 9

X = input('Enter a vector: '); % Asking the user to enter values
eliminated_No = 0; % variable to count the eliminated values
Modified_X = []; % Array to store modified elements
for j = 1:length(X) % loop to check each elements
    if(X(j)>=0) % Checking the element is positve or negative
        Modified_X = [Modified_X X(j)]; % if positive add them in modified array
    else % if not
        eliminated_No = eliminated_No+1; % increase the cout of eliminated values
    end % End of if
end % end of loop
% printing the results
fprintf('The elements entered ');
disp(X);
fprintf('Modified numbers ');
disp(Modified_X);
fprintf('Numbers eliminated ');
disp(eliminated_No);

OUTPUT

Enter a vector: randi([-15 20],1,25)
The elements entered
Columns 1 through 16

   -14     7    -2   -14     2    -9   -11    -8   -10    -9   -14     7    -5     4    10     2

Columns 17 through 25

     4     1   -11     2    15    16    -6    -8     5

Modified numbers
     7     2     7     4    10     2     4     1     2    15    16     5

Numbers eliminated
    13

QUESTION 14

format long
N = [10,100,1000]; % Various values of n
for i =1:3 % loop for each values of N
    PIVAL = 0; % Clearing the variable
    for n = 0:N(i) % Loop to compute the summation
        PIVAL = PIVAL + ((-1)^n)/((2*n+1)^3);
    end
    PIVAL = (PIVAL*32)^(1/3)
end
pi
OUTPUT

PIVAL =

   3.141642788603785


PIVAL =

   3.141592719141050


PIVAL =

   3.141592653657139


ans =

   3.141592653589793