Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop p

c++ program to get the last element of array | c++ programs

Get the last element from an Array, c++ code


#include < iostream >
#include < vector >
int main() {
/**
* first find the size of an array using sizeof()
*
* size of an array, s = sizeof(array) in bytes.
* find out the size of first element, sof = sizeof(array[0])
*
* find out the total number of elements, N = s / sof
*
* index of last item in array, iol = N - 1 <-- it is the index of last element */
// s=20 bytes // sof=4 bytes
// N=5 // iol=4
int nArray[]={1, 2, 3, 4, 9};
std::cout <<
nArray[sizeof(nArray)/sizeof(nArray[0]) - 1];
return 0;
}

No comments:

Post a Comment