0% found this document useful (0 votes)
68 views18 pages

Computer-T17-Solution Assignment-OK - 230418 - 042205

1. The program takes input for values A and B. 2. It compares the values of A and B. 3. If A is greater than B, it displays "greater". If A is less than B, it displays "less". If A is equal to B, it displays "equal".

Uploaded by

Iraqi for info
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views18 pages

Computer-T17-Solution Assignment-OK - 230418 - 042205

1. The program takes input for values A and B. 2. It compares the values of A and B. 3. If A is greater than B, it displays "greater". If A is less than B, it displays "less". If A is equal to B, it displays "equal".

Uploaded by

Iraqi for info
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Ministry of Higher Education and Scientific Research Southern

Southern Technical University


Engineering Technical College / Basrah
Department of Electrical Power Techniques Engineering

Computer/3
Lect.17/Assignment Solution

Third Year

Lecturer : M.Sc. 𝐖𝐚𝐟𝐞𝐞𝐪𝐚 𝐀𝐛𝐝𝐮𝐥𝐫𝐚𝐳𝐚𝐤 𝐇𝐚𝐬𝐚𝐧


➢ fprintf
• Formatted output can be printed to the screen using the fprintf function. For example:
• The fprintf function, first a string (called the format string) is passed, which contains any text to be
printed as well as formatting information for the expressions to be printed.
• The character in the placeholder is called the conversion character, and it specifies the type of value
that is being printed. There are others of the simple placeholders:
Placeholder Description Example
(character)
%d Format as a integer >> fprintf ( '%d', 4^5)
>>1024
%f Format as a floating point value >> fprintf ( '%f', sqrt(90.25))
>>9.500000
%g Format as the most compact >> fprintf ( '%g', sqrt(90.25))
form (no trailing zero) >>9.5
%e Format as a floating point >> fprintf ( '%e', pi)
value in scientific notation >>3.141593e+000
%s Format as a string >> fprintf ('%s', '3*8/2')
>>3*8/2
\n Insert a new line in the output >> fprintf ('Welcome! \n this is MATLAB')
string >>Welcome!
this is MATLAB
\t Insert a tab in the output string >> fprintf ('Welcome! \t this is MATLAB')
>>Welcome! this is MATLAB
Notes :
It’s important adding the character \n at the end of output string in order to avoid the
prompt (>>) from stick to the result as shown before.
❑ The character \n can also use in input function, for example:

>> h = input ('Enter \n The shape height :')


Enter
The shape height :

❑ The character \n in form ‘\n\n’ use to get blank line in output, for example:
>> fprintf ('Hello \n\n This is MATLAB \n')
Hello
This is MATLAB

❑ field width can also be included in the placeholder in fprintf, which specifies how many
characters total are to be used in printing.
▪ %5d would indicate a field width of 5 for printing an integer and
▪ %10s would indicate a field width of 10 for a string.
▪ %6.2f means a field width of 6(including the decimal point and the decimal places)
with two decimal places.
▪ %.3f indicates three decimal places.
Assignment /1/Lec.12 :

1. Write a program to read a string, then replace each character in the


string with its following character in ASCII code*.

2. If x=[1 5 9; 2 7 4], then:


a) display the last two elements by using disp command.
b) display the sum of each row as show below
The sum of 1st row =
The sum of 2nd row =

3. By using M-files in MATLAB ,plot the following cosine functions , y1 = 2


cos(x), y2 = cos(x), and y3 = 0.5 cos(x), in the interval 0 ≤ x ≤ 2π.And
make all format to the plot.
Assignment /1/Lec.12 :

1. Write a program to read a string, then replace each character in the


string with its following character in ASCII code*.

Solution

clear;
clc;
x=input('string =','s')
double(x)

• Convertcode
string =IRAQ
x=
'IRAQ’
105 114 97 113
Assignment /1/Lec.12 :
2. If x=[1 5 9; 2 7 4], then:
a) display the last two elements by using disp command.
b) display the sum of each row as show below
The sum of 1st row =
The sum of 2nd row =
Solutiona)
x=input('parameter=‘)
x=input('parameter=')
disp(x(2,2))
disp(x(2,3))
b)
y1=sum(x(1,:));
y2=sum(x(2,:));
disp(['The sum of 1st row =',num2str(y1)])
disp(['The sum of 2st row =',num2str(y2)])

>> ass
parameter=[1 5 9;2 7 4];
x=
1 5 9
2 7 4
7
4
The sum of 1st row =15
The sum of 2st row =13
Assignment /1/Lec.12 :

3. By using M-files in MATLAB ,plot the following cosine functions , y1 = 2


cos(x), y2 = cos(x), and y3 = 0.5 cos(x), in the interval 0 ≤ x ≤ 2π.And
make all format to the plot.

Solution
x=0:0.01:2*pi;
y1 = 2* cos(x);
y2 = cos(x);
y3 = 0.5 *cos(x);
plot(x,y1,'--r',x,y2,'-.g',x,y3,'-b')
xlabel('x-axis')
ylabel ('y-axis')
title('use plot function in M-file')
grid
gtext('y1')
gtext('y2')
gtext('y3’)
Assignment /2/Lec.13 :

1. write a program to find the cosine of angles (0,15,45,60,75,90) and


format the output result as : “The cosine of angle X is : Y “, named
file“AngleCosine”

2. write a program (vector.m) to generate a vector with 12 random


elements and find:

a. The largest element and its position.


b. The smallest element and its position.

3. Write a program to test the degrees of 40 students and find: (named


file “students”)
1) The number of students those degrees above 80
2) The number of students those degrees between 60 and 79
3) The number of students those failed and their degrees
Assignment /2/Lec.13 :
1: write a program to find the cosine of angles (0,15,45,60,75,90) and
format the output result as : “The cosine of angle X is : Y “, named
file“AngleCosine”
Solution
clc;
clear;
theta=0:15:90;
cosine=cosd(theta);
result=[theta;cosine];
fprintf('The cosine of angle %d is :%4.3f \n',result)

>> Anglecosine
The cosine of angle 0 is :1.000
The cosine of angle 15 is :0.966
The cosine of angle 30 is :0.866
The cosine of angle 45 is :0.707
The cosine of angle 60 is :0.500
The cosine of angle 75 is :0.259
The cosine of angle 90 is :0.000
Assignment /2/Lec.13 :
2: write a program (vector.m) to generate a vector with 12 random elements and find:
a. The largest element and its position.
b. The smallest element and its position.

Solution
clc
clear;
format bank
v=rand(1,12);
[vmax,pmax]=max(v)
[vmin,pmin]=min(v)
>> vector
vmax =
0.97
pmax =
12.00
vmin =
0.10
pmin =
6.00
Assignment /2/Lec.13 :
3. Write a program to test the degrees of 40 students and find: (namedfile “students”)
1) The number of students those degrees above 80
2) The number of students those degrees between 60 and 79
3) The number of students those failed and their degrees

Solution
clear; clc;
Degree= randi([25,100],1,40); % generate the degrees of 40 students their range (25-100)

y=Degree'
DegreeAbove80=sum(Degree>80); % find no. of students which degree>80 using sum function

Degree60to79=sum(Degree>=60 & Degree<80); % find no. of students which 60<degree<80

Failures=sum(Degree<50) % find no. of failures students


fprintf('\n') % format output results
disp('------------------------------------------------')
fprintf('The no. of students with degree above 80 :
%d\n',DegreeAbove80)
fprintf('The no. of students with degrees 60 to 79 :
%d\n',Degree60to79)
fprintf('The no. of failures students: %d\n',Failures)
fprintf('And their degrees are :\n')
disp(Degree(Degree<50)) % display the degrees which are less than 50
>> student
DegreeAbove80 =

9.00
Failures =
12.00
------------------------------------------------
The no. of students with degree above 80 : 9
The no. of students with degrees 60 to 79 : 13
The no. of failures students: 12

And their degrees are :


Columns 1 through 4
29.00 45.00 47.00 38.00
Columns 5 through 8
34.00 38.00 27.00 39.00

Columns 9 through 12

36.00 39.00 34.00 42.00

>>
Assignment /3/Lec.14 :
1. Write a program to compeer between the value A and B ,when Ais greater than B
it is display a message (‘greater’) ,when A is less than B it is display a
message(‘less’) and when A is equal B it is display a message(‘equal’): (named
file “comparevalue”)
2. write a program to find the Y value when: (named file “Zvalue”)
𝟐
−𝟏𝟎 ≤ 𝐱 < 𝟎
𝐱𝟑
𝐙= 𝟐 𝐱=𝟎
𝟑
𝐱𝟐 + 𝟒 𝟎<𝐱≤𝟕

3. Write a program to display the name of day by giving the day number as the
following:
No. Day
1 Saturday
2 Sunday
3 Monday
4 Tuesday
5 Wednesday
6 Thursday

And handle the invalid number (named file “dayname”) 7 Friday


Assignment /3/Lec.14 :
1:Write a program to compeer between the value A and B ,when Ais greater than B it is
write (‘greater’) ,when Ais less than B is write (‘less’) and when A is equal B is write (‘equal:
(named file “comparevalue”)

Solution :
A=input('A=');
B=input('B=');
if A > B
'greater'
elseif A < B
'less' >> comparevalue
A=500
elseif A == B B=5000
'equal' ans =
else 'less'
>> comparevalue
error ('Unexpected situation') A=20
end B=2
ans =
'greater'
Assignment /3/Lec.14 :
2:write a program to find the Y value when: (named file “Zvalue”)
𝟐
−𝟏𝟎 ≤ 𝐱 < 𝟎
𝐱𝟑
𝐙= 𝟐 𝐱=𝟎
𝟑
𝟐
𝐱 +𝟒 𝟎<𝐱≤𝟕

Solution :
x=input('Enter the x value : '); >> Zvalue
if x<-10 | x>7 Enter the x value : 9
disp('Undefined the Y value') Undefined the Y value
elseif x>=-10 & x<0 >> Zvalue
Enter the x value : 0
Y=2/x^3;
The Y value = 2
fprintf('The Y value = %0.3f\n ',Y); x >> Zvalue
elseif x>0 & x<=7 Enter the x value : -5
Y=nthroot(sqrt(x^2+4),3); The Y value = -0.016
fprintf('The Z value = %0.3f\n ',Y); >> Zvalue
else Enter the x value : 1
fprintf('The Z value = %d\n ',2); The Y value = 1.308
end
𝟐
−𝟏𝟎 ≤ 𝐱 < 𝟎
𝐱𝟑
𝐙= 𝟐 𝐱=𝟎
𝟑
𝐱𝟐 + 𝟒 𝟎<𝐱≤𝟕

>> Zvalue
Enter the x value : 9
Undefined the Y value
>> Zvalue
Enter the x value : 0
The Y value = 2
x >> Zvalue
Enter the x value : -5
The Y value = -0.016
>> Zvalue
Enter the x value : 1
The Y value = 1.308
Assignment /3/Lec.14 :
3: Write a program to display the name of day by giving the day
number as the following:And handle the invalid number (named file “dayname”

Solution : No. Day


Nday=input('Enter The Day Number : ');
1 Saturday
switch Nday
case 1 2 Sunday
disp('The day is SATURDAY'); 3 Monday
case 2 4 Tuesday
disp('The day is SUNDAY'); 5 Wednesday
case 3 Thursday
6
disp('The day is MONDAY');
7 Friday
case 4
disp('The day is TUESDAY'); >> dayname
case 5 Enter The Day Number : 3
disp('The day is WEDNESDAY'); The day is MONDAY
case 6 >> dayname
disp('The day is THURSDAY'); Enter The Day Number : 7
case 7 The day is FRIDAY
disp('The day is FRIDAY'); >> dayname
otherwise Enter The Day Number : 10
disp('Invalid’); Invalid
end
Thank You

You might also like