Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. /*This program will take in a number
  2. assign it to an array as its size, populate
  3. it with random numbers from 1000 to 1999.*/
  4. //Jeremy Logoteta
  5.  
  6. #include "stdafx.h"
  7. #include <iostream>
  8. #include <iomanip>
  9. #include <cstdlib>
  10. #include <ctime>
  11.  
  12. using namespace std;
  13.  
  14. int populateArray();
  15. void printArray(int *, int);
  16. void sortArray(int [], int);
  17.  
  18. int main(){
  19. int *pointer;
  20. int size = 0;
  21. pointer = new int[size];
  22.  
  23. cout << "Enter in the array size: ";
  24. cin >> size;
  25.  
  26. for (int i = 0; i < size; i++)
  27. pointer[i] = populateArray();
  28.  
  29. //prints the unsorted array
  30. cout << "\nThe unsorted array: ";
  31. printArray(pointer, size);
  32.  
  33. //sorts the array
  34. sortArray(pointer, size);
  35. cout << "\nThe sorted array: ";
  36.  
  37. //prints the sorted array
  38. printArray(pointer, size);
  39.  
  40. //deletes the allocated memory
  41. //delete (&array[size]);
  42.  
  43. return 0;
  44. }
  45.  
  46. /*
  47. The populateArray function populates the array with random numbers
  48. Pre Conditions:An int array of size to be determined by the user
  49. is passed in
  50. Post Conditions:The array is returned populated by the random
  51. numbers*/
  52. int populateArray(){
  53. int y;
  54. int seed;
  55. int *temp;
  56. seed = time(0);
  57. srand(seed);
  58. y = 1000 + (rand () % 1999);
  59. *temp = y;
  60. return *temp;
  61. }
  62. /*
  63. The pringArray function prints the array
  64. Pre Conditions:An int pointer to an array of size to be determined by the user
  65. is passed in
  66. Post Conditions:The array is printed*/
  67. void printArray(int *pointer, int size){
  68. for (int i = 0; i < size; i++){
  69. cout << pointer[i] << " ";
  70. cout << "\n";
  71. }
  72. }
  73. /*
  74. The sortArray function sorts the array
  75. Pre Conditions:An int array of size to be determined by the user
  76. is passed in
  77. Post Conditions:The array is sorted*/
  78. void sortArray(int array[], int size){
  79. int startScan = 0, minIndex;
  80. int minValue = array[0];
  81. for (int count = startScan + 1; count < size; count++){
  82. minValue = array[count];
  83. minIndex = count;
  84. }
  85. array[minIndex] = array[startScan];
  86. array[startScan] = minValue;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement