1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#include <cstdlib>
using namespace std;
void insertionSort( int[], int, int );
int main()
{
const int size = 100000;
int temp, i, j, list[size] = {};
int *dptr;
dptr = &list[0];
int count = 0;
int n = 15;
int selection = 0;
do
{
cout << "\n1) Enter useable Array Size, default = 15, max = 100000\n";
cout << "2) Manually enter Array data for initial elements\n";
cout << "3) Book Sort current Array\n";
cout << "4) Display Array\n";
cout << "5) Insert final 5 elements of Array\n";
cout << "6) Have computer enter random numbers for you\n";
cout << "7) Have computer enter final 5 random numbers for you\n";
cout << "8) Find Interger and its location\n";
cout << "9) Exit\n";
cin >> selection;
if ( selection == 1 ) // get the list size, or array size from the user
{
cout<<"\nEnter the size of your Array: Min 5, Max 100000\n";
cin>>n;
// make sure user can not input array that is to small or to large
while( ( n < 5 ) || ( n > 100000 ) )
{
cout << "Try entering something between 5 and 100000\n";
cin >> n;
}//end while
}//end if selection 1
if ( selection == 2 ) // prompt for user to input initial 10 elements of array
{
for( i = 0; i < ( n - 5 ); i++)
{
cout<<"Enter list's element #"<<i<<"-->";
cin >> dptr[i];
}
}
if ( selection == 3 ) // Sort function call
{
insertionSort( dptr, n, count );// function call to sort
}//end selection 3
if ( selection == 4 ) // Display Array
{
for( int j = 0; j < ( n - 5 + count ) ; ++j)
{
if ( j < 10 )
{
//cout << "Element# " << j << " " << list[j] << "\n";
cout << "*( dptr + " << j << " ) = " << *( dptr + j ) << "\n";
}
else
//cout << "Element# " << j << " " << dptr[j] << endl;
cout << "*( dptr + " << j << " ) = " << *( dptr + j ) << "\n";
}//end for
}//end selection 4
if ( selection == 5 ) //insert final 5 elements of array
{
int diff;
int grapeseed = 0;//Same random word
cout << "How many intergers would you like to insert?\n";
cin >> diff;
for( i = n - 5 + count; (i < n) && (grapeseed < diff ) ; i++ )
{
cout<<"Enter array element # "<<i<<"-->";
cin>>dptr[i];
count++;
grapeseed++;
}// end for
insertionSort( dptr, n, count );// sort function
}//end selection 5
if ( selection == 6 ) // let computer insert random numbers for me and give time in seconds to complete
{
int r;
int Q = 49;
for(i=0; i<n-5; i++)
{
// initialize random seed
srand( time(0) * Q );
r = rand() % 1000 +1;
dptr[i] = r;
Q++;
}//end for
}//end selection 6
if ( selection == 7 ) //insert final 5 elements of array
{
int r;
int Q = 14;
for(i=n-5; i<n; i++)
{
// initialize random seed
srand( time(0) * Q );
r = rand() % 1000 +1;
dptr[i] = r;
Q++;
count++;
}//end for
insertionSort( dptr, n, count );// sort function
}//end selection 7
if ( selection == 8 )
{
int findme = 0;
int i = 0;
cout << "Enter interger value to find\n";
cin >> findme;
for ( i; (findme != *( dptr + i)) && ( i <= n ); i++ )
{
//nothing to see here, keep moving on
}
if ( i <= n )
cout << "*( dptr + " << i << " )" << " is where " << findme << " is located\n";
else
cout << " You select a number that is not in the array\n";
}//end if selection 8
} while ( selection != 9 );//end do while loop
return 0; //exit
}//end main
void insertionSort( int dptr[], int n, int count ) //Sort function from book
{
int insert = 0;//Variable Dec
for( int next = 1; next < ( n - 5 + count ); next++ )
{
insert = dptr[ next ]; // store the value in the current element
int moveItem = next; // initialize location to place element
// search for the location in which to put the current element
while ( ( moveItem > 0 ) && ( dptr[ moveItem - 1 ] > insert ) )
{
// shift element one slot to the right
dptr[ moveItem ] = dptr[ moveItem - 1 ];
moveItem--;
} // end while
dptr[ moveItem ] = insert; // place inserted element into the array
} // end for
}//end insertion sort function
|