Sequentially Outputting Elements Of An Array

I've been working on an example from a book, which says to do the following:

Write a C++/CLI program that creates an array with a random number of elements of type int. The array should have from 10 to 20 elements. Set the array elements to random values between 100 and 1000. Output the elements, five to a line, in ascending sequence without sorting the array.

So, I worked on it, and came up with a program that does exactly what the example requires. However, when I checked my work against the solution from the book, I went about it in a much different way. My question is, even though my program works, did I do it the wrong way, in relation to the spirit of the exercise?

Here is my program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Exercise 4.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
	//Declare variable to size array
	int size = 0 ;

	//Randomly generate array size between 10 & 20
	Random^ sizeGenerator = gcnew Random ;
	size = sizeGenerator->Next( 10, 20 ) ;

	//Declare array with random size
	array<int>^ elements = gcnew array<int>( size ) ;

	//Generate & assign random element values
	Random^ elementGenerator = gcnew Random ;
	
	for ( int i = 0 ; i < elements->Length ; i++ )
		elements[ i ] = elementGenerator->Next( 100, 1000 ) ; 

	//Spacers
	Console::WriteLine() ;
	Console::WriteLine() ;

	//Find maximum value
	int max = 0 ;
	for ( int i = 0 ; i < elements->Length ; i++ )
	{
		if ( max < elements[ i ] )		//If max value is < individual element...
			max = elements[ i ]  ;		//Store that element max variable....

	}

	int min = 0 ;			//Set minimum variable to zero.
	int counter = 0 ;		//Set counter variable for elements to zero.

	while ( max > min )		//Find each descending value and output 5 to a line. 
	{
		//Find decending maximum values
		int nextValue = max ;	//Set starting variable to maximum value.
		int index = 0 ;

		for ( int j = 0 ; j < elements->Length ; j++ )	
		{
			if ( ( nextValue > elements[ j ] ) && ( elements[ j ] > min ) )	//Test for new minimum value each iteration
			{
				nextValue = elements[ j  ] ;	//Store new value in nextValue variable
				index = j ;						//Store element index in variable
			}

		}

		//Output values in ascending order, 5 to a line.
		Console::Write(L"{0, 6}", nextValue) ;
		counter++ ;			//Increment iteration count

		if ( counter % 5 == 0 )
			Console::WriteLine() ;
		min = nextValue ;	//Reset minimum to next lowest value
	}

	Console::ReadLine() ;

    return 0;
}


And here is the solution from the book:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 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 = Int32::MaxValue;            // Current minimum - set to maximum possible value
  int minIndex = 0;                         // Index of current minimum

  // Output the values in ascending sequence
  for(int count = 0 ; count<values->Length ; count++)
  {
    // Find minimum of those not yet output
    for(int i = 0 ; i<values->Length ; i++)
      if(!used[i] && minimum > values[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 = Int32::MaxValue;             // Reset to maximum possible
  }

  Console::WriteLine();
  return 0;
}
IMO the spirit of the exercise is to make you practice solving problems.
Printing a sorted sequence without actually sorting it doesn't seem something you'd do in a real world scenario.
Also there is not a one true solution to everything. Some code is more optimized, some is more readable, some both or none...
Last edited on
Yes, there seems to be a flaw in your algorithm. I believe your code fails if the random number generator generates two equal numbers in the sequence. I think in this case your code will only print that repeated number once.
Topic archived. No new replies allowed.