To understand the implementation an array using C++
Objective
To understand the implementation an array using C++
#include <iostream>
int main() {
// Declare an array of size 5
int arr[5];
// Assign values to the array
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;
// Print the values of the array
for (int i = 0; i < 5; i++) {
std::cout << "Element at " << i << " is " << arr[i] << std::endl;
}
return 0;
}
Discussion of Algorithm
- Start
- Declare and initialize an array of size 5 with values 1, 2, 3, 4, 5
- Loop through the array and print each value
- End
Representations
Flow Diagram
+----------------------------------+ | | | Start | | | | | V | | Declare arr[5] | | | | +----------------------------------+ | V +----------------------------------+ | | | Assign values to arr | | | | +----------------------------------+ | V +----------------------------------+ | | | Initialize i = 0 | | | | +----------------------------------+ | V +----------------------------------+ | | | Check i < 5? | | | No | +-------+--------------------------+ | V +----------------------------------+ | | | Exit | | | | +----------------------------------+
Tabular Dry Run
arr[] | output 0 | 1 1 | 2 2 | 3 3 | 4 4 | 5
Output
Element at 0 is 1 Element at 1 is 2 Element at 2 is 3 Element at 3 is 4 Element at 4 is 5