# 9.4: Finding a Specific Member of an Array

$$\newcommand{\vecs}{\overset { \rightharpoonup} {\mathbf{#1}} }$$ $$\newcommand{\vecd}{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$

Given an array, find first and last elements of it.

In C++, we can use sizeof operator to find number of elements in an array. Notice we have to calculate the size based on the entire size of memory taken up by the array divided by the sizeof one element of the array.

// C++ Program to print first and last element in an array
#include <iostream>
using namespace std;
int main()
{
int myArr[] = { 4, 5, 7, 13, 25, 65, 98 };
int first, last, numberOf;

// Calculate how many elements are in the array
numberOf = sizeof(myArr) / sizeof(myArr);

first = myArr;
// use [numberOf - 1] because arrays count from 0
last = myArr[numberOf - 1];
cout << "First element: " << first << endl;
cout << "Last element: " << last << endl;
// OR you could simply write
cout << "First element: " << myArr << endl;
cout << "Last element: " << myArr[numberOf - 1] << endl;
return 0;
}


This is pretty simple...and the output is:

First element: 4
Last element: 98


We could have made the code simpler by coding the cout statements as:

cout << "First element: " << myArr << endl;
cout << "Last element: " << myArr[n - 1] << endl;

There is no need to use other variables...but it was an example of how to access elements of the array.

A little more complex code here as an example of how to access array elements.

#include <iostream>
using namespace std;

int main()
{
int newArr[] = { 74, 5, 37, 13, 2, 15, 9 };
int low, arrSize = sizeof(newArr) / sizeof(newArr);

// Assign the first element to the variable low
low = newArr;

// Loop over every element in the array
for(int index = 0; index < arrSize; index++)
{
// If the current array element (newArr[index]) is less than temp
if(low > newArr[index])
{
// Then we assign that current array element to be the new lowest
low = newArr[index];
}
}

cout << "Smallest Element is: " << low << endl;
return 0;
}


The smallest number is found when we look at every elements and simply track the smallest value.

Smallest Element is: 2


9.4: Finding a Specific Member of an Array is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.