0
why isnt the formating correct
does someone know the reason why the output isnt 1 2 3 4 https://code.sololearn.com/c7O2IdlIB1KD/?ref=app
7 odpowiedzi
+ 1
Cat Sauce, simply initialize your vector like this
... grid { { 1, 2 }, { 3, 4 } };
Right now, you have a vector containing four vectors with a single element inside, so each is printed to a new line. Doing it the way I showed above creates two vectors inside the 'grid' vector, both with two elements inside. That way the output will be what you expect it to be.
+ 1
If you are referring to the old version of thw code, the reason is that the vector contained four vectors with each one element. The outer for loop loops over the container vector, which means it iterates over the four vectors inside 'grid'. The inner loop goes over all elements inside those vectors and prints them. Since each vector only contains a single element, that element is printed and the loop ends. After the loop has been executed, a new line is inserted. Therefore, each number was in a new line because it was stored in a separate vector.
However, in the new version, you have three vectors, each containing three numbers. Since the inner loop iterates over a vector, all three numbers are printed before the loop is terminated and a new line is added.
It's essentially about how the nested for loop runs over the two-dimensional vector.
+ 1
Please allow me to doubt that. :)
0
Martin Taylor then how should i do this?
0
but why does it print to a new line?
0
Shadow oh wow, im stupid