#include<iostream>
using namespace std;
double average(int num1, int num2);
int main()
{
int num1, num2;
while ((cin >> num1 >> num2) && (num1 != 0) && (num2 != 0)) {
cout << average(num1, num2) << endl;
}
return 0;
}
double average(int num1, int num2) {
return 2.0 * num1 * num2 / (num1 + num2);
}
#include<iostream>
using namespace std;
const int MAX = 10;
int fill_golf(double arr[], int n);
void show_golf(double arr[], int n);
double average_golf(double arr[], int n);
int main()
{
double golf[MAX];
int size = fill_golf(golf, MAX);
if (size > 0) {
show_golf(golf, size);
cout << "Average golf: " << average_golf(golf, size) << endl;
}
else {
cout << "There is no scores!" << endl;
}
return 0;
}
int fill_golf(double arr[], int n)
{
double temp;
int i;
for (i = 0; i < n; i++) {
cout << "Enter golf score #" << i + 1 << ": ";
cin >> temp;
if (!cin) {
cin.clear();
cin.sync();
cout << "Invalid enter, terminate." << endl;
break;
}
else if (temp < 0)
break;
else
arr[i] = temp;
}
return i;
}
void show_golf(double arr[], int n)
{
cout << "Golf result: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
double average_golf(double arr[], int n)
{
double sum = 0.0, average = 0.0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
average = sum / n;
return average;
}
#include<iostream>
using namespace std;
struct box {
char maker[40];
float height;
float width;
float length;
float volume;
};
void show(box Box);
void setBox(box *Box);
int main()
{
box b = { "cube", 3, 4, 5 };
setBox(&b);
show(b);
return 0;
}
void show(box Box)
{
cout << "maker: " << Box.maker << endl;
cout << "height: " << Box.height << endl;
cout << "width: " << Box.width << endl;
cout << "length: " << Box.length << endl;
cout << "volume: " << Box.volume << endl;
}
void setBox(box *Box)
{
Box->volume = Box->height * Box->width * Box->length;
}
#include<iostream>
using namespace std;
long double probability(unsigned numbers, unsigned picks);
int main()
{
long double field = probability(47, 5);
long double special = probability(21, 1);
long double result = field * special;
cout << result << endl;
return 0;
}
long double probability(unsigned numbers, unsigned picks)
{
long double result = 1.0;
long double n;
unsigned p;
for (n = numbers, p = picks; p > 0; n--, p--) {
result = result * n / p;
}
return result;
}
#include<iostream>
using namespace std;
long double jiecheng(int n);
int main()
{
int n;
while (cin >> n) {
cout << n <<"!= " << jiecheng(n) << endl;
}
return 0;
}
long double jiecheng(int n)
{
if (n == 0 || n == 1)
return 1.0;
else
return n * jiecheng(n - 1);
}
#include<iostream>
#include<cmath>
using namespace std;
int Fill_array(double arr[], int n);
void Show_array(double arr[], int n);
void Reverse_array(double arr[], int n);
const int MAX = 10;
int main()
{
double num[MAX];
int size = Fill_array(num, MAX);
if (size > 0) {
Show_array(num, size);
Reverse_array(num, size);
Show_array(num, size);
Reverse_array(num + 1, size - 2);
Show_array(num, size);
}
else
cout << "There is no numbers." << endl;
return 0;
}
int Fill_array(double arr[], int n)
{
double temp;
int i;
for (i = 0; i < n; i++) {
cout << "Enter the number#" << i + 1 << ": ";
cin >> temp;
if (!cin) {
cin.clear();
cin.sync();
cout << "Failed to enter, terminate." << endl;
break;
}
else
arr[i] = temp;
}
return i;
}
void Show_array(const double arr[], int n)
{
cout << "The numbers of array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void Reverse_array(double arr[], int n)
{
for (int i = 0, j = n - 1; i <= j; i++, j--) {
swap(arr[i], arr[j]);
}
}
#include<iostream>
using namespace std;
double* Fill_array(double arr[], int n);
void Show_array(double arr[], double *p);
void revalue(double r, double arr[], double* p);
const int MAX = 5;
int main()
{
double num[MAX];
double *p = Fill_array(num, MAX);
Show_array(num, p);
if (p - num > 0) {
cout << "Enter revaluation factor: ";
double factor;
while (!(cin >> factor)) {
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; Please enter a number: ";
}
revalue(factor, num, p);
Show_array(num, p);
}
cout << "Done.\n";
cin.get();
cin.get();
return 0;
}
double* Fill_array(double arr[], int n)
{
double temp;
double* p = arr - 1;
for (int i = 0; i < n; i++) {
cout << "Enter the number#" << i + 1 << ": ";
cin >> temp;
if (!cin) {
cin.clear();
cin.sync();
cout << "Failed to enter, terminate." << endl;
break;
}
else
arr[i] = temp;
p++;
}
return p;
}
void Show_array(double arr[], double *p)
{
cout << "The numbers of array: ";
double* i = arr;
while (i != p) {
cout << *i << " ";
i++;
}
cout << *i;
cout << endl;
}
void revalue(double r, double arr[], double *p)
{
double* i = arr;
while (i != p) {
*i *= r;
i++;
}
*i *= r;
}
//a.
#include<iostream>
using namespace std;
const int Seasons = 4;
const char* Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
void fill(double (*pa)[Seasons]);
void show(double da[Seasons]);
int main()
{
double expenses[Seasons];
fill(&expenses);
show(expenses);
return 0;
}
void fill(double (*pa)[Seasons])
{
for (int i = 0; i < Seasons; i++) {
cout << "Enter " << Snames[i] << " expenses: ";
cin >> (*pa)[i];
}
}
void show(double da[Seasons])
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++) {
cout << Snames[i] << ": $" << da[i] << endl;
total += da[i];
}
cout << "Total expenses: $" << total << endl;
}
//b.
#include<iostream>
using namespace std;
const int Seasons = 4;
const char* Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
struct Spend {
double exp[Seasons];
};
void fill(double (*pa)[Seasons]);
void show(double da[Seasons]);
int main()
{
Spend expenses;
fill(&expenses.exp);
show(expenses.exp);
return 0;
}
void fill(double (*pa)[Seasons])
{
for (int i = 0; i < Seasons; i++) {
cout << "Enter " << Snames[i] << " expenses: ";
cin >> (*pa)[i];
}
}
void show(double da[Seasons])
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++) {
cout << Snames[i] << ": $" << da[i] << endl;
total += da[i];
}
cout << "Total expenses: $" << total << endl;
}
#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);
void display1(student st);
void display2(const student* ps);
void display3(const student pa[], int n);
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]);
display2(&ptr_stu[i]);
}
display3(ptr_stu, entered);
delete[]ptr_stu;
cout << "Done.\n";
return 0;
}
int getinfo(student pa[], int n)
{
int i;
cout << "Please input the students information:" << endl;
for (i = 0; i < n; i++) {
cout << "Student #" << i + 1 << ": " << endl;
cout << "Fullname: ";
cin.getline(pa[i].fullname, SLEN);
if (pa[i].fullname[0] == '\0')
break;
cout << "Hobby: ";
cin.getline(pa[i].hobby, SLEN);
cout << "Ooplevel: ";
cin >> pa[i].ooplevel;
if (!cin) {
cin.clear();
while (cin.get() != '\n')
continue;
break;
}
cin.get();
}
cout << "Enter End!" << endl;
return i;
}
void display1(student st)
{
cout << "Fullname: " << st.fullname << endl;
cout << "Hobby: " << st.hobby << endl;
cout << "Level: " << st.ooplevel << endl;
}
void display2(const student* ps)
{
cout << "Fullname: " << ps->fullname << endl;
cout << "Hobby: " << ps->hobby << endl;
cout << "Level: " << ps->ooplevel << endl;
}
void display3(const student pa[], int n)
{
for (int i = 0; i < n; i++) {
cout << "Student info#" << i + 1 << ": " << endl;
cout << "Fullname: " << pa[i].fullname << endl;
cout << "Hobby: " << pa[i].hobby << endl;
cout << "Level: " << pa[i].ooplevel << endl;
}
}
#include<iostream>
using namespace std;
double calculate(double, double, double (*fun)(double, double));
double add(double x, double y);
double subtract(double x, double y);
double multiply(double x, double y);
int main()
{
double n1, n2;
double (*pf[3])(double, double) = { add, subtract, multiply };
cout << "Enter two numbers(q to quit): ";
while (cin >> n1 >> n2) {
for (int i = 0; i < 3; i++) {
cout << "The result #" << i + 1 << ":" << calculate(n1, n2, pf[i]) << endl;
}
cout << "Enter two numbers(q to quit): ";
}
return 0;
}
double calculate(double num1, double num2, double (*fun)(double, double))
{
return (*fun)(num1, num2);
}
double add(double x, double y) {
return x + y;
}
double subtract(double x, double y)
{
return x - y;
}
double multiply(double x, double y)
{
return x * y;
}