Order It


General Statement: For a square matrix, indicate the ascending order of the elements by placing an A in the location of the smallest value, a B in the location of the next largest value, etc.

Input: The first line in the data set is an integer that represents the number of data collections that follow. There are an unknown number of integers in each dataset. The integer –1 is used to indicate the end of the collection of integers.

Output: Each column is to be aligned vertically. Output some white space between columns. The output is to be formatted exactly like that for the sample output given below.

Assumptions: The largest matrix will be 5 by 5. There may be duplicate values in the matrix. The integers are in the range 1..99. The integer –1 that indicates the end of a data collection is not part of the data for the problem.

Discussion: A square matrix has the same number of rows as columns. If there are duplicate values, use the same letter for all locations of that value and the next larger value is then assigned the letter it would have if there had been no duplicates.

Sample Input:
3
4 3 9 7 -1
2 8 6 7 9 2 3 2 2 -1
4 5 9 2 7 6 9 8 9 17 6 4 4 5 6 3 -1

Sample Output:
B A
D C

A E C
D F A
B A A

C D H A
F E H G
H I E C
C D E B

Solutions :

#include
#include using namespace std;

int main ()
{
int cases;
scanf (“%d”, &cases);

while ( cases– ) {

int a [28];
int i = 0;

scanf (“%d”, &a [i]);

while ( a [i] != -1 )
scanf (“%d”, &a[++i]);

int length = (int) sqrt (i);
i = 0;
int alpha = 65;

char output [6] [6];

// set output
while ( i < length * length ) { int min = 100; for ( int j = 0; j < length * length; j++ ) { if ( min > a [j] )
min = a [j];
}

for ( int j = 0; j < length * length; j++ ) { if ( a [j] == min ) { output [j / length] [j % length] = alpha; a [j] = 100; i++; } } alpha++; } // show output for ( int j = 0; j < length; j++ ) { for ( int k = 0; k < length; k++ ) { if ( k ) printf (" "); printf ("%c", output [j] [k]); } printf ("\n"); } } return 0; } [/sourcecode]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s