You are on page 1of 4

DOMINGUEZ, ALDRIN JOHN E.

1. Write a for loop that will print the column of real numbers from 1.5 to 3.1 in
steps of 0.2
SCRIPT FILE:
clear;clc;
for i=1.5:0.2:3.1
disp(i)
end

DOMINGUEZ, ALDRIN JOHN E.

10. Sales (in millions) from two different divisions of a company for the four
quarters of 2012 are stored in vector variables, such as the following:
div1 = [4.2 3.8 3.7 3.8];
div2 = [2.5 2.7 3.1 3.3];
Using subplot, show side-by-side the sales figures for the two divisions. In one
graph, compare the two divisions.
SCRIPT FILE:
x=1:4;
div1 = [4.2 3.8 3.7 3.8];
div2 = [2.5 2.7 3.1 3.3];
figure
subplot(2,2,1)
plot(x,div1)
title('Division 1')
xlabel('Quarters 2012')
ylabel('Sales (Millions)')
subplot(2,2,2)
plot(x,div2,'g')
title('Division 2')
xlabel('Quarters 2012')
ylabel('Sales (Millions)')
subplot(2,2,3:4)
plot(x,div1,x,div2,'g')
legend('Div1','Div2')
title('Comparison')
xlabel('Quarters 2012')
ylabel('Sales (Millions)')

DOMINGUEZ, ALDRIN JOHN E.

21. Write a script echoletters that will prompt the user for letters of the alphabet
and echo print them until the user enters a character that is not a letter of the
alphabet. At that point, the script will print the nonletter and a count of how many
letters were entered. Here are examples of running this script:
>> echoletters
Enter a letter: T
Thanks, you entered a T
Enter a letter: a
Thanks, you entered a a
Enter a letter: 8
8 is not a letter
You entered 2 letters
>> echoletters
Enter a letter: !
! is not a letter
You entered 0 letters
SCRIPT FILE:
clear;clc
a=1;
y=0;
while a==1
x=input('\nEnter a letter: ','s');
a=isletter(x);
if a==1
fprintf('Thanks, you entered a %s',x);
y=y+1;
else
fprintf('%s is not a letter',x);
fprintf('\nYou entered %i letters\n',y);
end
end

DOMINGUEZ, ALDRIN JOHN E.

45. Write a script to add two 30-digit numbers and print the result. This is not as
easy as it might sound at first because integer types may not be able to store a
value this large. One way to handle large integers is to store them in vectors, where
each element in the vector stores a digit of the integer. Your script should initialize
two 30-digit integers, storing each in a vector, and then add these integers, also
storing the result in a vector. Create the original numbers using the randi function.
Hint: add two numbers on paper first and pay attention to what you do!
SCRIPT FILE:
clear;clc;
for a=1:30
x(a)=randi(9);
y(a)=randi(9);
end
x
y
carry=0;
for a=30:-1:1
z(a)=x(a)+y(a)+carry;
if z(a)>=10&a~=1
z(a)=z(a)-10;
carry=1;
else
carry=0;
end
end
sum_of_x_and_y=z

You might also like