I have a sequence of numbers that splits on the lines. The number of rows depends on the number of numbers (it is selected randomly in the range from 1 to 10) in one row. And all these numbers need to be aligned on the right edge. I learned how to implement it – with the setw (). But I incorrectly align numbers. What is the error, tell me, please.
Code C++:
#include & lt; iostream & gt;
#InClude & lt; Iomanip & gt;
#Include & lt; String & GT;
#Include & lt; Cmath & GT;
#Include & lt; Ctime & gt;
#Include & lt; fstream & gt;
#InClude & lt; Iomanip & gt;
Using Namespace STD;
Void Number1 (int n, int m)
{
int x = n / m;
int y = n% m;
if (y == 0) {
for (int i = 0; i & lt; x; ++ i)
{
COUT & LT; & LT; Endl;
For (int j = 0; j & lt; m; ++ j)
{
COUT & LT; & LT; SETW (230) & LT; & LT; Rand ()% 201 - 100 & lt; & lt; "";
}
}
}
ELSE {
for (int i = 0; i & lt; x; ++ i)
{
COUT & LT; & LT; Endl;
For (int j = 0; j & lt; m; ++ j)
{
COUT & LT; & LT; SETW (230) & LT; & LT; Rand ()% 201 - 100 & lt; & lt; "";
}
}
COUT & LT; & LT; Endl;
for (int k = 0; k & lt; y; k ++)
{
COUT & LT; & LT; SETW (230) & LT; & LT; Rand ()% 201 - 100 & lt; & lt; "";
}
COUT & LT; & LT; Endl;
}
}
INT MAIN ()
{
SETLOCALE (LC_ALL, "RUSSIAN");
SRAND (TIME (0));
// NUMBER1
int n;
COUT & LT; & LT; "Enter the number of numbers =";
CIN & GT; & GT; n;
COUT & LT; & LT; Endl;
int m = rand ()% 11;
COUT & LT; & LT; "Number of numbers in line =" & lt; & lt; M & LT; & lt; Endl;
COUT & LT; & LT; Endl;
NUMBER1 (N, M);
}
They somehow space are located: (
Answer 1, Authority 100%
std :: ostringstream line;
For (int j = 0; j & lt; m; ++ j) {
Line & lt; & lt; (j? "": "") & lt; & lt; Rand ()% 201 - 100;
}
STD :: COUT & LT; & LT; STD :: SETW (230) & LT; & LT; line.str () & lt; & lt; '\ n';
(j? "": "")
, in principle, you can replace one unconditional space, it is for beauty added.
Answer 2
void number1 (int n, int m)
{
int x = n / m;
int y = n% m;
if (y == 0) {
for (int i = 0; i & lt; x; ++ i)
{
COUT & LT; & LT; Endl;
For (int j = 0; j & lt; m; ++ j)
{
COUT & LT; & LT; SETW (230) & LT; & LT; Rand ()% 201 - 100 & lt; & lt; Endl;
}
}
}
ELSE {
For (int j = 0; j & lt; m; ++ j)
{
COUT & LT; & LT; SETW (230) & LT; & LT; Rand ()% 201 - 100 & lt; & lt; Endl;
}
COUT & LT; & LT; Endl;
for (int k = 0; k & lt; y; k ++)
{
COUT & LT; & LT; SETW (230) & LT; & LT; Rand ()% 201 - 100 & lt; & lt; Endl;
}
COUT & LT; & LT; Endl;
}
}