//file: COOL002.cpp
//Author: AOU
//Date 09/03/2004
////////////////////////////////////////////////////////
// Assignment 01, Date Assigned 9/3/2004, Due: 9/10/2004
////////////////////////////////////////////////////////
/*
As discussed in the class, implement the search function that finds
the occurrence of a value x in a list of sorted values and returns
the first position of the value and the number of times it occurs.
Submit the printed form of the following:
(1) Detailed Description:
(2) Two Example:
(3) Detailed Algorithm:
(4) Code:
(5) Output from 20 Test Cases:
void COOL::search(int x, int &p, int &c)
{
//Code to be replaced
p=1;
c=3;
}
Sample output:
COOL[19] = [-9999 0 1 1 2 3 5 5 5 6 7 7 7 7 7 8 9 9 9 10 9999 ]
Searching for 7
Found 5 copies of 7 starting at position 10
In other words, 7 was found at positions 10 11 12 13 14
COOL[18] = [-9999 1 2 2 4 5 5 6 6 7 7 7 8 8 9 9 10 10 10 9999 ]
Searching for 4
Found 1 copies of 4 starting at position 4
In other words, 4 was found at positions 4
COOL[7] = [-9999 0 0 5 5 5 6 7 9999 ]
Searching for 4
Found 0 copies of 4 starting at position -1
In other words, 4 was found at positions
COOL[5] = [-9999 0 1 3 6 10 9999 ]
Searching for 0
Found 1 copies of 0 starting at position 1
In other words, 0 was found at positions 1
*/
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// includes
////////////////////////////////////////////////////////
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
////////////////////////////////////////////////////////
// constants
////////////////////////////////////////////////////////
int const MAX_SIZE = 20+2;
int const INFINITY = 9999;
int const TEST_COUNT = 20;
int const MAX_VALUE = 10;
/*
Description:
Example:
Algorithm:
*/
//Problem:
// List of values (duplicates allowed) kept in order
// with several operations:
// initialize, insert, delete, modify, search, ...
//Example:
// {}, {50}, {25,50}, {25,36,50}, {25,36,50,76},
// {25,36,50,76}
//Algorithms:
////////////////////////////////////////////////////////
// class COOL
////////////////////////////////////////////////////////
class COOL
{
private:
int a[MAX_SIZE];
int n;
public:
COOL(void);
bool insert(int x);
void display(void);
bool isSorted(void);
COOL(int m);
int search(int x);
void search(int x, int &p, int &c);
};
////////////////////////////////////////////////////////
// test function prototypes
////////////////////////////////////////////////////////
void test_initialize(void);
void test_constructor(void);
void test_search(void);
void test_search2(void);
////////////////////////////////////////////////////////
// main
////////////////////////////////////////////////////////
void main(void)
{
//srand(time(NULL));
//test_initialize();
//test_constructor();
//test_search();
test_search2();
}
////////////////////////////////////////////////////////
// void COOL::search(int x, int &p, int &c)
////////////////////////////////////////////////////////
/*
Description: searches for copies of x in the list,
in p returns the position, in c returns the number
of copies of x starting at p.
Example:
[-99, 12, 24, 24, 24, 45, 50, 60, 75, 99], 24
=> 2, 3
=> at positions 2, 3, 4
Algorithm:
*/
void COOL::search(int x, int &p, int &c)
{
//Code to be replaced
p=1;
c=3;
}
////////////////////////////////////////////////////////
// void test_search2(void)
////////////////////////////////////////////////////////
void test_search2(void)
{
for (int i=1; i<=TEST_COUNT; i++)
{
int m = rand()%MAX_SIZE;
COOL myList(m);
myList.display();
int x = rand()%(MAX_VALUE +1);
cout << "Searching for " << x << endl;
int p, c;
myList.search(x, p, c);
cout << "Found " << c << " copies of " << x << " starting at position " << p << endl;
cout << "In other words, " << x << " was found at positions ";
for (int j=p; j<=p+c-1; j++)
cout << j << ' ';
cout << endl << endl;
}
}
////////////////////////////////////////////////////////
// int COOL::search(int x)
////////////////////////////////////////////////////////
/*
Description: searches for the first value x in the list
and returns the position if found else returns -1
Example:
[-99, 12, 24, 45, 50, 60, 75, 99], 24 => 2
[-99, 12, 24, 45, 50, 60, 75, 99], 25 => -1
Algorithm:
for i=1 to n-2
if x = a[i] then
return i
if x < a[i] then
exit loop
end for
return -1
*/
int COOL::search(int x)
{
for (int i=1; i<=n-2; i++)
{
if (x == a[i])
return i;
if (x < a[i])
break;
}
return -1;
}
////////////////////////////////////////////////////////
// void test_search(void)
////////////////////////////////////////////////////////
void test_search(void)
{
for (int i=1; i<=TEST_COUNT; i++)
{
int m = rand()%MAX_SIZE;
COOL myList(m);
myList.display();
int x = rand()%(MAX_VALUE +1);
cout << "Searching for " << x << endl;
cout << "Found at " << myList.search(x) << endl;
}
}
////////////////////////////////////////////////////////
// COOL::COOL(int m)
////////////////////////////////////////////////////////
//create a list with m values
COOL::COOL(int m)
{
n = 2;
a[0] = -INFINITY;
a[1] = +INFINITY;
for (int i=2; i<=MAX_SIZE-1; i++)
a[i] = 0;
for (i=1; i<=m; i++)
{
int x = rand()%(MAX_VALUE+1);
insert(x);
}
}
////////////////////////////////////////////////////////
// void test_constructor(void)
////////////////////////////////////////////////////////
void test_constructor(void)
{
for (int i=1; i<=TEST_COUNT; i++)
{
int m = rand()%MAX_SIZE;
COOL myList(m);
cout << "After COOL myList(" << m << ");\n";
myList.display();// display(myArray, m);
}
}
////////////////////////////////////////////////////////
// bool COOL::isSorted(void)
////////////////////////////////////////////////////////
// bool isSorted(int a[], int n)
/*
Description: checks if the list is in order
Example:
Algorithm:
*/
bool COOL::isSorted(void)
{
for (int i=0; i<=n-2; i++)
if (a[i] > a[i+1])
return false;
return true;
}
////////////////////////////////////////////////////////
// void test_initialize(void)
////////////////////////////////////////////////////////
void test_initialize(void)
{
COOL myList;
for (int i=1; i<=TEST_COUNT/TEST_COUNT; i++)
{
//myList.initialize(); //initialize(myArray, m);
myList.display();// display(myArray, m);
int j, x;
for (j=1; j<=TEST_COUNT; j++)
{
x = rand()%(MAX_VALUE+1);
myList.insert(x); //insert(myArray, m, x);
myList.display(); //display(myArray, m);
if (myList.isSorted()) //(isSorted(myArray, m))
cout << "List is in order\n";
else
cout << "List is NOT in order\n";
}
}
}
////////////////////////////////////////////////////////
// COOL::COOL(void)
////////////////////////////////////////////////////////
/*
Description:
Initializes the list of values by setting n=0, and
a[0..MAX_SIZE-1] = 0;
Algorithm:
n = 2
a[0] = -INFINITY
for i=1 to MAX_SIZE-2
a[i] = 0;
a[MAX_SIZE-1] = +INFINITY
*/
COOL::COOL(void)
{
n = 2;
a[0] = -INFINITY;
a[1] = +INFINITY;
for (int i=2; i<=MAX_SIZE-1; i++)
a[i] = 0;
}
////////////////////////////////////////////////////////
// bool COOL::insert(int x)
////////////////////////////////////////////////////////
/*
Description: inserts x into the list and maintains the order
Example:
{}, {50}, {25,50}, {25,36,50}, {25,36,50,76},
{25,36,50,76}
Algorithm0:
Case0: list is full
Case1: x goes to empty list
x Case3: x > all the values in the list
x Case2: x < all the values in the list
Case4: x goes between two values and handle duplicates
Algorithm1:
m = n
s1: if (x >= a[m-1]) then
a[m] = x
n++
exit function
else
a[m] = a[m-1]
m--
goto s1
Algorithm2:
m = n
while x < a[m-1])
a[m] = a[m-1]
m--
wend
a[m] = x
n++
*/
bool COOL::insert(int x)
{
if (n >= MAX_SIZE)
return false;
int m = n;
while (x < a[m-1])
{
a[m] = a[m-1];
m--;
}
a[m] = x;
n++;
return true;
}
////////////////////////////////////////////////////////
// void COOL::display(void)
////////////////////////////////////////////////////////
/*
Description:
Algorithm:
*/
void COOL::display(void)
{
cout << "COOL[" << n-2 << "] = [";
for (int i=0; i<=n-1; i++)
cout << a[i] << ' ';
cout << "]\n";
}
|