Name:.. ID: Question 1:- [10 Marks]

Islamic University of Gaza Faculty of Engineering Civil Engineering Dep. First Semester (2012-2013) Computer Programming Midterm Exam .ECIV 2303 Eng....
Author: Jade Mathews
38 downloads 2 Views 515KB Size
Islamic University of Gaza Faculty of Engineering Civil Engineering Dep. First Semester (2012-2013)

Computer Programming Midterm Exam .ECIV 2303 Eng. Ramy Salem Eng. Hassan El-Najjar

Allowable Time = 80 min.

Name:………………………………………..

ID:…………………

 Question 1:- [10 Marks] The following statement was written in MATLAB: A = [1:5;linspace(6,10,5);4*ones(1,5);3:-1:-1]; Write what will be displayed if the following commands are executed by MATLAB:>> A 1

2

3

4

5

6

7

8

9

10

4

4

4

4

4

3

2

1

0

-1

>> B=A(2:3,4:5) 9

10

4

4

>>C=A([1,3],[1:3,5]) 1

2

3

5

4

4

4

4

>> D=[A(4,1:3)',C(1,2:4)'] 3

2

2

3

1

5

>> E([2,5],[1,3])=A([1,3],[2,5]) 0

0

0

2

0

5

0

0

0

0

0

0

4

0

4

Page 1 of 9

Computer Programming “ MATLAB”  Question 2:-

Midterm 2012/2013

[10 Marks]

Consider the command window outputs shown below. Write a program (using MATLAB) that executes this output: Hint: “The value of each element in the first row is the number of the column. The value of each element in the first column is the number of the row. The rest of the elements each has a value equal to the sum of the element above it and the element to the left. “Do not type individual elements explicitly “

Solution :x =input('Enter the number of rows : '); y =input('Enter the number of columns : '); a = zeros(x,y);a(1,:) =1:y;a(:,1) = [1:x]'; for i = 2:x for j=2:y a(i,j) = a(i-1,j) + a(i,j-1); end end disp(a) n =input('Enter the number of rows : ');m =input('Enter the number of columns : '); for k = 1:n for h=1:m if k==1 a(k,h)=h; elseif h==1 a(k,h) =k; else a(k,h) =a(k,h-1) + a(k-1,h); end end end a Page 2 of 9

Computer Programming “ MATLAB”

Midterm 2012/2013

 Question 3:- [15 Marks] Find the output of the following programs:A.

B.

C.

D.

E. F.

G.

H.

clc ,clear all teams = randperm(12) ; fprintf('%i vs. %i \n' , teams) clc , clear all g = [90,80,70]; fprintf(' There are %i grades .\n',length(g)); fprintf(' The average grade is %3.2f .\n',mean(g)); fprintf(' The median is %3.2f .\n',median(g)); Clc for i=1:5 for ii=1:6-i fprintf('@') end fprintf('\n') end clc a = zeros(4,3); for i=1:4 for j=1:3 a(i,j)=j ; end end disp(a) x = [73,91,37,81,63,66]; y = sort(x,'descend');disp(y) x = [-3.5 ,5, -6.2 ,11.1 ,0 , 7, -9.5, 2, 15, -1, 3, 2.5]; P=x(x>0); N = x(x 3*~5+1-3 > 8; disp(y) clc,clear all x = [1 ,2 ,3 ,9 ,3 ,2 ,1]; for i =1:length(x) if i ==4 continue end y(i) = x(i)^2 ; end disp(y)

Page 3 of 9

Computer Programming “ MATLAB”

Midterm 2012/2013

Solution;A.

B.

C.

D.

E. F. G. H.

8 vs. 10 11 vs. 6 9 vs. 4 5 vs. 1 7 vs. 3 2 vs. 12 There are 3 grades . The average grade is 80.00 . The median is 80.00 . @@@@@ @@@@ @@@ @@ @ 1 2 3 1 2 3 1 2 3 1 2 3 91 81 73 66 63 37 5 11.1 7 2 -3.5 -6.2 -9.5 -1 1 1 4 9 0 9 4 1

15

Page 4 of 9

3

2.5

Computer Programming “ MATLAB”

Midterm 2012/2013

 Question 4:- [10 Marks] A. Write 3 commands to solve the following system of five linear equations : Use the Right division.

A = [3,1.5,0;-2,1,2;6,0,0]; B=[-11.75;19;-8]; X=B'/A' or C = [3 -2 6;1.5 1 0;0 2 0]; D=[-11.75 19 -8]; X = D/C

B. A twin primes is a pair of prime numbers such that the difference between them is 2 (for example, 17 and 19). Write a computer program that finds all the twin primes between 10 and 500. The program displays the results in a two-column matrix in which each row is a twin prime. “You allowed using primes command to generate the prime’s numbers “.

x = primes(500); x = x(x>10); for i=2:length(x) if (x(i) - x(i-1) == 2) fprintf('%i \t %i \n',x(i-1),x(i)); end end or x = primes(500); x = x(x>10); for i=1:length(x)-1 if (x(i+1) - x(i) == 2) fprintf('%i \t %i \n',x(i),x(i+1)); end end Page 5 of 9

Computer Programming “ MATLAB”  Question 5 :-

Midterm 2012/2013

[10 Marks]

A. Find the error and make a correction:X =-5:-10 X=10 ; y = Cos(x) a=rand(2);b=[7 ,8 ,1 ,3]; c=a/b; A=5; break b=8*A; x = 5; fprintf(The value of x is %s , x)

X = -5 : -1: -10 X = 10 ; y= cos(X) a=rand(2);b=[7,8;1,3];c=a/b; A=5; b=8*A; break x=5; fprintf(‘The value of x is %i ‘,x)

B. Consider the command window outputs shown below. Write a program (using MATLAB) that executes this output: “Do not type individual elements explicitly “

for i=1:10 for j=1:i fprintf('A%i',j) end fprintf('\n') end x=1:10; for i=1:10 for j=1:i fprintf('A%i',x(j)) end fprintf('\n') end Page 6 of 9

Computer Programming “ MATLAB” 

Bonus :

Midterm 2012/2013

[5 Marks]

Write a script beautyofmath that produces the following output.          

beautyofmath 1x8‫ =‏‬9 12 x 8 ‫ = ‏‬98 123 x 8 ‫ = ‏‬987 1234 x 8 ‫ = ‏‬9876 12345 x 8 ‫ = ‏‬98765 123456 x 8 ‫ = ‏‬987654 1234567 x 8 ‫ = ‏‬9876543 12345678 x 8 ‫ = ‏‬98765432 123456789 x 8 ‫ = ‏‬987654321

x ='123456789'; y='987654321'; for i=1:length(x) fprintf('%s x 8 = %s \n',x(1:i),y(1:i)) end

Page 7 of 9

Computer Programming “ MATLAB”

Command clc clear clear x y z close fclose fopen help who whos break case continue else elseif end for if otherwise switch while eye linspace ones rand randi randn randperm zeros length reshape size mean median sort sort(A,'descend') disp fprintf num2str

Midterm 2012/2013

Description Clears the Command Window Removes all variables from the memory Removes variables x, y, and z from the memory Closes the active Figure Window Closes a file Opens a file Displays help for MATLAB functions Displays variables currently in the memory Displays information on variables in the memory Terminates execution of a loop Conditionally execute commands Terminates a pass in a loop Conditionally execute commands Conditionally execute commands Terminates conditional statements and loops Repeats execution of a group of commands Conditionally execute commands Conditionally execute commands Switches among several cases based on expression Repeats execution of a group of commands Creates a unit matrix Creates equally spaced vector Creates an array with ones Creates an array with random numbers Creates an array with random integers Creates an array with normally distributed numbers Creates vector with permutation of integers Creates an array with zeros Number of elements in the vector Rearrange a matrix Size of an array Calculates mean value Calculates median value Arranges elements in ascending order Arranges elements in Descending order Displays output Displays or saves output Convert numbers to a string Page 8 of 9

Computer Programming “ MATLAB” str2num

Midterm 2012/2013

Convert string matrix to numeric array

Page 9 of 9