Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Fundamentals of Programming

UMA-LCC
problems on a r r a y < >
prof. Dr. Juan Falgueras

1 Suppose (N is a previous declared constant) to interchange the contents of the two variables a and
typedef array<int,N> TVec; b, build the procedure
Build the next subprograms: void reversarr ( TVector & a ) ;
(a) void printArr(TVec a); to print it that reverses the contents of the array a
(b) void readArr(TVec& a); to read it from the user
10 Build a function to return the index in which the pair of
keyboard
numbers 1, 2, appear consecutively in an array TVector
(c) TVec readArr(); to read it from the user keyboard a. For example, in [−2, 3, 1, 2, 0, 1, 2] it would return:
but as a function that returns the local TVec array 2. If they did not appear return −1
returned
NOTA: Be careful accessing the components of the array,
From the main program, try them all declaring a TVec not reading values out of the range of it.
a;, then printArr(a);, to see the values an uninitialize 11 Let’s suppose we have the type typedef array<float
array has, then test readArr(a); and a = readArr(); ,3> TPos; an array to hold the cartesian coordinates
reading the vector in each form, modifying the parame- of a point in the 3D space. Given the positions of two
ter by reference and getting it as the returned value from points, r1 and r2, build a function to compute (and
a function, and again print the vector in each case. return) the distance between these 2 points. The dis-
~ ~
2 Given TVec from 1 build qP between 2 points ~a and b is defined as: d(~a, b) =
tance
3 2
(a) a subprogram void printrra(TVec a); to print i=1 (bi − ai )

it but reversed. As an example, in main() do 12 Build


printrra(readArr());, to read the array, return
void stats ( TVector a ,
it and send it to printrra() to see it reversed: float & mean , float & max ,
60 16 32 float & min , float & stdeviation ) ;
the program, then, will print: that from a TVector with N reals gives stdeviation,
32 16 60 the corrected standard deviation:
s
3 Build a new type THeights as an array of float to hold PN −1 2
up to 5 real numbers. Once declared, build a variable i=0 (ai − a)
σ=
alumHeights and save in it the 5 real values, as for N −1
example, 1.83 1.78 1.55 1.90 1.60. Then call the with a the average of the elements.
function: float media(THeights a); that returns the Observe that the previous equation requires two different
mean of that array. Check the given example gives 1.732 iterations over the array. One to compute the average
. and then another to compute the sum of the squared
differences. Try using it but, then try with this that is
4 Suppose we have two vectors
equivalent:
typedef array<float,N> TVector with N=3 named a s s
and b, build the scalar product of both. The scalar PN −1 2
PN −1 2 PN −1 2
i=0 (ai − a) i=0 ai − ( i=0 ai ) /N
product of tou vectors ~a and ~b is a real number. This σ= =
N −1 N −1
number is the sum of the product of the pairs ai · bi , that
PN −1
is: i=0 ai bi With this new formula for σ you won’t need to do two
but only one iteration over the array.
5 Build a function that returns the number of negative
numbers a TVec parameter has 13 Using the procedure in 12 compute the mean, maximum
and minimum, and σ of the mean temperatures (±) of
6 Build a function that returns the minimum value of a the latests November, 30th, from the last 100 years in
TVec received as parameter Málaga (up to 2005). Change the constant N in TVec to
100 and copy and use the next data:
7 Build a subprogram that yields the maximum and the
minimum of a TVec received as parameter TVec datos =
{{
9.3,15.2,12.3,12.8,12.5,14.9,15.8,12.2,10.6,14.1,14.5,14.5,
8 (Find) Build a function that returns the first position 13.3,14.4,11.4,11.3,13.5,12.1,12.2,12.0,12.1, 9.1,12.2, 9.8,
in which a value x is in an array TVector both received 8.7,14.1,13.0,15.6,13.4,12.7,11.7,14.3,11.7,14.5,10.4,11.2,
13.1,14.9,14.0,15.2,13.8, 9.4,15.4,14.4,19.7,15.4,17.3,16.5,
as parameters. If the value is not in the array, return 17.3,15.7,11.9,14.8,14.6,13.3,14.7,12.9,12.9,13.7,10.2,13.2,
−1 15.0,16.2,12.7, 7.4,13.6,12.4,14.2,19.5,13.3,12.8,14.9,10.7,
13.3,12.3,12.0,12.8,10.3,15.6,14.9,13.6,11.2,11.3,12.7,16.8,
int find ( TVector m , int x ) ; 9.1,12.0,17.7,15.5,15.8,15.3,15.2,15.9,11.1,16.7,13.6,12.9,
17.0,17.2,14.2,11.9
}};
9 Using the procedure
float av, max, min, std;
void swap ( int & a , int & b ) stats(datos, av, max, min, std);
cout << "Mean: " << av << endl; // Mean: 13.483
{ cout << "StdDev: " << std << endl; // StdDev: 2.29475
int t = a ; a = b ; b = t ; cout << "Max: " << max << endl; // Max: 19.7
} cout << "Min: " << min << endl; // Min: 7.4
2
14 Count of positives/sum of negatives Given an ar- it with the array of 20 elements:
ray of integers, build a subprogram to get the count of {{27, 84, -78, 77, -45, -50, 1, -11, 53, 44,
positives numbers and the sum of negative numbers. -18, 60, 42, 5, 77, 73, 29, 33, 100, -6}}

Print these two values in the main program after calling It should print: 14, -208

You might also like