C ++ Primer Plus Chapter 1 question solution, primer Chapter 2

Source: Internet
Author: User

C ++ Primer Plus Chapter 1 question solution, primer Chapter 2

Chapter C ++ Primer Plus

# Include
 
  
Using namespace std; double fun (double x, double y); int main () {cout <"enter two numbers:"; double a, B; while (cin> a> B & a & B) {cout <fun (a, B) <endl; cout <"enter two numbers: ";} cout <" BYE! "<Endl; return 0;} double fun (double x, double y) {return 2.0 * x * y/(x + y );}
 

7-2

#include 
 
    using namespace std;int input(double *a);void output(double *a, int n);double fun(double *a, int n);int main(){double a[10];int num = input(a);output(a, num);return 0;}int input(double *a){int i;for (i = 0; i < 10; ++i){cin >> a[i];if (a[i] <= 0)break;}return i;}void output(double *a, int n){for (int i = 0; i < n; ++i)cout << a[i] << " ";cout << fun(a, n) << endl;}double fun(double *a, int n){double sum = 0;for (int i = 0; i < n; ++i)sum += a[i];sum /= n;return sum;}
 

7-3

#include 
 
    using namespace std;struct box{char maker[40];float height;float width;float length;float volume;};void fun_a(box a);void fun_b(box *a);int main(){box a = {"Yuuji",10,20,1,22};fun_a(a);fun_b(&a);return 0;}void fun_a(box a){cout << a.maker << endl << a.height << endl << a.width << endl << a.length << endl << a.volume << endl;}void fun_b(box *a){a->volume = a->height * a->width * a->length;cout << a->volume << endl;}
 

7-4

#include 
 
    using namespace std;int main(){double ans = 1;for (int i = 0; i < 5; i++)ans *= (double)(5 - i) / (double)(47 - i);ans *= double(1) / 27;cout << ans << endl;return 0;}
 

7-5

#include 
 
    using namespace std;  long long fun(int n);  int main()  {      int n;      while(cin >> n && n >= 0){          long long ans = 0;          if(!n)              cout << "1" << endl;          else{              ans = fun(n);              cout << ans << endl;          }      }      return 0;  }    long long fun(int n)  {      long long ans = 1;      for( ; n ; --n)          ans *= n;      return ans;  }  
 

7-6

#include 
 
    using namespace std;  int File_array(double *a , int len);  void Show_array(double *a , int len);  void Reverse_array(double *a , int len);  int main()  {      double a[10];      int num = File_array(a , 10);      Show_array(a , num);      Reverse_array(a , num);      Show_array(a , num);      Reverse_array(a + 1 , num - 2);      Show_array(a , num);      return 0;  }    int File_array(double *a , int len)  {      int i;      for(i = 0 ; i < len ; ++ i)          cin >> a[i];      return i;  }    void Show_array(double *a , int len)  {      for(int i = 0 ; i < len ; ++ i)          cout << a[i] << " ";      cout << endl;  }    void Reverse_array(double *a , int len)  {      double temp;      for(int i = 0 ; i < len / 2 ; ++ i)    {          temp = a[i];          a[i] = a[len-1-i];          a[len-1-i] = temp;      }  }  
 

7-7

#include 
 
    using namespace std;double *fill_array(double *a);void show_array(double *a, double *b);void revalue(double r, double *a, double *b);int main(){double a[5];double *e = fill_array(a);show_array(a, e);revalue(0.5, a, e);show_array(a, e);return 0;}double *fill_array(double *a){int i = 0;while (cin >> a[i++])if (i == 5)break;return &a[i];}void show_array(double *a, double *b){while (a != b){cout << *a << " ";++a;}cout << endl;}void revalue(double r, double *a, double *b){while (a != b){(*a) *= r;++a;}}
 

7-8

A

# Include
 
  
Using namespace std; void fill_array (double * a, int len); void show_array (double * a, int len, const char ** p); // char * p is a string, the string array is char ** pint main () {const char * p [4] = {"Spring", "Summer", "Fall", "Winter "}; double a [4]; fill_array (a, 4); show_array (a, 4, p); return 0;} void fill_array (double * a, int len) {for (int I = 0; I <len; ++ I) cin> a [I];} void show_array (double * a, int len, const char ** p) {for (int I = 0; I <len; ++ I) cout <* (p + I) <"\ t "; cout <endl; for (int I = 0; I <len; ++ I) cout <a [I] <"\ t"; cout <endl ;}
 

B

#include 
 
    using namespace std;  struct money{      double a[4];  };  void fill_array(money *m , int len);  void show_array(money *m , int len , char **p);  int main()  {      money t;      char *p[4] = {"Spring" , "Summer" ,  "Fall"  , "Winter"};      fill_array(&t , 4);      show_array(&t , 4 , p);      return 0;  }    void fill_array(money *m , int len)  {      for(int i = 0 ; i < len ;  ++ i)          cin >> m->a[i];  }    void show_array(money *m , int len , char **p)  {      for(int i = 0 ; i < len ; ++ i)          cout << *(p + i) << "\t";      cout << endl;      for(int i = 0 ; i < len ; ++ i)          cout << m->a[i] << "\t";      cout << endl;  }  
 

7-9

It's too troublesome to write again when you are free.

 

7-10

    #include 
 
              double calculate(double a, double b, double (*pf)(double a, double b));      double add(double a, double b);      double max(double a, double b);      double min(double a, double b);            int main()      {          double a, b;          double (*pf[3])(double a, double b);          pf[0] = add;          pf[1] = max;          pf[2] = min;          while(std::cin >> a >> b)              for(int i = 0 ; i < 3 ; ++i)                  std::cout << (*pf[i])(a, b) << std::endl;          return 0;      }            double calculate(double a, double b, double (*pf)(double a, double b))      {          return (*pf)(a, b);      }            double add(double a, double b)      {          return a + b;      }            double max(double a, double b)      {          return a > b ? a : b;      }            double min(double a, double b)      {          return a < b? a : b;      }  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.