-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment7.15.cpp
79 lines (79 loc) · 1.8 KB
/
Assignment7.15.cpp
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
/* Peter Danshov
* CST2403 Section 2340
* Assignment 7.15 page 336
* May 6, 2012, 11:58 AM
*
* This application reads in, stores in an array,
* and prints twenty unique numbers.
*/
#include<iostream>
bool linearSearch(const int[], int, int); //linearSearch prototype
void dataSort(int[], int); //dataSort prototype
void validInput(int&); //validInput prototype
using namespace std;
int main()
{
const int arraySize = 20;
int array[arraySize]={};
int number;
cout<<"Input 20 numbers from 10 - 100."<<endl;
for(int c=0; c<arraySize; c++) //populate array with user-entered numbers
{
cout<<"Integer #"<<c+1<<": ";
validInput(number); //gets and validates input
if(linearSearch(array, number, arraySize)) //use linearSearch to check if number already exists in array, if not, insert into array
{
array[c]=number;
}
}
dataSort(array, arraySize); //sort the user-entered numbers
cout<<"Unique user-entered numbers: "; //display all unique user-entered numbers
for(int c=0; c<arraySize-1; c++)
{
if(array[c] != 0)
cout<<array[c]<<", ";
}
cout<<array[19]<<"."<<endl;
return 0;
}
//linearSearch function
bool linearSearch(const int array[], int searchKey, int arraySize)
{
for(int e=0; e<arraySize; e++)
if(array[e] == searchKey)
return false;
return true;
}
//dataSort function
void dataSort(int array[], int arraySize)
{
int insert;
for(int next=1; next<arraySize; next++)
{
insert=array[next];
int moveItem=next;
while((moveItem>0) && (array[moveItem-1]>insert))
{
array[moveItem] = array[moveItem-1];
moveItem--;
}
array[moveItem]=insert;
}
}
//validInput function
void validInput(int& digit)
{
for(;;)
{
if(cin>>digit)
{
break;
}
else
{
cout<<"Please enter a valid integer"<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
}