Home c++ Complexable C++ task. Subject: Sorting Matrix (two-dimensional array)

# Complexable C++ task. Subject: Sorting Matrix (two-dimensional array)

Author

Date

Category

A one-dimensional array of numbers is set, its values ​​must be transferred to a square matrix, but so that the numbers and columns do not repeat.

The number of elements in the array provides the possibility of creating a square matrix. That is, if the numbers 9 then the matrix will be 3 to 3, if 25 then 5 to 5.

``````while (true)
{ // endless cycle
System ("CLS"); // Clean the screen
COUT & LT; & LT; "Enter the number of elements" & lt; & lt; Endl;
CIN & GT; & GT; m;
if (SQRT (M) == (Long Long) SQRT (M)) Break; // Check that the root is an integer
}
``````

The number of repetitive elements cannot exceed the root of all elements. That is, if numbers 9 then the repetitions can be maximum 3 (as the number of rows and columns).

``````while (true) {
COUT & LT; & LT; "Enter an array" & lt; & lt; Endl;
for (int i = 0; i & lt; m; i ++)
{
While (! (CIN & GT; & GT; (MAS [I])) || (cin.peek ()! = '\ n'))
{
check ();
}
}
int repeat = 0;
for (int i = 0; i & lt; n; i ++)
{
for (int j = 0; j & lt; n; j ++)
{
if (MAS [i] == MAS [j]) REPEAT ++;
}}
if (N & GT; = REPEAT) Break;
else {cout & lt; & lt; "From these numbers it is impossible to build a matrix satisfying the assignment condition" & lt; & lt; endl;}
}
``````

My code But without sorting itself:

``````# include & lt; iostream & gt;
#Include & lt; Cmath & GT;
#Include & lt; Cstdlib & gt;
#InClude & lt; Iomanip & gt;
Using Namespace STD;
Void Check ()
{
cin.clear ();
While (cin.get ()! = '\ n');
COUT & LT; & LT; "Input Error!" & lt; & lt; Endl;
COUT & LT; & LT; "Repeat input!" & lt; & lt; Endl;
}
INT MAIN ()
{
While (True) {// Infinite Cycle
System ("CLS"); // Clean the screen
SetLocale (LC_ALL, "");
int m;
// -------------------
While (True)
{ // endless cycle
System ("CLS"); // Clean the screen
COUT & LT; & LT; "Enter the number of elements" & lt; & lt; Endl;
While (! (CIN & GT; & GT; M) || (cin.peek ()! = '\ n'))
{
check ();
}
if (SQRT (M) == (Long Long) SQRT (M)) Break;
}
// -------------------
INT N = SQRT (M);
INT MAS [M];
// -------------------
While (True) {
COUT & LT; & LT; "Enter an array" & lt; & lt; Endl;
for (int i = 0; i & lt; m; i ++)
{
While (! (CIN & GT; & GT; (MAS [I])) || (cin.peek ()! = '\ n'))
{
check ();
}
}
int repeat = 0;
for (int i = 0; i & lt; n; i ++)
{
for (int j = 0; j & lt; n; j ++)
{
if (MAS [i] == MAS [j]) REPEAT ++;
}}
if (N & GT; = REPEAT) Break;
else {cout & lt; & lt; "From these numbers it is impossible to build a matrix of a removable assignment condition" & lt; & lt; endl;}
}
// --------------------------------------- Checking the array
COUT & LT; & LT; "---------------" & lt; & lt; Endl;
for (int i = 0; i & lt; m; i ++)
{
COUT & LT; & LT; MAS [I];
}
COUT & LT; & LT; "---------------" & lt; & lt; Endl;
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// ------------------------------------ In this part requires help
m = 0;
int * mat [n] [n];
for (int i = 0; i & lt; (n); i ++)
for (int j = 0; j & lt; (n); j ++)
{
IF (Mat [i] [j]) {
Mat [i] [j] = & amp; mas [m];
m ++;}
}
COUT & LT; & LT; "---------------" & lt; & lt; Endl;
for (int i = 0; i & lt; (n); i ++)
{
for (int j = 0; j & lt; (n); j ++)
COUT & LT; & LT; (* Mat [i] [j]) & lt; & lt; "\ t";
COUT & LT; & lt; "\ n";
}
COUT & LT; & LT; "---------------" & lt; & lt; Endl;
// ------------------------------------
COUT & LT; & LT; "" & lt; & lt; endl;
COUT & LT; & LT; "Perform the program again? (1-yes, 2-no) \ n";
CIN & GT; & GT; MENU;
}
Return 0;
}
``````

Well, enter a lot of numbers – not mine, so I have this matrix simply generated by chance, change there.

``````# include & lt; vector & gt;
#Include & lt; iostream & gt;
#InClude & lt; Iomanip & gt;
#Include & lt; Map & GT;
#Include & lt; Cmath & GT;
#Include & lt; assert.h & gt;
Using Namespace STD;
vector & lt; vector & lt; int & gt; & gt; make (int m, map & lt; int, int & gt; els)
{
vector & lt; vector & lt; int & gt; & gt; s (m, vector & lt; int & gt; (m, 0));
vector & lt; vector & lt; int & gt; & gt; f = s;
// Arrange takes
int col = 0;
for (auto & amp; x: els)
{
if (x.second & gt; 1)
{
for (int j = 0; j & lt; x.second; ++ j)
{
s [(col + j)% m] [j] = x.first;
f [(col + j)% m] [j] = 1;
}
col ++;
x.second = 0;
}
}
// Arrange the rest
auto it = els.begin ();
if (it! = els.end ())
for (int i = 0; i & lt; m; ++ i)
for (int j = 0; j & lt; m; ++ j)
if (f [i] [j] == 0)
{
while (it- & gt; second == 0) it ++;
s [i] [j] = it- & gt; first;
it ++;
}
return s;
}
int main (int argc, const char * argv [])
{
srand (time (0));
int n = 25;
int m = sqrt (n) +0.5;
if (m * m! = n) {cout & lt; & lt; n & lt; & lt; "- not a square! \ n"; return 0; }
map & lt; int, int & gt; els;
for (int k, i = 0; i & lt; n; ++ i)
{
k = rand ()% 40;
els [k] ++;
cout & lt; & lt; k & lt; & lt; "";
}
cout & lt; & lt; endl & lt; & lt; endl;
int dbls = 0, max_dbl = 0;
for (auto x: els)
{
if (x.second & gt; 1) dbls ++;
if (x.second & gt; max_dbl) max_dbl = x.second;
}
if (dbls & gt; m || max_dbl & gt; m) {cout & lt; & lt; "No solution! \ N"; return 0; }
auto v = make (m, els);
for (auto r: v)
{
for (auto c: r) cout & lt; & lt; setw (2) & lt; & lt; c & lt; & lt; "";
cout & lt; & lt; endl;
}
}
``````

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.