7.6
//using three functions
//first one to fill the array ,second one to show the array
//last one to reverse the array
#include <iostream>
using namespace std;
int Fill_array(double*,int);
void Show_array(const double*,int);
void Reverse_array(double*,int);
const int Size=10;
int main()
{
double arr[Size];
int count;
count=Fill_array(arr,Size);
Show_array(arr,count);
Reverse_array(arr,count);
Show_array(arr,count);
return 0;//dd
}
int Fill_array(double* arr,int size)
{
int i=0;
cout<<"Please enter elements: "<<endl;
for(i;i<size;i++)
{
cout<<"# "<<i+1<<": ";
if(cin>>arr[i])
continue;
else
break;
}
return i;
}
void Show_array(const double* arr,int count)
{
cout<<"\nHere are the elements!\n";
for(int i=0;i<count;i++)
cout<<"arr["<<i<<"]= "<<arr[i]<<endl;
}
void Reverse_array(double* arr,int count)
{
double arr2;
for(int i=1;i<count/2;i++)
{
arr2=arr[i];
arr[i]=arr[count-i-1];
arr[count-i-1]=arr2;
}
}
7.7
// arrfun3.cpp -- array functions and const
#include <iostream>
const int Max = 5;
// function prototypes
double* fill_array(double ar[], int limit);
void show_array(const double*, const double*); // don't change data
void revalue(double r, double*, double*);
int main()
{
using namespace std;
double properties[Max];
double* end = fill_array(properties, Max);
show_array(properties, end);
if ((end-properties) > 0)
{
cout << "Enter revaluation factor: ";
double factor;
while (!(cin >> factor)) // bad input
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; Please enter a number: ";
}
revalue(factor, properties, end);
show_array(properties, end);
}
cout << "Done.\n";
// cin.get();
// cin.get();
return 0;//dd
}
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];
}
// the following function can use, but not alter,
// the array whose address is ar
void show_array(const double ar[], const double end[])
{
using namespace std;
const double* pt;
int i=0;
for (pt=ar,i; pt!=end; pt++,i++)
{
cout << "Property #" << (i+1) << ": $";
cout << *pt << endl;
}
}
// multiplies each element of ar[] by r
void revalue(double r, double ar[], double end[])
{
for (double* pt=ar; pt !=end; pt++)
*pt *= r;
}
7.8a
//adapt the 7.15 arrobj.cpp
#include <iostream>
#include <string>
using namespace std;
const int Seasons=4;
const char* Snames[Seasons]={"Spring","Summer","Fall","Winter"};
void Fill(double*);
void Show(double*);
int main()
{
double expenses[Seasons];
Fill(expenses);
Show(expenses);
return 0;//dd
}
void Fill(double ar[])
{
for(int i=0;i<Seasons;i++)
{
cout<<"Enter "<<Snames[i]<<" expenses: ";
cin>>ar[i];
}
}
void Show(double arr[])
{
double total=0.0;
cout<<"\nEXPENSES\n";
for(int i=0;i<Seasons;i++)
{
cout<<Snames[i]<<": $"<<arr[i]<<endl;
total+=arr[i];
}
cout<<"Total Expenses: $"<<total<<endl;
}
7.8b
//adapt the 7.15 arrobj.cpp
#include <iostream>
#include <string>
using namespace std;
const int Seasons=4;
const char* Snames[Seasons]={"Spring","Summer","Fall","Winter"};
struct Expense
{
double expenses[Seasons];
};
void Fill(Expense*);
void Show(Expense*);
int main()
{
Expense expenses;
Fill(&expenses);
Show(&expenses);
return 0;//dd
}
void Fill(Expense* ar)
{
for(int i=0;i<Seasons;i++)
{
cout<<"Enter "<<Snames[i]<<" expenses: ";
cin>>ar->expenses[i];
}
}
void Show(Expense* arr)
{
double total=0.0;
cout<<"\nEXPENSES\n";
for(int i=0;i<Seasons;i++)
{
cout<<Snames[i]<<": $"<<arr->expenses[i]<<endl;
total+=arr->expenses[i];
}
cout<<"Total Expenses: $"<<total<<endl;
}
7.9
#include <iostream>
using namespace std;
const int SLEN=30;
struct student{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[],int n)
{
int i=0;
for(i;i<n;i++)
{
cout<<"Enter # "<<i+1<<" student's name.\n";
cin.getline(pa[i].fullname,SLEN);
if(pa[i].fullname!="")
{
cout<<"Enter # "<<i+1<<" student's hobby\n";
cin.getline(pa[i].hobby,SLEN);
cout<<"Enter # "<<i+1<<" student's opplevel\n";
(cin>>pa[i].ooplevel).get();
}
else
break;
}
return i;
}
//getinfo() has two arguments:a pointer to the first element of
//an array of student sturctures and an int representing the
//number of elements of the array.The function solicits and
//stores data about students.It terminates input upon filling
//the array or upon encountering a blank line for the student
//name.The function returns the actual number of array elements
//filled.
void display1(student st)
{
cout<<"Fullname: "<<st.fullname<<endl;
cout<<"Hobby: "<<st.hobby<<endl;
cout<<"ooplevel: "<<st.ooplevel<<endl;
//display1() takes a student structures as an argument
} //and displays its contents
void display2(const student* ps)
{
cout<<"Fullname: "<<ps->fullname<<endl;
cout<<"Hobby: "<<ps->hobby<<endl;
cout<<"ooplevel: "<<ps->ooplevel<<endl;
} //display2() takes the address of students structure as an
//argument and displays the sturture's contents
void display3(const student pa[],int n)
{
for(int i=0;i<n;i++)
{
cout<<"Fullname: "<<pa[i].fullname<<endl;
cout<<"Hobby: "<<pa[i].hobby<<endl;
cout<<"ooplevel: "<<pa[i].ooplevel<<endl<<endl;
}
}
//display3() takes the address of the first element of an array
//of student structures and the numbers of array elements as
//arguments and displays the contents of the structures
int main()
{
cout<<"Enter class size: ";
int class_size;
cin>>class_size;
while (cin.get()!='\n')
continue;
student *ptr_stu = new student[class_size];
int entered = getinfo(ptr_stu,class_size);
for (int i = 0;i < entered; i++)
{
display1(ptr_stu[i]);
cout<<endl;//for distinct can delete
display2(&ptr_stu[i]);
}
display3(ptr_stu,entered);
delete [] ptr_stu;
cout <<"Done\n";
return 0;//dd
}
7.10
//according to the topic finish the functions
#include <iostream>
using namespace std;
double add(double,double);
double mean(double,double);
double differ(double,double);
double calculate(double,double,double (*pf)(double,double));
int main()
{
double x,y;
cout<<"Enter two numbers(q to quit): ";
while(cin>>x>>y)
{
cout<<"Sum = "<<calculate(x,y,add)<<endl;
cout<<"Average = "<<calculate(x,y,mean)<<endl;
cout<<"Difference = "<<calculate(x,y,differ)<<endl;
double (*pf[3])(double,double)={add,mean,differ};//using pointer array
char* pt[3]={"Sum","Average","Difference"};
for(int i=0;i<3;i++)
{
cout<<pt[i]<<" = "<<calculate(x,y,pf[i])<<endl;
}
cout<<"Enter two numbers: ";
}
cout<<"Bye!!";//dd
return 0;
}
double add(double x,double y)
{
return x+y;
}
double mean(double x,double y)
{
return (x+y)/2;
}
double differ(double x,double y)
{
return x-y;
}
double calculate(double x,double y,double (*pf)(double,double))
{
return (*pf)(x,y);
}