Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*This program will take in a number
- assign it to an array as its size, populate
- it with random numbers from 1000 to 1999.*/
- //Jeremy Logoteta
- #include "stdafx.h"
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int populateArray();
- void printArray(int *, int);
- void sortArray(int [], int);
- int main(){
- int *pointer;
- int size = 0;
- pointer = new int[size];
- cout << "Enter in the array size: ";
- cin >> size;
- for (int i = 0; i < size; i++)
- pointer[i] = populateArray();
- //prints the unsorted array
- cout << "\nThe unsorted array: ";
- printArray(pointer, size);
- //sorts the array
- sortArray(pointer, size);
- cout << "\nThe sorted array: ";
- //prints the sorted array
- printArray(pointer, size);
- //deletes the allocated memory
- //delete (&array[size]);
- return 0;
- }
- /*
- The populateArray function populates the array with random numbers
- Pre Conditions:An int array of size to be determined by the user
- is passed in
- Post Conditions:The array is returned populated by the random
- numbers*/
- int populateArray(){
- int y;
- int seed;
- int *temp;
- seed = time(0);
- srand(seed);
- y = 1000 + (rand () % 1999);
- *temp = y;
- return *temp;
- }
- /*
- The pringArray function prints the array
- Pre Conditions:An int pointer to an array of size to be determined by the user
- is passed in
- Post Conditions:The array is printed*/
- void printArray(int *pointer, int size){
- for (int i = 0; i < size; i++){
- cout << pointer[i] << " ";
- cout << "\n";
- }
- }
- /*
- The sortArray function sorts the array
- Pre Conditions:An int array of size to be determined by the user
- is passed in
- Post Conditions:The array is sorted*/
- void sortArray(int array[], int size){
- int startScan = 0, minIndex;
- int minValue = array[0];
- for (int count = startScan + 1; count < size; count++){
- minValue = array[count];
- minIndex = count;
- }
- array[minIndex] = array[startScan];
- array[startScan] = minValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement