📅 Date: Jan 11, 2026
🔥 Topic: Array Input & Traversal (Loops)
🔄 Loops + Arrays = ❤️
Since array indexes follow a sequence (0, 1, 2...), we can use a For Loop to visit every element. This is called "Traversing".
💻 Day 11 Code: Taking User Input
Here is a program that asks the user for 5 numbers and then prints them back.
#include <iostream>
using namespace std;
int main() {
int n = 5;
int arr[n];
// Input Loop
cout << "Enter 5 numbers:" << endl;
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
// Output Loop
cout << "You entered: ";
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
💠Thoughts
The variable i in the loop acts perfectly as the index for the array. This is the foundation for almost every array algorithm I will learn next.
No comments:
Post a Comment