forked from noahc66260/C-PrimerPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpe7-7.cpp
68 lines (62 loc) · 1.47 KB
/
pe7-7.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
// pe7-7.cpp -- a modified version of arrfun3 (listing 7.7)
// This is exercise 7 of chapter 7 in C++ Primer Plus by Stephen Prata
#include<iostream>
const int Max = 5;
// function prototypes
double * fill_array(double * ar, int limit);
void show_array(const double * ar, double * end); // don't change data
void revalue(double r, double * ar, double * end);
int main(void)
{
using namespace std;
double properties[Max];
double * end = fill_array(properties, Max);
show_array(properties, end);
cout << "Enter revaluation factor: ";
double factor;
cin >> factor;
revalue(factor, properties, end);
show_array(properties, end);
cout << "Done.\n";
return 0;
}
double * fill_array(double * ar, int limit)
{
using namespace std;
double temp;
int i;
for (i = 0; i < limit; i++)
{
cout << "Enter value #" << (i + 1) << ": ";
cin >> temp;
if (!cin) // bad input
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated.\n";
break;
}
else if (temp < 0) // signal to terminate
break;
*(ar + i) = temp;
}
return (ar + i);
}
void show_array(const double * ar, double * end)
{
using namespace std;
const double * temp = ar;
for (int i = 0; temp < end; i++, temp++)
{
cout << "Property #" << (i + 1) << ": $";
cout << *temp << endl;
}
}
// multiplies each element of ar[] by r
void revalue(double r, double * ar, double * end)
{
double * temp = ar;
for (int i = 0; temp < end; i++, temp++)
*temp *= r;
}