Advertisement
Guest User

main.cpp

a guest
Feb 19th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. int main()
  2. {
  3. // CONSTANTS
  4. const int COL = 2;
  5.  
  6. // Variable Declaration
  7. int numElements1D;
  8. int numElements2D;
  9. int* ptr1D;
  10. int** ptr2D;
  11.  
  12. cout << "Enter number of elements for 1D array: ";
  13. cin >> numElements1D;
  14. cin.clear();
  15. cin.ignore(10000, '\n');
  16.  
  17. if (numElements1D <= 0)
  18. ptr1D = new int[1];
  19. else
  20. ptr1D = new int[numElements1D];
  21.  
  22. // Fill 1D array
  23. fill1dArray(ptr1D, numElements1D);
  24. cout << endl;
  25.  
  26. //2D ARRAY
  27. cout << "Enter number of elements for 2D array: ";
  28. cin >> numElements2D;
  29. cin.clear();
  30. cin.ignore(10000, '\n');
  31.  
  32. // Calc number of rows based on number of elements
  33. if (numElements2D <= 0)
  34. numElements2D = 1;
  35. else if (numElements2D % 2 == 0)
  36. numElements2D /= 2;
  37. else
  38. {
  39. numElements2D++;
  40. numElements2D /= 2;
  41. }
  42.  
  43. //Creates the row of the 2D array
  44. ptr2D = new int*[numElements2D];
  45.  
  46. // Creates the columns of the array
  47. for (int row = 0; row < numElements2D; row++)
  48. ptr2D[row] = new int[COL];
  49.  
  50. // Nested Loop to insert numbers into 2D array
  51. fill2dArray(ptr2D, numElements2D);
  52.  
  53. print1D(ptr1D, numElements1D);
  54. print2D(ptr2D, numElements2D);
  55.  
  56. delete[] ptr1D;
  57. delete[] ptr2D;
  58.  
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement