|
Extraignment01
|
Date Assigned 03/13/2002 Date Due 03/15/2002 10:00 AM Weight 1% of the final grade Add recursive version of the min1 function to the following program discussed in the class and call it min2. It should return the index of the minimum value in an array. The array will never be empty, it will have at least one value. Call min1 and min2 in the main to show that your function really works. //pizza02.cpp
//Authors: AOU
/////////////////////////////////////////////////////////////////
// include files
/////////////////////////////////////////////////////////////////
#include <iostream.h>
#include <time.h>
#include <stdlib.h>
/////////////////////////////////////////////////////////////////
// prototypes
/////////////////////////////////////////////////////////////////
void sortBubble(int array[], int low, int high);
void displayArray(int array[], int low, int high);
void displayArray2(int array[], int low, int high);
int min1(int array[], int low, int high);
/////////////////////////////////////////////////////////////////
// main
/////////////////////////////////////////////////////////////////
void main(void)
{
int a[]={21, 12, 30, 19, 21, 69, 13, -1};
int n = sizeof(a)/sizeof(a[0]);
cout << "n = " << n << endl;
displayArray(a, 0, n-1);
displayArray2(a, 0, n-1);
int k = min1(a, 0, n-1);
cout << "minimum value at " << k << " is " << a[k] << endl;
// call here the min2 function
int b[]={21, 12, 30, 19, 21, 69, 13};
int m = sizeof(b)/sizeof(b[0]);
cout << "m = " << m << endl;
displayArray(b, 0, m-1);
displayArray2(b, 0, m-1);
int p = min1(b, 0, m-1);
cout << "minimum value at " << p << " is " << b[p] << endl;
// call here the min2 function
}
/////////////////////////////////////////////////////////////////
// min1 to return the index of minimum value (non-recursive)
/////////////////////////////////////////////////////////////////
int min1(int array[], int low, int high)
{
/*
index of the minimum value?
minYet = low
compare value at minYet with every value in the list
if a value is < value at minYet then
copy index of that value to minYet
return minYet
*/
/*
minYet = low
for i= low to high
if array[i] < array[minYet]
minYest = i
return minYet
*/
int minYet = low;
for (int i= low; i<=high; i++)
if (array[i] < array[minYet])
minYet = i;
return minYet;
}
/////////////////////////////////////////////////////////////////
// displayArray to display array elements (non-recursive)
/////////////////////////////////////////////////////////////////
void displayArray(int array[], int low, int high)
{
for (int i=low; i<= high; i++)
cout << array[i] << ' ';
cout << endl;
}
/////////////////////////////////////////////////////////////////
// displayArray2 to display array elements (recursive)
/////////////////////////////////////////////////////////////////
void displayArray2(int array[], int low, int high)
{
/*
if only one element to be printed then
print that element
else
print the fist element
display the rest
-----------------------
if low and high are the same
print element at low
else
print element at low
display (array, low+1, high)
*/
if (low == high)
cout << array[low] << endl;
else
{
cout << array[low] << ' ';
displayArray2(array, low+1, high);
}
}
/////////////////////////////////////////////////////////////////
// sortBubble to sort array elements (non-recursive)
/////////////////////////////////////////////////////////////////
void sortBubble(int array[], int low, int high)
{
bool sorted;
do
{
sorted = true;
for (int i=low; i<high; i++)
{
if (array[i] > array[i+1])
{
int temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
sorted = false;
}
}
}
while (!sorted);
}
/////////////////////////////////////////////////////////////////
// Output
/////////////////////////////////////////////////////////////////
/*
n = 8
21 12 30 19 21 69 13 -1
21 12 30 19 21 69 13 -1
minimum value at 7 is -1
m = 7
21 12 30 19 21 69 13
21 12 30 19 21 69 13
minimum value at 1 is 12
Press any key to continue
*/
|