-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_basic.cpp
More file actions
157 lines (145 loc) · 5.45 KB
/
array_basic.cpp
File metadata and controls
157 lines (145 loc) · 5.45 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Purpose: basic algorithms for arrays
* Author: Emanuele Rizzolo
* Class: 3XIN
* Date: 2022/11/23
* Note:
*/
#include <iostream>
using namespace std;
/**
* Come già detto, gli array si elaborano elemento per elemento.
* Vi sono degli schemi di base che si raccomandano per i compiti più tipici.
* In questo esempio si mostrano alcuni di tali schemi.
* La base di tutto sono le scansioni degli elementi (o di parte di essi),
* che possono avvenire in ordine di indice crescente o decrescente.
* Durante la scansione, si accede al singolo elemento e lo si elabora
* secondo le necessità.
* La scansione può eventualmente essere interrotta se si raggiunge l'obiettivo.
* Le varianti sono ovviamente infinite.
* Gli esempi qui illustrati valgono per array di dati di tipo generico type.
* E' possibile sostituire a type qualunque tipo fisico (compatibile con le
* operazioni effettuate) modificando l'istruzione using seguente.
*/
// modificabile per usare altri tipi di dato (compatibili con i valori usati!!!)
// definisco type come "sinonimo" di double
using type = double;
// basic scan prototypes
void forwardScanAll(type a[], int end); // scansione crescente da 0 a end escluso: [0, end)
void backwardScanAll(type a[], int end); // scansione decrescente da end escluso a 0: [0, end)
// basic scan prototypes with default parameter begin
void forwardScan(type a[], int end, int begin = 0); // scansione crescente da begin a end escluso: [begin, end)
void backwardScan(type a[], int end, int begin = 0); // scansione decrescente da end escluso a begin: [begin, end)
// applicazione delle precedenti scansioni per la visualizzazione di un array
void JSON(const type a[], int end, int begin = 0, ostream &out = cout); // output su out in formato JSON
#define DEBUG 1
// main function
int main(int argc, char *argv[])
{
// error: narrowing conversion of '375' from 'int' to 'type' {aka 'char'} inside { } [-Wnarrowing]|
// type v[]{1, 5, 9, 2, 375};
type v[]{1, 5, 9, 2, 75};
constexpr int dim = sizeof(v) / sizeof(v[0]);
cout << "Forward scan of all elements" << endl;
forwardScanAll(v, dim);
cout << "backward scan of all elements" << endl;
backwardScanAll(v, dim);
cout << "Forward scan of first three elements" << endl;
forwardScanAll(v, 3);
cout << "backward scan of first three elements" << endl;
backwardScanAll(v, 3);
cout << "Forward scan of all elements but the first 2" << endl;
forwardScan(v, dim, 2);
cout << "backward scan of all elements but the first 2" << endl;
backwardScan(v, dim, 2);
cout << "Forward scan of all elements" << endl;
forwardScan(v, dim);
cout << "backward scan of all elements" << endl;
backwardScan(v, dim);
cout << "Output in JSON format of all elements" << endl;
JSON(v, dim);
cout << endl;
cout << "Output in JSON format of all elements but the first 2" << endl;
JSON(v, dim, 2);
cout << endl;
cout << "Output in JSON format of all elements in [2,2)" << endl;
JSON(v, 2, 2, cerr);
// successful termination
return 0;
}
/**
* Basic forward scan
* scansione crescente da 0 a end escluso: [0, end)
* @param a the array to scan
* @param end fine (esclusa) della scansione: a[end] non viene elaborato
*/
void forwardScanAll(type a[], int end)
{
for (int i = 0; i < end; ++i)
{
// process a[i]
cout << "processing a[" << i << "]" << endl;
}
}
/**
* Basic backward scan (with default limits)
* scansione decrescente da end escluso a 0: [0, end)
* @param a the array to scan
* @param end inizio (escluso) della scansione: a[end] non viene elaborato
*/
void backwardScanAll(type a[], int end)
{
for (int i = end; i-- > 0;)
{
// process a[i]
cout << "processing a[" << i << "]" << endl;
}
}
/**
* Basic forward scan (with default limits)
* scansione crescente da begin a end escluso: [begin, end)
* @param a the array to scan
* @param end fine (esclusa) della scansione: a[end] non viene elaborato
* @param begin inizio (incluso) della scansione: a[begin] e' il primo elemento elaborato
*/
void forwardScan(type a[], int end, int begin /* = 0 */)
{
for (int i = begin; i < end; ++i)
{
// process a[i]
cout << "processing a[" << i << "]" << endl;
}
}
/**
* Basic backward scan (with default limits)
* scansione decrescente da end escluso a begin: [begin, end)
* @param a the array to scan
* @param end inizio (escluso) della scansione: a[end] non viene elaborato
* @param begin fine (inclusa) della scansione: a[begin] e' l'ultimo elemento elaborato
*/
void backwardScan(type a[], int end, int begin /* = 0 */)
{
for (int i = end; i-- > begin;)
{
// process a[i]
cout << "processing a[" << i << "]" << endl;
}
}
/**
* Output su out in formato JSON da begin a end escluso: [begin, end)
* @param a the array to output
* @param end fine (esclusa) della scansione: a[end] non viene elaborato
* @param begin inizio (incluso) della scansione: a[begin] e' il primo elemento elaborato
* @param out the output stream to be used
*/
void JSON(const type a[], int end, int begin /* = 0 */, ostream &out /* = cout */)
{
out << "[ ";
for (int i = begin; i < end; ++i)
{
if (i != begin)
out << ", ";
out << a[i];
}
out << " ]";
}