Skip to content

Instantly share code, notes, and snippets.

@mahata
Created September 21, 2011 05:10
Show Gist options
  • Save mahata/1231299 to your computer and use it in GitHub Desktop.
Save mahata/1231299 to your computer and use it in GitHub Desktop.
C++ Primer 7.7
#include <iostream>
const int Max = 5;
// function prototypes
double * fill_array(double ar[], int limit);
void show_array(const double ar[], double * n);
void revalue(double r, double ar[], double * n);
int main()
{
using namespace std;
double properties[Max];
double * pointer = fill_array(properties, Max);
show_array(properties, pointer);
cout << "Enter revaluation factor: ";
double factor;
cin >> factor;
revalue(factor, properties, pointer);
show_array(properties, pointer);
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;
}
if (0 == i) {
exit(-1);
}
return &ar[i - 1];
}
// the following function can use, but not alter,
// the array whose address is ar
void show_array(const double ar[], double * n)
{
using namespace std;
for (int i = 0; ; i++) {
cout << "Property #" << (i + 1) << ": $";
cout << ar[i] << endl;
if (n == &ar[i]) { break; }
}
}
// multiplies each element of ar[] by r
void revalue(double r, double ar[], double * n)
{
for (int i = 0; ; i++) {
ar[i] *= r;
if (&ar[i] == n) { break; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment