Computer Science

Arrays

Arrays are a data structure used to store a collection of elements of the same data type. They are commonly used in programming to store and manipulate large amounts of data efficiently. Arrays are accessed using an index, which starts at 0 for the first element.

Written by Perlego with AI-assistance

3 Key excerpts on "Arrays"

  • C Programming
    eBook - ePub

    C Programming

    Learn to Code

    10 Arrays and Strings
    DOI: 10.1201/9781003188254-10

    10.1 Introduction

    Many times, we come across a situation where we use a set of data rather than a single datum. For example, assume that you are an instructor and you teach C programming. You want to store the marks of your students and later perform different types of operations on them, such as finding the top performer or knowing how many students secure less than 50 marks. In that case usage of a single variable is not enough: you need multiple variables to store the data of your students. Again, if you use many variables in your program then remembering those variable names will become difficult. So, the solution is the array – a concept provided by C that handles large numbers of items simultaneously.
    In our day-to-day life we also came across situations where we need to group items and keep them in a sequential manner for easy access. For example, Figure 10.1 shows a toy train built to store painting items such as watercolors, sketch pens, brushes, pencils, and oil pastels. We name this train the Painter’s Train. We number the boxes from 1 to 5 and store many items in them. The concept of arranging similar data and calling them using a common name is sometimes known as an array of items.
    Figure 10.1 Introducing the concept of an array.
    An array is a series of elements of the same type, placed in contiguous memory locations that can be individually referenced by adding an index number to each location. Other definitions are:
    • An array is a single programming variable with multiple "compartments." Each compartment can hold a value.
    • A collection of data items, all of the same type, in which the position of each item is uniquely designated by a discrete type.
    This chapter is dedicated to a discussion of Arrays. After completion of this chapter, readers will have learnt the following:
  • Learn C Programming
    Chapter 11 : Working with Arrays
    We have already seen how a structure is a grouping of one or more components that can each be of different data types. Often, we need a grouping that consists of the same type; this is called an array . An array is a collection of multiple occurrences of the same data type grouped together under a single name. Each element of the array is accessed via its base name and an offset of that base. Arrays have many uses, from organizing homogenous data types to providing the basis for strings, or Arrays of characters.
    Before we can learn about some of the wide uses of Arrays, we need to explore the basics of declaring and manipulating Arrays. The following topics will be covered in this chapter:
    • Declaring an array of values
    • Initializing an array in several ways
    • Understanding variable-length Arrays
    • Accessing each element of an array
    • Understanding zero-based array indexing
    • Assigning and manipulating elements of an array
    • Using looping statements to access all elements of an array
    • Using array references as function parameters

    Technical requirements

    Continue to use the tools chosen from the Technical requirements section of Chapter 1 , Running Hello, World! .
    The source code for this chapter can be found at https://github.com/PacktPublishing/Learn-C-Programming-Second-Edition/tree/main/Chapter11 .

    Declaring and initializing Arrays

    An array is a collection of two or more values, all of which have the same type and share a single common base name. It makes no sense to have an array of just one value; that would simply be a variable. An array definition has the following syntax: dataType arrayIdentifier[ numberOfElements ]; Here, dataType is any intrinsic or custom type, arrayIdentifier is the base name of the array, and numberOfElements specifies how many values of dataType are in the array. numberOfElements , for whatever type and values are given, is a literal constant or expression that will be converted to an integer. Most commonly, numberOfElements is an integer constant expression. An array whose size is defined by a constant expression we call a constant-length array
  • Anyone Can Code
    eBook - ePub

    Anyone Can Code

    The Art and Science of Logical Creativity

    This means that our program will be much longer and harder to write or read. A typical database program, for example, may include hundreds and thousands of numbers that need to be entered and processed. A simple loop can do that in a few lines. But with different names, these lines have to be repeated multiple times, instead of using a loop.
    There is a second problem that is probably less obvious but can be equally challenging to deal with and that is naming. When dealing with a large number of data, it is difficult to name them individually.1 As our example shows, all these data items are performing a similar task, and numbering is probably a better approach than naming. This is the inspiration for the concept of Arrays.
    An array is a single data item since it is defined once and with one name, but it also represents multiple data items since it consists of more than one element. It is a module of data aimed at grouping a set of data items that are related and have a similar type and role in the program. In our previous example, the grade variables are integer numbers representing student grades, and they are all treated the same way by the program (entered by the user and added to a sum variable).
    In C/C++, an array is defined using the [ ] pair after the name of a variable that holds the number of data items that are grouped together as a module and represented by that variable: int grades[10];
    The definition follows the standard for all variables: a type followed by a name. While the definition of a single variable ends there (int number; ), the array has the part that defines the size. Just like single variables that can be initialized right away (int number =0; ), we can initialize an array immediately:
    int grades[] = {100, 90, 83}; //array of three members int numbers[5] = {100, 90, 83}; //5 members. The last 2 are zero
    Accessing the array has to be by referencing a particular member which is done through an index value :
    grades[0] = 100; grades[1] = 95; grades[2] = 0;
    The index values are placed inside the [ ]. Note that the first member of the array is at index 0, and the index value for the last member is the array size minus one. Instead of a constant number, we can use any variable or operation for an index, as long as they have a value which is an integer greater than or equal zero and less than the array size:
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.