4.Arrays
04 Sep 2019 | C++
Example 1
#include <iostream>
using namespace std;
//ARRAYS
main()
{
int a;
//int a,b,c,d,e,f,g,h,i,j;
//int var1,var2,var3,var4,var5...
int x[100];
int array[4]; //TYPE NAME[SIZE_NR_OF_ELEMENTS];
array[0] = 10;
array[1] = 50;
array[2] = 256;
array[3] = 512;
//array[4] = 125125; we can't do it
/*
0 1 2 3 4 5 6 7 8 9 a b c d e f
1 2 a = 1 * 16 ^ 2 + 2 * 16 ^ 1 + a * 16 ^ 0 = 256 + 32 + 10 = 298
*/
cout << "array [0] = " << array[0] << ", address: " << &array[0] << endl;
cout << "array [1] = " << array[1] << ", address: " << &array[1] << endl;
cout << "array [2] = " << array[2] << ", address: " << &array[2] << endl;
cout << "array [3] = " << array[3] << ", address: " << &array[3] << endl;
cout << "array [0] = " << array[0] << ", address: " << array << endl;
if (&array[0] == array)
cout << "thats true";
}
Example 2
#include <iostream>
using namespace std;
//multidimensional arrays
main()
{
int biArray[3][4] = {0};
//int biArray[2][3][2];
cout << &biArray[0] << endl;
cout << &biArray[0][0] << endl;
cout << &biArray[0][1] << endl;
cout << &biArray[0][2] << endl;
cout << &biArray[0][3] << endl;
cout << &biArray[1] << endl;
cout << &biArray[1][0] << endl;
cout << &biArray[1][1] << endl;
}
Example 3
#include <iostream>
using namespace std;
main ()
{
int arr[3][3];
int xCoordinate, yCoordinate;
arr[0][0] = 5;
arr[0][1] = 3;
arr[0][2] = 2;
arr[1][0] = 1;
arr[1][1] = 4;
arr[1][2] = 15;
arr[2][0] = 123;
arr[2][1] = 12;
arr[2][2] = 42;
cout << "Input x coordinate: ";
cin >> xCoordinate;
cout << "Input y coordinate: ";
cin >> yCoordinate;
cout << arr[xCoordinate-1][yCoordinate-1];
}
Example 1
#include <iostream>
using namespace std;
//ARRAYS
main()
{
int a;
//int a,b,c,d,e,f,g,h,i,j;
//int var1,var2,var3,var4,var5...
int x[100];
int array[4]; //TYPE NAME[SIZE_NR_OF_ELEMENTS];
array[0] = 10;
array[1] = 50;
array[2] = 256;
array[3] = 512;
//array[4] = 125125; we can't do it
/*
0 1 2 3 4 5 6 7 8 9 a b c d e f
1 2 a = 1 * 16 ^ 2 + 2 * 16 ^ 1 + a * 16 ^ 0 = 256 + 32 + 10 = 298
*/
cout << "array [0] = " << array[0] << ", address: " << &array[0] << endl;
cout << "array [1] = " << array[1] << ", address: " << &array[1] << endl;
cout << "array [2] = " << array[2] << ", address: " << &array[2] << endl;
cout << "array [3] = " << array[3] << ", address: " << &array[3] << endl;
cout << "array [0] = " << array[0] << ", address: " << array << endl;
if (&array[0] == array)
cout << "thats true";
}
Example 2
#include <iostream>
using namespace std;
//multidimensional arrays
main()
{
int biArray[3][4] = {0};
//int biArray[2][3][2];
cout << &biArray[0] << endl;
cout << &biArray[0][0] << endl;
cout << &biArray[0][1] << endl;
cout << &biArray[0][2] << endl;
cout << &biArray[0][3] << endl;
cout << &biArray[1] << endl;
cout << &biArray[1][0] << endl;
cout << &biArray[1][1] << endl;
}
Example 3
#include <iostream>
using namespace std;
main ()
{
int arr[3][3];
int xCoordinate, yCoordinate;
arr[0][0] = 5;
arr[0][1] = 3;
arr[0][2] = 2;
arr[1][0] = 1;
arr[1][1] = 4;
arr[1][2] = 15;
arr[2][0] = 123;
arr[2][1] = 12;
arr[2][2] = 42;
cout << "Input x coordinate: ";
cin >> xCoordinate;
cout << "Input y coordinate: ";
cin >> yCoordinate;
cout << arr[xCoordinate-1][yCoordinate-1];
}
Comments