CISC106-010, Fall 2007

Answers to the PracticeQuestions_2007_11_7

 

 

Strings, String Concatenation. 1

A1. 1

A2. 1

Formatted Output with fprintf. 1

A1. 1

A2. 1

Cell Arrays, Content indexing vs. Cell indexing. 2

A1. 2

A2. 2

A3. 2

A4. 2

A5. 2

A6. 2

A7. 2

A8. 2

Repetition Structures (while loops, for loops, nested loops) and Selection Structures (If/Else) 2

A1. 2

A2. 2

A3. 2

A4. 3

A5. 4

File Operations. 4

A1. 4

SPLITing a string using  regexp built-in function. 4

A1. 4

A2. 5

Script M-files, Function M-files, Test scripts. 5

Chmod Unix Command. 5

A1. 5

A2. 5

 

Strings, String Concatenation

A1.  >> name(3)

A2. >> fullName = [firstName ' ' lastName]

Formatted Output with fprintf

A1. >> fprintf('The answer is:%9.2f\n', 12.34567)

A2. >> fprintf('My name is %s and I am %d years old.\n', name, age)

 

Cell Arrays, Content indexing vs. Cell indexing

A1. (b) 1x3 matrix of doubles

 

A2. (a) 1x1  cell array

 

A3. (a) 1x3 cell array

 

A4.     i  (i.e., the 2nd char of the string mike)

 

A5.  mike

 

A6. mycell{3} returns the matrix in the 3rd coloum of the cell array mycell. That is,  

mike

john

 

A7. Any of the expressions below would work.

 

>> mycell{3}(2,1)

 

>> mycell{1,3}(2,1)

 

>> mycell{1,3}(2)

 

A8.  >> mycell{1,3}(1, :) = 'adam'

Repetition Structures (while loops, for loops, nested loops) and Selection Structures (If/Else)

A1.

v = [10 20 30 40];

 

index = 1;

while (index < length(v))

   fprintf('%d ', v(index));

   index = index + 1;

end % while

fprintf('\n');

 

A2.

 

for i = [1:6]

   fprintf('%d ', i);

end % for

fprintf('\n');

 

A3.

 

function result = howManyMs(str)

%  inputs:

%     str: the string that we are searching the m's

%  outputs:

%     result: number of m's in the string

%  Example:

% >> howManyMs('how let the dogs out?')

%

% ans =

%   0

%

% >> howManyMs('I like M&M')          

%
% ans =

%     0

%

% >> howManyMs('I like m&m')

%

% ans =

%     2

 

result = 0;

index = 1;

while (index <= length(str))

   if (str(index) == 'm')

      result = result +1;

   end % if

   index = index+1;

end %while

 

return;

end % function

 

A4.

 

function result = howManyMs_for(str)

%  inputs:

%     str: the string that we are searching the m's

%  outputs:

%     result: number of m's in the string

%  Example:

% >> howManyMs('how let the dogs out?')

%

% ans =

%   0

%

% >> howManyMs('I like M&M')          

%
% ans =

%     0

%

% >> howManyMs('I like m&m')

%

% ans =

%     2

 

result = 0;

 

for index = [1: length(str)]

   if (str(index) == 'm')

      result = result +1;

   end % if

   index = index+1;

end % for

 

return;

end % function

 

A5. The transpose of the matrix v is printed to the screen. That is, the output on the screen will be:

 

10 40 70

20 50 80

30 60 90

File Operations

A1.

% open both files, one file to read the other one to write

fromFile = fopen('input.txt', 'r');

toFile  = fopen('output.txt', 'w');

 

% read one line from the input file

str = fgets(fromFile);

 

% if the input file is empty or there is no line left to read, fgets returns

% the EOF (i.e., -1).

while (str ~= -1)

 

   % write the line into the output file

   fprintf(toFile, '%s', str);

     

   % read the next line from the input file

   str = fgets(fromFile);

 

end % while

 

% since you are done with the files, now close both files

fclose(fromFile);

fclose(toFile);

SPLITing a string using  regexp built-in function

A1. Note that, after spliting the given string into sub-strings (using the given delimiter), regexp returns all the sub-strings in a cell array. That is why the expression

 

>> regexp('my team is: galatasaray', ':' , 'split')

 

returns  (b) 1x2 cell array.

 

A2.

 

>> regexp(mystr, ',' , 'split')

 

The expresion above would return the answer below:

 

>> regexp(mystr, ',' , 'split') 

 

ans =

 

    'eagles'    ' 70000'    ' 5 '    ' 1'

Script M-files, Function M-files, Test scripts

Chmod Unix Command

A1. Any of the Unix commands below would work  

 

> chmod g+w copyFromFile.m

> chmod g=w copyFromFile.m

> chmod 620 copyFromFile.m

 

A2.  Gives read and write permissions to the file copyFromFile.m