Docsity

Programming - Second partial assessment 2018/19: triangle and supermarket (solved), Exámenes de Programación Informática

Toggle sidebar
bg1
UNIVERSIDAD CARLOS III DE MADRID
PROGRAMMING.
GRADE IN AEROSPACE ENGINEERING
SECOND
PARTIAL ASSESSMENT 2018-19. SECOND PART. EXERCISES.
Surname(s): ______________________________________
N
ame: _______________________________________
NIU: _____________________________________
Exercise 1 (4.0 points)
Write a MATLAB program to do the following:
1. Write a function to generate a matrix of N rows and M columns with random integer numbers in
a specific range. The values for N, M and the range of the random values will be pre-defined in the
main program. (0.5 points)
The function must have this format:
function matrix=generateMatrix(rows, cols, low, high);
2. Write a function to search for a specific value in the matrix. The function must return 1 if the value
exists in the matrix and the row and column where the value is, and 0 in any other case. (1 point)
The function must have this format:
function [r,c, exist] = searchValue(matrix,rows, cols,val);
3. Write a function to obtain the top-left triangle of side N from the matrix. The function must return
the elements of this triangle in a vector, and the number of elements of this vector. (1.25 points)
The function must have this format:
function [triangle, size] = obtainTriangle(matrix, side)
Ex:
N=3; M=4;
low = 1; high = 100;
matrix:
side = 3;
triangle:
pf3
pf4
pf5

Vista previa parcial del texto

¡Descarga Programming - Second partial assessment 2018/19: triangle and supermarket (solved) y más Exámenes en PDF de Programación Informática solo en Docsity!

UNIVERSIDAD CARLOS III DE MADRID

PROGRAMMING. GRADE IN AEROSPACE ENGINEERING

SECOND PARTIAL ASSESSMENT 2018-19. SECOND PART. EXERCISES.

Surname(s): ______________________________________

Name: _______________________________________

NIU: _____________________________________

Exercise 1 (4.0 points)

Write a MATLAB program to do the following:

1. Write a function to generate a matrix of N rows and M columns with random integer numbers in

a specific range. The values for N, M and the range of the random values will be pre-defined in the

main program. (0.5 points)

The function must have this format:

function matrix=generateMatrix(rows, cols, low, high);

2. Write a function to search for a specific value in the matrix. The function must return 1 if the value

exists in the matrix and the row and column where the value is, and 0 in any other case. (1 point)

The function must have this format:

function [r,c, exist] = searchValue(matrix,rows, cols,val);

3. Write a function to obtain the top-left triangle of side N from the matrix. The function must return

the elements of this triangle in a vector, and the number of elements of this vector. (1.25 points)

The function must have this format:

function [triangle, size] = obtainTriangle(matrix, side)

Ex:

N=3; M=4;

low = 1; high = 100;

matrix:

side = 3;

triangle:

4. Write a function to sort the triangle obtained in the previous point, using for that the Bubble Sort

Algorithm ( 0.5 points ):

The function must have this format:

function triangle = sort(triangle,size)

5. Write a main program to do (0.75 points):

∑ Define the values for the size of the matrix and the range for the values inserted in the

matrix

∑ Generate the matrix

∑ Ask for a valid value to the user and search this value in the matrix. Show on the screen if

the value exists or not, and if exists, the position of the matrix where the value is.

∑ Ask for the user to type a valid side for the triangle (must NOT be higher that the rows or

columns) and obtain the top-left triangle from the matrix for this side that the user has

typed

∑ Sort the triangle obtained in the previous point.

function [triangle, size] = obtainTriangle(matrix, side) size = 0; for (i=1:side) size = size + i; end triangle = zeros(1,size); el = 1; for (i=1:side) for (j=1:side+1-i) triangle(el) = matrix(i,j); el = el + 1; end end end

function triangle = sort(triangle,size) for i = 1:(size - 1) for j = 1:(size - i) % comparison index^th with (index + 1)^th if ( triangle(j) > triangle(j+1) ) % swap the content of the two positions a = triangle(j); triangle(j) = triangle(j+1); triangle(j+1) = a; end end end end

UNIVERSIDAD CARLOS III DE MADRID

PROGRAMMING. GRADE IN AEROSPACE ENGINEERING

SECOND PARTIAL ASSESSMENT 2018-19. SECOND PART. EXERCISES.

Surname(s): ______________________________________

Name: _______________________________________

NIU: _____________________________________

Exercise 2 (3.5 points)

A supermarket chain is beginning its implantation in Spain. To do this, it requires a MATLAB program

to manage the network of supermarkets and the internal operation of each of them. The chain wants

to install a total of 15 supermarkets in Spain. Each of them can be modeled using a data structure with

the following fields: a supermarket identifier (integer number between 1 and 15 that must be

automatically assigned by the program), name of the supermarket, accumulated sales during the year,

and a vector containing the 100 different products that are sold in each supermarket.

In turn, each product is modeled by a data structure containing the following fields: identifier (integer

between 1 and 100 that must be automatically assigned by the program), name of the product, and

current retail price.

A) Main program: Design the main program according to the following steps ( 1 point ):

∑ Define and preallocate the data structure to represent the information needed to

manage the network of supermarkets.

∑ Require the user the information corresponding to the network

of supermarkets (calling the function designed in section B).

∑ Ask the user to provide a product identifier and a maximum price, then print the name

of the supermarkets where the given product is sold with a price lower than the

maximum price (calling the function designed in section C).

B) Define a function that asks the user the data of the 15 supermarkets. The accumulated sales

of each supermarket must be initialized to 0. Each supermarket will be automatically assigned

with a different identifier in the range [1,15]. The identifier of each product must be also

assigned automatically following the order of introduction [1, 100]. The retail price of each

product must be completed with a random number in the range [1, 1000]. The rest of the fields

must be required to the user. The function must have the following prototype ( 1.2 points )

function supermarkets = readSuper (supermarkets)

C) Define a function to obtain the list of supermarkets that sell the product with identifier

productId with a price lower than PriceMax. The function must return an array with the

identifiers of the supermarkets that fulfill the described condition, according to the following

prototype: ( 1.3 points )

function cheapestSupermarkets = obtainCheapest(supermarkets,

productId, priceMax);

%Section C function cheapestSupermarkets = obtainCheapest(supermarkets, productId, priceMax) numSuper = 5; numProducts= 100;

cheapestSupermarkets = zeros(1,numSuper);

j=1; for i = 1:numSuper if ((supermarkets(i).products(productId).price)