0% found this document useful (0 votes)
2 views2 pages

Array

The document provides an overview of arrays in C++, explaining their definition, creation syntax, and how to check their size. It includes code examples demonstrating array initialization, element access, and looping through array elements using both 'for' and 'while' loops. Additionally, it highlights best practices in array declaration.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Array

The document provides an overview of arrays in C++, explaining their definition, creation syntax, and how to check their size. It includes code examples demonstrating array initialization, element access, and looping through array elements using both 'for' and 'while' loops. Additionally, it highlights best practices in array declaration.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

3IIR – C++ 24/25

/*

Arrays

- What Is Array ?

--- Collection Of Elements Of The Same Type

--- Placed in Contiguous Memory Locations

--- Referenced By Index Started From 0

- Why We Need Array ?

- Creating Array Syntax

- Check Array Size

- Create Array Without Size

Loop

- Loop With For

- Loop On Array

Syntax

for(init, Condition, Update)

// Block Of Code

*/

#include <iostream>

using namespace std;

int main(){

int nums[4] = {100, 200, 300, 400}; // it is possible to leave out the “=” sign – But it is a good
practice to put it.

cout << sizeof(int) << "\n"; // 4 Bytes

cout << sizeof(nums) << "\n"; // 16 Bytes

cout << “Array Elements Count” << sizeof(nums) / sizeof(nums[0]) << "\n"; // Array Elements Count

Pr. Hasnââ CHAABI


3IIR – C++ 24/25

cout << "First Element: " << nums[0] << "\n"; // Prints the first element

cout << "Location: " << &nums[0] << "\n"; // Print the address

double dos[4] = {100, 200, 300, 400};

for (int index = 0; index < 4; index++)

cout << dos[index] << "\n";

while (i < 4) {

cout << dos[i] << "\n";

i++;

int rands[]{100, 5000, 950}; // it is possible to leave out the “=” sign

return 0;}

Pr. Hasnââ CHAABI

You might also like