Advertisement
Guest User

2-array-passing

a guest
Feb 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. void readN(int &m);
  7. int WriteSequence(int m);
  8. int CountToArray(int A[],int m);
  9. void WriteArray(int A[],int e1);
  10.  
  11. int main()
  12. {
  13. int n;
  14. readN(n);
  15. int cnt=0;
  16. cnt=WriteSequence(n);
  17. cout<<" -> "<<" in the sequence we have "<<cnt<<" numbers."<<endl;
  18.  
  19. int X[100];
  20. cnt=CountToArray(X,n);
  21. WriteArray(X,cnt);
  22.  
  23.  
  24.  
  25.  
  26. return 0;
  27. }
  28.  
  29. void readN(int &m)
  30. {
  31. cin>>m;
  32. }
  33.  
  34. bool even(int what)
  35. {
  36. return ((what%2)==0);
  37. }
  38.  
  39.  
  40. int WriteSequence(int m)
  41. {
  42. int j=0, i=m;
  43. cout<<"Sequence: ";
  44.  
  45. while(i>1)
  46. {
  47. if(even(i))
  48. {i=i/2;}
  49.  
  50. else {i=i*3+1;}
  51.  
  52. j++;
  53. cout<<"->"<<i;
  54.  
  55. }
  56.  
  57.  
  58. return j;
  59.  
  60. }
  61.  
  62. int CountToArray(int A[],int m)
  63. {
  64. int j=0, i=m;
  65. cout<<"Sequence: ";
  66.  
  67. while((i>1)&&(j<100))
  68. {
  69. if(even(i))
  70. {
  71. i=i/2;
  72.  
  73.  
  74. }
  75.  
  76. else {i=i*3+1;}
  77. A[j]=i;
  78.  
  79. j++;
  80.  
  81.  
  82. }
  83.  
  84.  
  85. return j;
  86.  
  87.  
  88. }
  89.  
  90. void WriteArray(int A[],int e1)
  91. {
  92. cout<<"array: ";
  93. for(int i=0;i<e1;i++)
  94. {
  95. cout<< " -> "<<A[i];
  96. }
  97.  
  98. cout<<endl;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement