why isnt the formating correct | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

24th Aug 2019, 6:55 PM
Cat Sauce
Cat Sauce - avatar
6 Answers
+ 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.
24th Aug 2019, 8:24 PM
Shadow
Shadow - avatar
+ 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.
24th Aug 2019, 9:16 PM
Shadow
Shadow - avatar
+ 1
Please allow me to doubt that. :)
24th Aug 2019, 9:25 PM
Shadow
Shadow - avatar
0
Martin Taylor then how should i do this?
24th Aug 2019, 8:16 PM
Cat Sauce
Cat Sauce - avatar
0
but why does it print to a new line?
24th Aug 2019, 9:04 PM
Cat Sauce
Cat Sauce - avatar
0
Shadow oh wow, im stupid
24th Aug 2019, 9:22 PM
Cat Sauce
Cat Sauce - avatar