|
Assignment 1 |
This is a warm-up assignment, reviewing material from CSC 310.
First, please read the programming style rules on the Assignments page.
Second, consider the following (slightly incorrect) implementation of selection sort:
void selectionSort(int a[], int n)
{
int minIndex;
for (int i = 0; i < n-1; i++) {
minIndex = findMinIndex(a, i, n);
swap(a[i], a[minIndex]);
}
}
int findMinIndex(int a[], int start, int n)
{
int minIndex = start;
for (int i = start; i < n-1; i++) {
if (a[i] < a[minIndex])
minIndex = i;
}
return minIndex;
}
void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
With this code, please do the following:
#include <iostream.h>
void displayArray(int a[], int n)
{
for(int i = 0; i < n; i++)
cout << a[i] << endl;
}
int main()
{
int a[5] = {5, 4, 3, 2, 1};
selectionSort(a, 5);
displayArray(a, 5);
return 0;
}
#include <iostream.h>
template <class T>
void displayArray(T a[], int n)
{
for(int i = 0; i < n; i++)
cout << a[i] << endl;
}
int main()
{
int a[5] = {5, 4, 3, 2, 1};
double b[7] = {10.5, 9.4, 8.3, 7.2, 6.1, 5.0, 4.9};
selectionSort(a, 5);
displayArray(a, 5);
cout << endl;
selectionSort(b, 7);
displayArray(b, 7);
cout << endl;
return 0;
}
Please submit this in two e-mail messages sent to jrogers@cs.depaul.edu.
Subject lines containing anything else will not be considered assignment submissions.