0

I have two vectors of type string with default elements and an array of type integer that is filled with random numbers:

string vector1[5] = {"columna 1", "columna 2", "columna 3", "columna 4", "columna 5"};
string vector2[5] = {"fila 1", "fila 2", "fila 3", "fila 4", "fila 5"};
int matriz[5][5];

srand(time(NULL));

for( int i=0; i<5 ; i++)
    {
    for( int j=0; j<5; j++)
    {
      matriz[i][j]=1+rand()%(1000-1);
      cout<<matriz[i][j]<<"   ";
    }
    cout<<endl;
}

At the moment of executing it it looks very messy because the numbers are random, an order cannot be predicted.

I would like to be able to center the elements of the array but with an order relative to the elements of my vectors, Is there a way to do it with the printf function? I think it would be something like printf ("% 8d% 13d ... but I find it very confusing to understand that syntax with arrays and even worse with arrays. My idea is as shown like an Excel Table.

         columna 1  columna 2  columna 3  columna 4  columna 5
fila 1      697        136        671        170         15
fila 2      637         9         382        758        133
fila 3      207        383        668        887         84
fila 4      796        751        566        236        334
fila 5       72        594        870        262         3
  • for terminal output you should learn ncurses – Egon Stetmann. Mar 04 '21 at 03:05
  • https://en.cppreference.com/w/cpp/io/manip/setw Might help – Borgleader Mar 04 '21 at 03:06
  • Yes, there is a way to do it. (Would you like the opportunity to make your question more precise? Maybe break "it" into a sequence of steps?) – JaMiT Mar 04 '21 at 03:12
  • It would have been more precise but in this forum in English it did not allow me to add images, but I asked the same question in Spanish and I could add images. Thanks for your comments https://es.stackoverflow.com/questions/432675/c%c3%b3mo-imprimir-los-elementos-de-una-matriz-y-de-dos-vectores-en-forma-de-una-tab?noredirect=1#comment772536_432675 – Adrian Bravo Mar 04 '21 at 03:25
  • @AdrianBravo An example of a more precise question: *"I have an `int` and a width. How can I stream the `int` to `cout` so that it is centered within the width?"* This is the precise tool you need to get the formatting you desire (assuming your column headings are wider than your integers -- otherwise determining the width is a separate, second question). If you want to provide an example, an image is not necessary. A code block can be used to provide fixed-width formatting, allowing you to type in your exact desired output. – JaMiT Mar 04 '21 at 03:57
  • @JaMiT Thank you for all of your help! And thank you for the edit in my question, I want exactly like that table, do you know how to do that but with printf? Something like printf("%8d%13d .... I'm so confused trying to understand how is the syntax combined with array and even worse with a matrix. – Adrian Bravo Mar 04 '21 at 05:02
  • @AdrianBravo You could look at [A: How can I easily format my data table in C++?](https://stackoverflow.com/a/20924887), which has a function for center alignment. I have not checked it for accuracy myself, but the voting indicates it is useful. – JaMiT Mar 05 '21 at 00:36

0 Answers0