Home c++ A small problem with leveling numbers on the right edge. C++

A small problem with leveling numbers on the right edge. C++

Author

Date

Category

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);
}

Result Screenshot:

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;
  }
}

Programmers, Start Your Engines!

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.

Recent questions