All About Arrays

Arrays are similar to ideas in mathematics. An array stores many values in memory using only one name. “Array” in programming means approximately the same thing as array, matrix, or vectordoes in math. Unlike in math, you must declare the array and allocate a fixed amount of memory for it. Subscripts are enclosed in square brackets []. xi in mathematics is x[i] in C++.

Declaring an Array
Declare the array, the array’s size, and the type of elements. All elements must be the same type. Write the element type name, the name of the array variable, then the size enclosed in square brackets: (“[]”).

int scores[100];  // array of 100 ints, scores[0] to scores[99]char name[40];    // array of 40 chars, name[0] to name[39]

Subscripts Start at Zero
Subscript ranges always start at zero. This is a bad idea because it isn’t the way that humans normally count, but it’s the way that C, C++, Java, and many other languages do it. Although the an element with subscript zero is allocated, there is no need to use it, so you can program using subscripts from 1 up. However, most C++ programs use subscripts starting at 0, and we’ll use the convention here.

Length of an Array
There is no way to find the length of an array. You must keep the size (both the maximum, and currently filled size) of the array in variables. For example,

const int MAXSIZE = 366;..int temperature[MAXSIZE];int numOfTemps = 0;  // number of entries in temperature
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS