/
Arrays_and_Pointers.cpp
118 lines (90 loc) · 3.24 KB
/
Arrays_and_Pointers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//============================================================================
// Name : Arrays_and_Pointers.cpp
// Author : HMS
// Version :
// Copyright : Your copyright notice
// Description :
/* Write a C++ function named apply_all that expects two arrays of integers and their sizes and
dynamically allocates a new array of integers whose size is the product of the 2 array sizes
The function should loop through the second array and multiplies each element across each element of array 1 and store the
product in the newly created array.
The function should return a pointer to the newly allocated array.
You can also write a print function that expects a pointer to an array of integers and its size and display the
elements in the array.
For example,
Below is the output from the following code which would be in main:
int array1[] {1,2,3,4,5};
int array2[] {10,20,30};
cout << "Array 1: " ;
print(array1,5);
cout << "Array 2: " ;
print(array2,3);
int *results = apply_all(array1, 5, array2, 3);
cout << "Result: " ;
print(results,15);
Output
---------------------
Array 1: [ 1 2 3 4 5 ]
Array 2: [ 10 20 30 ]
Result: [ 10 20 30 40 50 20 40 60 80 100 30 60 90 120 150 ]
*/
//============================================================================
#include <iostream>
//make const int const * as to prevent any changes to pointer or what it's pointing to so if
//any changes make, the error will bbe caught
int *apply_all(const int const *array1,size_t array1_size,const int const *array2,size_t array2_size);
void print(const int const *array1,size_t array1_size);
using namespace std;
int main() {
const size_t array1_size {5};
const size_t array2_size {3};
int array1[] {1,2,3,4,5};
int array2[] {10,20,30};
apply_all(array1, array1_size, array2, array2_size);
cout << "Array 1: " ;
print(array1,array1_size);
cout << "Array 2: " ;
print(array2,array2_size);
int *results = apply_all(array1, array1_size, array2, array2_size);
constexpr size_t results_size {array1_size * array2_size};
cout << "Result: " ;
print(results, results_size);
//delete array pointer
delete [] results;
cout << endl;
return 0;
}
int *apply_all(const int const *array1,size_t array1_size,const int const *array2,size_t array2_size){
/*
* dynamically allocates a new array then it loops through each element of arr2
* and multiples it across all the elements of arr1 and each product is stored
* in the newly created array
*/
int *result{nullptr};
auto size_calc{array1_size*array2_size};
//allocating new int size
result = new int[size_calc];
int arr_size{0};
for(size_t x=0; x < array2_size; x++){
for(size_t y=0; y < array1_size; y++){
/*get by dereferencing pointer*/
// result[arr_size] = (*(array1+y)) * (*(array2+x));
//use by address value
result[arr_size] = array1[y] * array2[x];
arr_size++;
}
}
return result;
}
void print(const int const *array1,size_t array1_size){
/*
* loops through array and display all the integers
*/
cout << "[ " ;
for(size_t i{0}; i < array1_size; i++){
/*get by dereferencing pointer*/
// cout << *(array1+i) << " ";
cout << array1[i] << " ";
}
cout << "]" << endl;
}