My Array class

Source: Internet
Author: User


  1. *copyright (c) 2016, College of Computer and Control engineering, Yantai University
  2. *all rights reserved
  3. * File name: 123.cpp
  4. * Author: Zong Tao
  5. * Completion Date: May 10, 2016
  6. * Version number: v1.0
  7.  *
  8. * Problem Description: Read program, complete the definition of member function
  9. * Input Description:
  10. * Program output:
  11. */   
  12. #include <iostream>   
  13. using   namespace std;
  14. class MyArray
  15. {
  16. Private :  
  17. int *arrayaddr; //Save the first address of an array with Len integer elements   
  18. int       Len; //record the length of the dynamic array
  19. int       Max; The maximum value in the//dynamic array (not the data member that must be in the dynamic array)
  20. Public :  
  21. MyArray (int *a, int n);
  22. ~myarray ();
  23. int   getValue (int i); //Get the value of the element labeled I in the array
  24. int          Getlen (); //return array length
  25. int         Getmax (); //Returns the maximum value in the array
  26. };
  27. Myarray::myarray (int *a, int N)
  28. {
  29. Len=n;
  30. arrayaddr=New int[Len];
  31. int  i;
  32. MAX=A[0];
  33. for (i=0;i<n;i++)
  34. {
  35. Arrayaddr[i]=a[i];
  36. if (Max<arrayaddr[i])
  37. Max=arrayaddr[i];
  38. }
  39. }
  40. Myarray::~myarray ()
  41. {
  42. Delete  [] arrayaddr;
  43. }
  44. int myarray::getvalue (int i) //Get the values in the array labeled I elements   
  45. {
  46. return  Arrayaddr[i];
  47. }
  48. int Myarray::getlen () //Returns the length of the array   
  49. {
  50. return  Len;
  51. }
  52. int Myarray::getmax () //Returns the largest value in the array   
  53. {
  54. return  Max;
  55. }
  56. int Main () {
  57. int  b[10]= {75, 99, 90, 93, 38, 15, 5, 7, 52, 4};
  58. MyArray R1 (b,10);
  59. cout<<"Max:"<<r1.getmax () <<endl;
  60. int  c[15] = {18,68,10,52,3,19,12,100,56,96,95,97,1,4,93};
  61. MyArray R2 (c,15);
  62. int  i,s=0;
  63. for (i=0; I<r2.getlen (); i++)
  64. S+=r2.getvalue (i);
  65. cout<<"and for all elements:"<<s<<endl;
  66. return  0;
  67. }
  68. Operation Result:
  69. <pre name= "code" class= "CPP" >/* *copyright (c) 2016, College of Computer and Control engineering, Yantai University *all rights reserved * File name: 123.cpp * Author: Wang Lui * finish date : April 24, 2016 * Version number: v1.0 * * Problem Description: Read the program, complete the definition of member function * Input Description: * Program output: */#include <iostream>using namespace Std;class myarray{ Private:int *arrayaddr;       Holds the first address of an array with len integer elements int len;       Record the length of the dynamic array int max;    The maximum value in the dynamic array (not the data member that must be in the dynamic array) public:myarray (int *a, int n);    ~myarray ();   int getValue (int i);          Gets the value of the element labeled I in the array int getlen ();         Returns the array length int getmax (); Returns the maximum value in the array};    Myarray::myarray (int *a, int n) {len=n;    Arrayaddr=new Int[len];    int i;    MAX=A[0];        for (i=0;i<n;i++) {arrayaddr[i]=a[i];    if (Max<arrayaddr[i]) max=arrayaddr[i]; }}myarray::~myarray () {delete [] arrayaddr;} int myarray::getvalue (int i)//Get the value in the array labeled I element {return arrayaddr[i];} int Myarray::getlen ()//returns the length of the array {return len;} int Myarray::getmax ()//returns the largest value in the array {return max;} int main () {intb[10]= {75, 99, 90, 93, 38, 15, 5, 7, 52, 4};    MyArray R1 (b,10);    cout<< "Max:" <<r1.getmax () <<endl;    int c[15] = {18,68,10,52,3,19,12,100,56,96,95,97,1,4,93};    MyArray R2 (c,15);    int i,s=0;    for (i=0; I<r2.getlen (); i++) S+=r2.getvalue (i);    cout<< "and for all elements:" <<s<<endl; return 0;} Operation Result:

My Array class

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.