Work With Arrays

Submitted by:David Villa

Date added:15 November, 2011

Category:C++

Source code of C/C++ program, showing how to work with arrays.

Tags: arrays

Code Snippet:

#include <iostream>

using namespace std;

int main() {

// array of integer
// stores 3 values
int arr[3];

// assign values using square brackets []
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;

// print values using loop
for (int i = 0; i < 3; i++)
cout << "arr[" << i << "]: " << arr[i] << endl;
cout << endl;

// declare another array

// and assign initial values using figure brackets {}

int arr2[3] = {7, 8, 9};

// print values using loop

for (int i = 0; i < 3; i++)

cout << "arr2[" << i << "]: " << arr2[i] << endl;

cout << endl;

// when initializing using {} size of the array

// can be omitted

int arr3[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

for (int i = 0; i < 9; i++)

cout << "arr3[" << i << "]: " << arr3[i] << endl;

cout << endl;

return 0;
}
 
 

Comments