topical media & game development
basic-program-solutions-04-Soln4-4.c
? /
basic-program-solutions-04-Soln4-4.c
// Soln4_4.cpp : main project file.
include <stdafx.h>
using namespace System;
// This uses an array of bool values to record which data values
// have been output at any given time. A value that has been output
// is marked as true in the used array, and only values that have a
// corresponding true value in the used array are checked each time.
int main(array<System::String ^> ^args)
{
Random^ generator = gcnew Random;
array<int>^ values = gcnew array<int>(generator->Next(10,20));
// Initialize the array of data value
for (int i = 0 ; i < values->Length ; i++)
values[i] = generator->Next(100,1000);
// Create array to identify elements that have been output
array<bool>^ used = gcnew array<bool>(values->Length);
Array::Clear(used, 0, used->Length); // Set elements false
Console::WriteLine(L"There are {0} values.", values->Length);
int minimum = 0; // Current minimum
int minIndex = 0; // Index of current minimum
// Output the values in ascending sequence
for(int count = 0 ; count<values->Length ; count++)
{
// Find first candidate for minimum
for(int i = 0 ; i<values->Length ; i++)
if(minimum == 0 && !used[i])
{
minimum = values[i];
minIndex = i;
break;
}
// Look for a lower minimum from remaining elements
for(int i = minIndex+1 ; i<values->Length ; i++)
if(minimum>values[i] && !used[i])
{
minimum = values[i];
minIndex = i;
}
used[minIndex] = true; // Record minimum as used
Console::Write(L"{0,10}", minimum); // Write the minimum
if((count+1)%5 == 0) // If it's multiple of 5
Console::WriteLine(); // Write a newline
minimum = 0; // Reset minimum to 0
}
Console::WriteLine();
return 0;
}
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.