array_return.cpp


SUBMITTED BY: davindran21

DATE: Oct. 12, 2016, 1:56 a.m.

FORMAT: Text only

SIZE: 3.6 kB

HITS: 3944

  1. # include <cstdlib>
  2. # include <iostream>
  3. # include <iomanip>
  4. using namespace std;
  5. int main ( );
  6. void make_arrays ( int m, int **a, int n, int **b );
  7. void i4vec_print ( int n, int a[], string title );
  8. //****************************************************************************80
  9. int main ( )
  10. //****************************************************************************80
  11. //
  12. // Purpose:
  13. //
  14. // MAIN is the main program for ARRAY_RETURN.
  15. //
  16. // Discussion:
  17. //
  18. // The correct form of this program was worked out with the somewhat
  19. // bemused assistance of Miro Stoyanov.
  20. //
  21. // Licensing:
  22. //
  23. // This code is distributed under the GNU LGPL license.
  24. //
  25. // Modified:
  26. //
  27. // 03 February 2014
  28. //
  29. // Author:
  30. //
  31. // John Burkardt
  32. //
  33. {
  34. int *a;
  35. int *b;
  36. int m;
  37. int n;
  38. cout << "\n";
  39. cout << "ARRAY_RETURN:\n";
  40. cout << " C++ version\n";
  41. cout << " Create two arrays in a function, \n";
  42. cout << " return them in the argument list.\n";
  43. //
  44. // Specify the size of the arrays to be created.
  45. //
  46. m = 10;
  47. n = 5;
  48. make_arrays ( m, &a, n, &b );
  49. //
  50. // Verify that the arrays were created and transferred properly.
  51. //
  52. i4vec_print ( m, a, " A as received by main:" );
  53. i4vec_print ( n, b, " B as received by main:" );
  54. //
  55. // Free memory.
  56. //
  57. delete [] a;
  58. delete [] b;
  59. //
  60. // Terminate.
  61. //
  62. cout << "\n";
  63. cout << "ARRAY_RETURN:\n";
  64. cout << " Normal end of execution.\n";
  65. return 0;
  66. }
  67. //****************************************************************************80
  68. void make_arrays ( int m, int **a, int n, int **b )
  69. //****************************************************************************80
  70. //
  71. // Purpose:
  72. //
  73. // MAKE_ARRAYS creates, sets, and returns two arrays using the argument list.
  74. //
  75. // Licensing:
  76. //
  77. // This code is distributed under the GNU LGPL license.
  78. //
  79. // Modified:
  80. //
  81. // 03 February 2014
  82. //
  83. // Author:
  84. //
  85. // John Burkardt
  86. //
  87. // Parameters:
  88. //
  89. // Input, int M, the desired size of the first array.
  90. //
  91. // Output, int **A, the first array.
  92. //
  93. // Input, int N, the desired size of the second array.
  94. //
  95. // Output, int **B, the second array.
  96. //
  97. {
  98. int i;
  99. //
  100. // We create PA and PB simply as a convenience.
  101. // They make the code a little more readable.
  102. //
  103. int *pa;
  104. int *pb;
  105. ( *a ) = new int[m];
  106. pa = *a;
  107. for ( i = 0; i < m; i++ )
  108. {
  109. pa[i] = 10 + i;
  110. }
  111. i4vec_print ( m, pa, " A as defined in MAKE_ARRAYS:" );
  112. ( *b ) = new int[n];
  113. pb = *b;
  114. for ( i = 0; i < n; i++ )
  115. {
  116. pb[i] = 100 + 2 * i;
  117. }
  118. i4vec_print ( n, pb, " B as defined in MAKE_ARRAYS:" );
  119. return;
  120. }
  121. //****************************************************************************80
  122. void i4vec_print ( int n, int a[], string title )
  123. //****************************************************************************80
  124. //
  125. // Purpose:
  126. //
  127. // I4VEC_PRINT prints an I4VEC.
  128. //
  129. // Discussion:
  130. //
  131. // An I4VEC is a vector of I4's.
  132. //
  133. // Licensing:
  134. //
  135. // This code is distributed under the GNU LGPL license.
  136. //
  137. // Modified:
  138. //
  139. // 14 November 2003
  140. //
  141. // Author:
  142. //
  143. // John Burkardt
  144. //
  145. // Parameters:
  146. //
  147. // Input, int N, the number of components of the vector.
  148. //
  149. // Input, int A[N], the vector to be printed.
  150. //
  151. // Input, string TITLE, a title.
  152. //
  153. {
  154. int i;
  155. cout << "\n";
  156. cout << title << "\n";
  157. cout << "\n";
  158. for ( i = 0; i < n; i++ )
  159. {
  160. cout << " " << setw(8) << i
  161. << ": " << setw(8) << a[i] << "\n";
  162. }
  163. return;
  164. }

comments powered by Disqus