Lab #10. Array processing in function
Lab #10.1.
Sum of array elements in range
Create and
test function array_sum() with 3 parameters: arr
(array of integers), num1 (index of first element), num2 (index
of the last element) to calculate sum of elements num1..num2 (lab10.1.cpp).
#include <iostream>
using namespace std;
int array_sum
// ADD CODE HERE
};
int main()
{
int
arr[] = {1,2,5,7,3,6};
cout
<< array_sum(arr,1,3) << endl;
return 0;
}
/*
Expected output:
14
*/
A correct
solution: lab10.1a.cpp.
Lab #10.2.
Copying the contents of an array to another array
Create and
test function array_copy() with 3 parameters: arr_dest
(destination array), arr_src (source array),
num (number of elements), which copies first num elements, from arr_src
to arr_dest (lab10.2.cpp). The
destination array already exists in memory.
#include <iostream>
using namespace std;
void array_copy
// ADD CODE HERE
};
int main()
{
int
arr1[] = {1,2,3,4,5,6};
int
arr2[] = {11,22,33,44,55,66};
array_copy (arr1,
arr2, 4);
for (int i=0; i<6;
i++) cout << arr1[i] << endl;
return 0;
}
/*
Expected output:
11
22
33
44
5
6
*/
A correct
solution: lab10.2a.cpp.
Lab #10.3.
Creating a dynamic array
Create function
arr_create() with 2 parameters: arr
(array), num (size), which creates new dynamic
array arr of size num
(lab10.3.cpp).
#include <iostream>
using namespace std;
void array_create
// ADD CODE HERE
};
int main()
{
int
num = 6;
int
*arr;
array_create (arr, num);
for (int i=0; i<num; i++) arr[i] = (i+1)*10+i+1;
for (int i=0; i<num; i++) cout
<< arr[i] << endl;
delete[] arr;
return 0;
}
/*
Expected output:
11
22
33
44
55
66
*/
A correct
solution: lab10.3a.cpp.
Lab #10.4.
Modification of the previous function of creating a dynamic array
Modify
function arr_create() to return created array via return instead of parameter (function
will have only one parameter num) (lab10.4.cpp).
#include <iostream>
using namespace std;
int *array_create
// ADD CODE HERE
};
int main()
{
int
num = 6;
int
*arr;
arr
= array_create (num);
for (int i=0; i<num; i++) arr[i] = (i+1)*10+i+1;
for (int i=0; i<num; i++) cout
<< arr[i] << endl;
delete[] arr;
return 0;
}
/*
Expected output:
11
22
33
44
55
66
*/
A correct
solution: lab10.4a.cpp.
Lab #10.5.
Resizing dynamic array
Create
function arr_recreate() with 3 parameters:
arr (array), num
(new size), num0 (previous size), which resizes the array to num, preserving all values possible, returning new
array through parameters arr. With this function, array can be either
increased or decreased in size (lab10.5.cpp).
Suggested scheme
of solution: create another array of size num, copy
all possible values to it, and delete the old array replacing it by the new
array.
#include <iostream>
using namespace std;
void array_recreate
// ADD CODE HERE
};
int main()
{
int
num = 4;
int
*arr;
array_recreate (arr, num, 0);
for (int i=0; i<num; i++) arr[i] = (i+1)*10+i+1;
for (int i=0; i<num; i++) cout
<< arr[i] << endl;
cout
<< endl;
num
= 6;
array_recreate (arr, num, 4);
arr[4] = 100;
arr[5] = 101;
for (int i=0; i<num; i++) cout
<< arr[i] << endl;
array_recreate (arr, 0, num);
return 0;
}
/*
Expected output:
11
22
33
44
11
22
33
44
100
101
*/
A correct
solution: lab10.5a.cpp.