ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP1112Multi Dimensional List…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP1112

    Programming Fundamentals

    Progress0 / 19 done
    Topics19
    1Introduction to Problem Solving2Von-Neumann Architecture3Introduction to Programming4Role of Compiler and Linker5Introduction to Algorithms6Basic Data Types and Variables7Input/Output Constructs8Arithmetic, Comparison and Logical Operators9Conditional Statements and Execution Flow10Repetitive Statements and Execution Flow11Lists and Memory Organization12Multi-dimensional Lists13Introduction to Modular Programming14Function Definition and Calling15Stack Rolling and Unrolling16Strings and String Operations17Pointers/References18Static and Dynamic Memory Allocation19File I/O Operations
    4 min read
    Topic 12 of 19
    Topic Study Note Illustration
    12

    Multi-dimensional Lists

    Multi-dimensional lists, often referred to as multi-dimensional arrays or matrices, allow you to store data in a structured format that can be accessed via multiple indices. This...

    Multi-dimensional Lists in C++

    Multi-dimensional lists, often referred to as multi-dimensional arrays or matrices, allow you to store data in a structured format that can be accessed via multiple indices. This is useful in various applications, such as mathematical computations, graphics, and data representation.

    1. Definition of Multi-dimensional Arrays

    A multi-dimensional array is essentially an array of arrays. The most common type is the two-dimensional array, which can be visualized as a matrix with rows and columns. You can also have three-dimensional arrays and higher dimensions, although they are less common.

    2. Two-Dimensional Arrays

    Declaration: You can declare a two-dimensional array using the following syntax:

    dataType arrayName[rows][columns];
    

    Example:

    #include <iostream>
    using namespace std;
    
    int main() {
        const int rows = 3;
        const int columns = 4;
        int matrix[rows][columns] = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        };
    
        // Display the matrix
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                cout << matrix[i][j] << " ";
            }
            cout << endl;
        }
    
        return 0;
    }
    

    3. Memory Organization

    • Two-dimensional arrays are stored in row-major order. This means that the elements of each row are stored in contiguous memory locations.
    • The memory layout of a two-dimensional array can be visualized as follows:
    Matrix (3x4):
      1  2  3  4
      5  6  7  8
      9 10 11 12
    

    In memory, this would be stored sequentially as:

    1 2 3 4 5 6 7 8 9 10 11 12
    

    4. Three-Dimensional Arrays

    You can also create three-dimensional arrays, which can be visualized as an array of matrices.

    Declaration:

    dataType arrayName[depth][rows][columns];
    

    Example:

    #include <iostream>
    using namespace std;
    
    int main() {
        const int depth = 2;
        const int rows = 3;
        const int columns = 4;
        int threeD[depth][rows][columns] = {
            {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12}
            },
            {
                {13, 14, 15, 16},
                {17, 18, 19, 20},
                {21, 22, 23, 24}
            }
        };
    
        // Display the 3D array
        for (int d = 0; d < depth; d++) {
            cout << "Layer " << d + 1 << ":" << endl;
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    cout << threeD[d][i][j] << " ";
                }
                cout << endl;
            }
            cout << endl;
        }
    
        return 0;
    }
    

    5. Higher-Dimensional Arrays

    Higher-dimensional arrays follow the same principles as two- and three-dimensional arrays, with additional indices for each dimension. However, the complexity increases, and they are less frequently used in practice.

    Example of a Four-Dimensional Array:

    int fourD[2][3][4][5]; // A 4D array
    

    6. Dynamic Multi-dimensional Arrays

    You can also create dynamic multi-dimensional arrays using pointers and new. This approach provides more flexibility, especially when the dimensions are not known at compile time.

    Example:

    #include <iostream>
    using namespace std;
    
    int main() {
        int rows = 3;
        int columns = 4;
    
        // Create a dynamic 2D array
        int** matrix = new int*[rows];
        for (int i = 0; i < rows; i++) {
            matrix[i] = new int[columns];
        }
    
        // Initialize and display the matrix
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                matrix[i][j] = i * columns + j + 1;
                cout << matrix[i][j] << " ";
            }
            cout << endl;
        }
    
        // Cleanup
        for (int i = 0; i < rows; i++) {
            delete[] matrix[i];
        }
        delete[] matrix;
    
        return 0;
    }
    

    Summary

    Multi-dimensional lists (arrays) are powerful tools for organizing data in a structured manner. They allow for efficient storage and access, especially in mathematical and graphical applications. By understanding the declaration, memory organization, and both static and dynamic approaches to multi-dimensional arrays, you can effectively utilize them in your C++ programs.

    ← Previous topic 11
    Lists and Memory Organization
    Next topic 13 →
    Introduction to Modular Programming

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    731words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals