Loops thought Multi-Dimensional Arrays
As you may have noticed, assigning each element of the multi-dimensional array is extremely inconvenient. Here we can use loops! In the beginner c++ course, we used for loop to go through one-dimensional array. Let's modify this method and apply it to a 2-dimensional array:
12345678int x[2][3]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { x[i][j] = rand() % 10; cout << x[i][j] << " "; } } return 0;
Here we used two for loops, one for going thought rows, and another for columns. Then we assign to each element a random value from 0 to 10 and print the result.
You should always remember the rule: one loop for each array's dimensions.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Запитайте мені питання про цей предмет
Сумаризуйте цей розділ
Покажіть реальні приклади
Awesome!
Completion rate improved to 2.94
Loops thought Multi-Dimensional Arrays
Свайпніть щоб показати меню
As you may have noticed, assigning each element of the multi-dimensional array is extremely inconvenient. Here we can use loops! In the beginner c++ course, we used for loop to go through one-dimensional array. Let's modify this method and apply it to a 2-dimensional array:
12345678int x[2][3]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { x[i][j] = rand() % 10; cout << x[i][j] << " "; } } return 0;
Here we used two for loops, one for going thought rows, and another for columns. Then we assign to each element a random value from 0 to 10 and print the result.
You should always remember the rule: one loop for each array's dimensions.
Дякуємо за ваш відгук!