Code:
//File: areDistinct4.cpp
/////////////////////////////////////////////////
// Include files
/////////////////////////////////////////////////
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
/////////////////////////////////////////////////
// Prototypes
/////////////////////////////////////////////////
void display (int a[], int n);
void populate (int a[], int &n);
void testDeleteAtPos (void);
bool deleteAtPos (int a[], int &n, int p);
/////////////////////////////////////////////////
// Constants
/////////////////////////////////////////////////
const int MAX_VALUE = 50;
const int MAX_COUNT = 10;
const int TEST_CASES = 20;
/////////////////////////////////////////////////
// Main Examples
/////////////////////////////////////////////////
void main(void)
{
srand(time(NULL));
void testDeleteAtPos (void);
}
/////////////////////////////////////////////////
// bool deleteAtPos ( int a[], int &n, int p)
/////////////////////////////////////////////////
/*
Deletes the value at position p from the array a[], if
successful then n--, and returns true. If not successful
then returns false.
Examples:
a[] = {}, n=0, p=12 => a[] = {}, n=0, false
a[]={21},n=1, p=0 => a[]={},n=1, true
a[]={21, 22},n=2, p=0 => a[]={22},n=2, true
a[]={21, 22, 21},n=3, p=1 => a[]={21, 21}, n=2, true
Algorithm
if n = 0 then return false
if p> n - 1 then return false
if p < 0 then return false
if p == n - 1 then
n--, return true
for i = p to n - 2 Step 1
a[i] = i+1]
end for
n--, return true
*/
bool deleteAtPos ( int a[], int &n, int p)
{
if (n == 0)
return false;
if (p > (n - 1))
return false;
if (p < 0)
return false;
if (p == (n - 1))
{
n--;
return true;
}
for (int i = p; i <= n - 2; i++)
a[i] = a [i+1];
n--;
return true;
}
/////////////////////////////////////////////////
//void test DeleteAtPos (void)
/////////////////////////////////////////////////
/*
Algorithm:
create an array a[], n, result, p
do the following 10 times
fill a[] with random values
put a random value in p
display [], p
result = deleteAtPos (a, n, p)
display result, a[]
*/
void testDeleteAtPos (void)
{
cout << "***************\n";
cout << "testDeleteAtPos\n";
cout << "***************\n";
int a[MAX_COUNT];
int n, p;
bool result;
for (int c = 1; c <= TEST_CASES; c++)
{
populate (a, n);
p = -2 + rand()%(MAX_COUNT +3);
display (a, n);
cout << "p = " << p << endl;
result = deleteAtPos (a, n, p);
cout << "result = " << result << endl;
display (a,n);
cout <<"---------------------------\n";
}
}
/////////////////////////////////////////////////
// void populate(int a[], int &n)
/////////////////////////////////////////////////
void populate(int a[], int &n)
{
n = rand()%(MAX_COUNT+1); //n=0 to 10
for (int i = 0; i <= n - 1; i++)
a[i] = rand()%(MAX_VALUE+1);
}
/////////////////////////////////////////////////
// void display(int a[], int n)
/////////////////////////////////////////////////
void display(int a[], int n)
{
cout << "a[" << n << "] = ";
for (int i = 0; i <= n - 1; i++)
cout << a[i] << ' ';
cout << endl;
}
/////////////////////////////////////////////////
/////////////////////////////////////////////////
Thanks again!!