Computer Science

One Dimensional Arrays in C

One dimensional arrays in C are a way to store a collection of data of the same type in a contiguous block of memory. They are declared using square brackets and can be accessed using an index. Arrays can be used to simplify code and make it more efficient.

Written by Perlego with AI-assistance

9 Key excerpts on "One Dimensional Arrays in C"

  • Data Structures and Program Design Using C++
    3

    ARRAYS

    In This Chapter
    Introduction
    Definition of an array
    Array declaration
    Array initialization
    Calculating the address of array elements
    Analyzing an algorithm
    Abstract data types
    Declaration of two-dimensional arrays
    Operations on 2-D arrays
    Multidimensional arrays/ N-dimensional arrayys
    Calculating the address of 3-D arrays
    Arrays and pointers
    Array of pointers
    Arrays and their applications
    Sparse matrices
    Types of sparse matrices
    Representation of sparse matrices
    Summary
    Exercises

    3.1Introduction

    We have already studied the basics of programming in data structures and C++ in the previous chapter in which we aimed to design good programs, where a good program refers to a program which runs correctly and efficiently by occupying less space in the memory, and also takes less time to run and execute. Undoubtedly, a program is said to be efficient when it executes with less memory space and also in minimal time. In this chapter, we will learn about the concept of arrays. An array is a user-defined data type that stores related information together. Arrays are discussed in detail in the following sections.

    3.2Definition of an Array

    An array is a collection of homogeneous (similar) types of data elements in contiguous memory . An array is a linear data structure because all elements of the array are stored in linear order. Let us take an example in which we have ten students in a class, and we have been asked to store the marks of all ten students; then we need a data structure known as an array.
    FIGURE 3.1 .
    Representation of an array of 10 elements.
    In the previous example, the data elements are stored in the successive memory locations and are identified by an index number (also known as the subscript), that is, Ai or A[i]. A subscript is an ordinal number which is used to identify an element of the array
  • Composite Data Structures and Modularization
    eBook - ePub

    Composite Data Structures and Modularization

    Volume 2: Composite Data Structures and Modularization

    • Xingni Zhou, Qiguang Miao, Lei Feng(Authors)
    • 2020(Publication Date)
    • De Gruyter
      (Publisher)
    Figure 1.7 shows how to define an array, where a definition is constructed by a type identifier followed by an array name and multiple constants inside square brackets. Each constant indicates the number of variables in the corresponding dimension.
    Figure 1.7: Definition of arrays.
    In the figure above, the first row defines a one-dimensional integer array x with 100 variables. To compute the size of its memory space, we can obtain the size of its type using the sizeof operator and multiply it with the number of variables. The second row defines a two-dimensional character array with two rows and three columns. In other words, it has six variables in total. The array name is c.
    1.2.1.2  Reference of array elements
    C uses a special term for variables in an array: array elements. An array element is used in the same way as a single variable. To reference an array element, we use the array name suffixed by an index wrapped in square brackets.
    Think and discuss Do contents inside square brackets in an array definition and an element reference refer to the same thing?
    Discussion: The index of an array element is a numerical expression, which indicates the position of the element in an array; the object inside square brackets in an array definition has to be a constant, which indicates the number of elements in the corresponding dimension. It is worth noting that the number of elements must not be a variable. Like plain variables, arrays obtain memory space from the system during array definition. The size of the allocated space does not change during execution once the array is defined. Such a way of memory utilization and management is called static memory allocation. On the other hand, C also provides “dynamic memory allocation,” which will be introduced in examples in chapter “Functions”.
    Indices of array elements in C must start from 0. Accessing an array out of bound leads to a logic error, but it is not a syntax error.
    For example, the one-dimensional array x defined in Figure 1.8
  • C Programming
    eBook - ePub

    C Programming

    Learn to Code

    A 1D array is a collection of items having common data types stored in a contiguous memory location, identified by a single name with its index number. More generally, suppose we have stored five different integers in a continuous memory location with a common name. We can access each integer with the help of the name of the array and index number – then the whole scenario is called an integer array.
    A one-dimensional array is a collection of items having common data types stored in a contiguous memory location, identified by a single name with its index number.

    10.4.1 Declaration of 1D Arrays

    As we know, before using any variable, we must declare it. Similarly, we must declare the array before we use it in our program code. Let us recall how a variable is declared because there is a little difference between the declaration of a variable and the declaration of an array (see Figure 10.3 ).
    Figure 10.3 Variable declaration with example.
    In this example, x represents the name of the variable, and int represents the type of data that x can store and the size (in bytes) of x (see Figure 10.4 ).
    Figure 10.4 Describing int x.
    In the same manner, we can declare an array. Figure 10.5 shows the syntax.
    • The data type indicates the type of data we can store inside an array;
    • Array names can be valid identifiers;
    • Size (also known as the dimension) indicates the number of the same type of data we can store inside the array;
    • The size must be an integer and specified within two square brackets.
    • See Figure 10.6 for an illustration of the declaration process. This indicates the array named age will contain five integer values;
    • This declaration will immediately reserve 20 bytes of memory because each of these five integers will be four bytes long (we assume the integer takes four bytes for our illustration).
    Figure 10.5 Array declaration with example.
    Figure 10.6 Illustration of the example shown in Figure 10.5.

    Note

    The dimension of the array must be a constant. We cannot use a variable name for representing the dimension of the array. This is possible, but for the time being, we will not concentrate on this aspect, as it will be discussed later whenever there is a need.
  • C
    eBook - ePub

    C

    From Theory to Practice, Second Edition

    • George S. Tselikis, Nikolaos D. Tselikas(Authors)
    • 2017(Publication Date)
    • CRC Press
      (Publisher)
    7 Arrays
    The variables we’ve used so far can store a single value. In this chapter, we’ll talk about a new type of variable capable of storing a number of values. This type is called array. An array may be multidimensional. We’ll focus on the simplest and most usual kinds: the one-dimensional and two-dimensional arrays. To introduce you to arrays, we’ll show you how to use arrays of integers and floating-point numbers. We’ll discuss other types of arrays, as well as their close relationship to pointers in later chapters. In Chapter 12 , we’ll describe some popular algorithms for searching a value in an array and sorting its elements.

    One-Dimensional Arrays

    An array is a data structure that contains a number of values, or else elements, of the same type. Each element can be accessed by its position within the array.

    Declaring Arrays

    To declare a one-dimensional array, we must specify its name, the number of its elements, and its data type, like this:
    data_type array_name[number_of_elements];
    The number_of_elements , or else the length of the array, is specified by an integer constant expression greater than 0 enclosed in brackets. A popular error is to use a variable whose value is set during the program execution. No, it is not allowed. All the elements are of the same type; it may be of any type (e.g.,
    int
    ,
    float
    ,
    char
    , …). For example, the statement:
    int arr[1000];
    declares the array arr with 1000 elements of type int . To realize the importance of arrays, imagine that if C didn’t support this type of aggregate variable, we’d have to declare 1000 different integer variables.
    The length of an array cannot change during the program execution. It remains fixed.
    If the length of the array is used several times in the program, a good practice is to use a macro instead. If you ever need to change it, you just change the macro. For example:
    #define SIZE 150 float
  • Learn Programming with C
    eBook - ePub

    Learn Programming with C

    An Easy Step-by-Step Self-Practice Book for Learning C

    • Sazzad M.S. Imran, Md Atiqur Rahman Ahad(Authors)
    • 2024(Publication Date)
    CHAPTER 3 Arrays and Pointers
    DOI: 10.1201/9781003302629-3
    IN C, AN ARRAY is used to hold multiple values of the same data type. The array could be one-dimensional, two-dimensional, or three-dimensional. On the other hand, pointer variables are used to hold addresses rather than values of variables. In terms of memory access, arrays and pointers are synonymous. The first element of an array is referred to by its name, whereas the value of a pointer points to any memory location.

    3.1 ARRAYS

    An array is a collection of data types that are all of the same kind. A single array variable can store, access, and handle a large amount of data. In the following C code, for example, int num[100];
    num is an integer type array with a total of 100 elements. Starting at 0 and continuing until n-1, each array element can be retrieved using appropriate indexing. The 4th, 5th, and 6th elements of the above num array, for example, are accessed with num[3], num[4], and num[5].
    We initialize the array in the same way that we would for any other data type variable.
    1. While declaration of the array: int num[4] = {5, 10, 9, 19}; or, int num[] = {5, 10, 9, 19};
    2. After declaration of the array: int num[4]; num[4] = {5, 10, 9, 19}; or, num[] = {5, 10, 9, 19};
    3. Interactively by the user after running the program. In that case, we usually use any of the C loops to access each element of the array. Example: int i, num[4]; for (i=0; i<4; i++) scanf(“%d”, &num[i]);
    When any array is declared, the array is organized such that all the array elements occupy contiguous space in memory. If the size of the integer data type is 4 bytes, then the memory organization of the array num[4] is as follows:

    3.2 2D ARRAYS

    A two-dimensional array is an array of arrays. It is also known as a matrix in C and is defined as a set of rows and columns. The following expression represents an integer type 2D array of matrix 2 × 4.
  • The Art of Algorithm Design
    • Sachi Nandan Mohanty, Pabitra Kumar Tripathy, Suneeta Satpathy(Authors)
    • 2021(Publication Date)
    1

    Fundamental Concepts of Data Structure

    DOI: 10.1201/9781003093886-1

    1.1   Array

    Whenever we want to store some values, we have to take the help of a variable, and for this, we have to declare it before its use. For example, if we want to store the details of a student, we have to declare the variables as
    • char name [20], add [30];
    • int roll, age, regdno;
    • float total, avg;
      • etc…
        • for an individual student.
    If we want to store the details of more than one student, then we have to declare a huge number of variables that would increase the length of the program and create difficulty in its access. Therefore, it is better to declare the variables in a group, i.e., the name variable will be used for more than one student, roll variable will be used for more than one student, etc.
    Therefore, to declare the variables of the same kind in a group is known as an Array. The concept of array can be used for storing details of more than one student or other objects.
    • Definition : The array is a collection of more than one element of the same kind with a single variable name.
    • Types of Arrays: The arrays can be further classified into two broad categories:
      • One-dimensional (the array with one boundary specification)
      • Multidimensional (the array with more than one boundary specification)
    • One-Dimensional Array
    Declaration: Syntax:
    • Storage_class Data type variable_name[bound];
    The data type may be one of the data types that we have studied. The variable name is also the same as the normal variable_name, but the bound is the number that will further specify the number of variables that you want to combine into a single unit.
    Storage_class is optional. By default, it is auto. Ex: int roll[15]; In the above example, roll is an array of 15 variables that can store the roll_number of 15 students. In addition, the individual variables are
    • roll[0], roll[1], roll[2], roll[3], …, roll[14]

    1.1.1   Array Element in Memory

    The array elements are stored in consecutive memory locations, i.e. the array elements get allocated memory sequentially.
  • ANSI C Programming
    eBook - ePub

    ANSI C Programming

    Learn ANSI C step by step

    Can we not refer to the same element using pointer notation, the way we did in one- dimensional arrays? Answer is yes. Only the procedure is slightly difficult to understand. So, read on…

    Pointers and 2-Dimensional Arrays

    The C language embodies an unusual but powerful capability—it can treat parts of arrays as arrays. More specifically, each row of a two-dimensional array can be thought of as a one-dimensional array. This is a very important fact if we wish to access array elements of a two-dimensional array using pointers.
    Thus, the declaration, int s[5][2];
    can be thought of as setting up an array of 5 elements, each of which is a one-dimensional array containing 2 integers. We refer to an element of a one-dimensional array using a single subscript. Similarly, if we can imagine s to be a one-dimensional array, then we can refer to its zeroth element as s[0] , the next element as s[1] and so on. More specifically, s[0] gives the address of the zeroth one-dimensional array, s[1] gives the address of the first one-dimensional array and so on. This fact can be demonstrated by the following program.
    And here is the output… Address of 0 th 1-D array = 65508 Address of 1 th 1-D array = 65512 Address of 2 th 1-D array = 65516 Address of 3 th 1-D array = 65520
    Let’s figure out how the program works. The compiler knows that s is an array containing 4 one-dimensional arrays, each containing 2 integers. Each one-dimensional array occupies 4 bytes (two bytes for each integer). These one-dimensional arrays are placed linearly (zeroth 1-D array followed by first 1-D array, etc.). Hence, each one-dimensional array starts 4 bytes further along than the last one, as can be seen in the memory map of the array shown in Figure 10.6 .
    Figure 10.6
    We know that the expressions s[0] and s[1] would yield the addresses of the zeroth and first one-dimensional array respectively. From Figure 10.6 these addresses turn out to be 65508 and 65512.
    Now, we have been able to reach each one-dimensional array. What remains is to be able to refer to individual elements of a one-dimensional array. Suppose we want to refer to the element s[2][1] using pointers. We know (from the above program) that s[2] would give the address 65516, the address of the second one-dimensional array. Obviously (65516 + 1) would give the address 65518. Or (s[2] + 1) would give the address 65518. And the value at this address can be obtained by using the value at address operator, saying *(s[2] + 1) . But, we have already studied while learning one-dimensional arrays that num[i] is same as*(num + i) . Similarly, *(s[2] + 1) is same as, *(*(s + 2) + 1)
  • Data Structures and Program Design Using Java
    eBook - ePub
    3

    ARRAYS

    3.1  Introduction

    In the previous chapter we studied the basics of programming in data structures and Java in which we aimed to design good programs, where a good program refers to a program which runs correctly and efficiently by occupying less space in the memory, and also takes less time to run and execute. Undoubtedly, a program is said to be efficient when it executes with less memory space and also in minimal time. In this chapter, we will learn about the concept of arrays. An array is a user-defined data type that stores related information together. Arrays are discussed in detail in the following sections.

    3.2  Definition of an Array

    An array is a collection of homogeneous (similar) types of data elements in contiguous memory. An array is a linear data structure, because all elements of the array are stored in linear order. Let us take an example in which we have ten students in a class, and we have been asked to store the marks of all ten students; then we need a data structure known as an array.
    Figure 3.1. Representation of an array of ten elements.
    In the previous example, the data elements are stored in the successive memory locations and are identified by an index number (also known as the subscript), that is, Ai or A[i]. A subscript is an ordinal number which is used to identify an element of the array. The elements of an array have the same data type, and each element in an array can be accessed using the same name.
     

    Frequently Asked Questions

    1. What is an array? How can we identify an element in the array?
    Ans: An array is a collection of homogeneous (similar) types of data elements in contiguous memory. An element in an array can be identified by its index number, which is also known as a subscript.

    3.3  Array Declaration

    We know that all variables must be declared before they are used in the program. Therefore, the same concept also holds with array variables. An array must be declared before it is used. During the declaration of an array, the size of the array has to be specified. Declaring an array involves the following specifications:
  • Data Structure and Algorithms Using C++
    eBook - ePub

    Data Structure and Algorithms Using C++

    A Practical Implementation

    • Sachi Nandan Mohanty, Pabitra Kumar Tripathy(Authors)
    • 2021(Publication Date)
    • Wiley-Scrivener
      (Publisher)
    2 Review of Concepts of ‘C++’

    2.1 Array

    Whenever we want to store some values then we have to take the help of a variable, and for this we must have to declare it before its use. If we want to store the details of a student so for this purpose we have to declare the variables as
    char name [20], add[30] ;int roll, age, regdno ;float total, avg ;    etc……        for a individual student.
    If we want to store the details of more than one student than we have to declare a huge amount of variables and which are too much difficult to access it. I.e/ the programs length will increased too faster. So it will be better to declare the variables in a group. I.e/ name variable will be used for more than one student, roll variable will be used for more than one student, etc.
    So to declare the variable of same kind in a group is known as the Array and the concept of array is used for this purpose only.
    Definition: The array is a collection of more than one element of same kind with a single variable name.
    Types of Array:
    The arrays can be further classified into two broad categories such as:
    • One Dimensional (The array having one boundary specification)
    • Multi dimensional (The array having more than one boundary specification)

    2.1.1 One-Dimensional Array

    Declaration:
    Syntax :
    Data type variable_name[bound] ;
    The data type may be one of the data types that we are studied. The variable name is also same as the normal variable_name but the bound is the number which will further specify that how much variables you want to combine into a single unit.
    Ex : int roll[15];
    In the above example roll is an array 15 variables whose capacity is to store the roll_number of 15 students.
    And the individual variables are     roll[0] , roll[1], roll[2], roll[3] ,.................,roll[14]
    Array Element in Memory
    The array elements are stored in a consecutive manner inside the memory. i.e./ They allocate a sequential memory allocation.
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.