Printar Vector in C

Asked

Viewed 926 times

-1

I would like to print one vector[300] and another [500], but in a way it seems to be, for the user, an array... for example, I did one of [100], where I made the loop to print this vector and an "if" for him to break the line so that when he arrived at divisible values by 10 he would perform it as said.

for(l=0;l<100;l++){
if(l%10==0) // if de quebra de linha 
printf("\n");
if(vetorCEM[l]==0)
printf("[%2d]-",l+1);

the second if is just to show the explicit content of my vector, but what I want to highlight is this example I used, but for the 300 and 500 did not succeed, I wish someone could help me, plisss!!

3 answers

1

your code worked with 100 because 100 is a perfect square (a number that has an entire root), ie the matrix got 10x10, now 300 and 500 are not... If your goal is to get an array with the number of rows and columns equal, at these values will be impossible, now if that is not necessary, you can use any relation, even the i%10 to get a result or some other that brings the number of rows closer to the number of columns ex: for 300, i%30 that will be 30x10 and so on.

  • yes, I did the 100 in this theory of 10x10, and I imagined the 300, which in a matrix would be 15x20, and the 500 20x25... for the 300 I put %15=0, it even worked but until a certain amount of the vector, the same way happened in the 500, that I used %20==0... the intention was not really to print a "perfect matrix", but rather to break the lines in these parameters according to how far the looping goes

  • @Vinisrb But 15x20 does not form a square, and the same for 20x25. Wanting a matrix of 300 elements to be shown as a perfect square is not possible as explained in this answer, because 10x10 gives 100 but there is no integer multiplied by it itself gives 300. Now if what you want is a rapprochement then you should make that clear in the question too.

0

Remember that the vector positions start at 0 and end at x - 1, where x is the size of the vector. So the line break if should look like this: if((i + 1) % y == 0), where:

i: accountant;

y: position of line break.

-2

I believe that is not possible, because an element of a matrix is the intercession of 2 vectors. Create a matrix m[300][500] and use 2 for:

for(int i = 0; i<300; i++)
{
    for(int j = 0; j <500; j++)
    {
        Printf("%d ",m[i][j]);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.