๐
Date: Jan 10, 2026
๐ฅ Topic: Arrays (Introduction & Syntax)
๐ฆ The Problem with Variables
If I want to store marks of 50 students, creating 50 variables (m1, m2, m3...) is madness.
That's where Arrays come in.
An Array is a collection of items of the SAME type stored in contiguous memory locations.
๐ Syntax
// datatype arrayName[size];
int marks[5]; // Stores 5 integers
๐ข Indexing (The Catch)
In C++ (and most languages), counting starts from 0, not 1.
First element: marks[0]
Last element: marks[4] (Size - 1)
๐ป Day 10 Code: Basic Declaration
#include <iostream>
using namespace std;
int main() {
// Declare and Initialize
int luckyNumbers[4] = {7, 13, 21, 99};
// Accessing elements
cout << "First Number: " << luckyNumbers[0] << endl;
cout << "Third Number: " << luckyNumbers[2] << endl;
// Modifying an element
luckyNumbers[1] = 100;
cout << "New Second Number: " << luckyNumbers[1] << endl;
return 0;
}
๐ญ Thoughts
Arrays are static; the size is fixed once declared. I need to be careful not to access an index that doesn't exist (like arr[10] in a size 5 array) or I'll get a Garbage Value.
No comments:
Post a Comment