×

4-Dimensional Array in C/C++

A four-dimensional (4D) array is an array of three-dimensional (3D) arrays, or in other words we can say that a 4- dimensional array is an array of arrays of arrays of arrays. An array with more dimensions can hold more data, but it also makes managing and comprehending the array more challenging.

Syntax:

data_type array_name [i1] [i2] [i3] [i4] ……... [in];
       here i represent dimension, and n represents dimension size.

Example

Let’s take int student [4] [5] [6] [7], where 4-d array is the student and int is array integer type, by multiplying it can hold 4*5*6*7 which is equals to 840.

Another example is float country [5] [6] [5] [6] [5], where five-dimensional array is the country which can hold 5*6*5*6*5 which is equals to 4500.

Code:

include <iostream>
usingnamespacestd;
 
intmain()
{
    // indexes are used for the declaration of the variable
    inti, j, k, l, size;
 
    // declaration of an array
    inta[2][2][2][2];
 
    // array size                   
    size = 2;
 
    // elements for an input
    a[0][0][0][0] = 5;
    a[0][0][0][1] = 3;
    a[0][0][1][0] = 5;
    a[0][0][1][1] = 3;
    a[0][1][0][0] = 6;
    a[0][1][0][1] = 7;
    a[0][1][1][0] = 6;
    a[0][1][1][1] = 7;
    a[1][0][0][0] = 8;
    a[1][0][0][1] = 9;
    a[1][0][1][0] = 8;
    a[1][0][1][1] = 9;
    a[1][1][0][0] = 9;
    a[1][1][0][1] = 7;
    a[1][1][1][0] = 9;
    a[1][1][1][1] = 7;
 
    // values printing
    for(i = 0; i < size; i++) {
        for(j = 0; j < size; j++) {
            for(k = 0; k < size; k++) {
                for(l = 0; l < size; l++) {
                    cout << "Value of a["<< i << "]["<< j << "]["<< k << "]["<< l
                      << "]["<< l<< "] :- "<< a[i][j][k][l];
                    cout << "\n";
                }
            }
        }
    }
   
    return0;
}

Output:

4-Dimensional Array in C/C++

Multidimensional arrays in C++ are mostly used to perplex new users and cause them to wonder how to dynamically allocate them, how to delete them, or why they can't be converted to arrows to arrows.

Use

If we give input of 3 coordinates and 1 time, i.e., x, y, z, and t, and wish to determine whether there is a collision between two cars or not, we can store the data in a 4D array.

An array of 3Darrays makes up a 4-dimensional array.

Algorithm

  • Begin.
  • Make the variables known.
  • Indicate the array's components.
  • Enter the number of items.
  • Consider the components as input.
  • Print the array's stored elements.
  • End.

Example 1:

#include<iostream>
using namespace std;
int main() {
   int a [2][2][3][2];
   cout << "Enter the elements of array: \n";
   for(int i = 0; i < 2; ++i) {
      for (int j = 0; j < 2; ++j) {
         for(int k = 0; k < 3; ++k ) {
            for(int l = 0; l < 2; ++l ) {
               cin >> a[i][j][k][l];
            }
         }
      }
   }
   cout<<"\n array elements are stored as:"<<endl;
   for(int i = 0; i < 2; ++i) {
      for (int j = 0; j < 2; ++j) {
         for(int k = 0; k < 3; ++k) {
            for(int l = 0; l < 2; ++l) {
               cout << "a[" << i << "][" << j << "][" << k << "] [" <<l<<"]= " << a[i][j][k][l] << endl;
            }
         }
      }
   }
   return 0;
}

Output:

4-Dimensional Array in C/C++ 4-Dimensional Array in C/C++

Example 2:

//Code for printing the values which is stored by user


#include <stdio.h>
int main()
{
  int test[2][3][2];


  printf("Enter 12 values: \n");


  for (int i = 0; i < 2; ++i)
  {
    for (int j = 0; j < 3; ++j)
    {
      for (int k = 0; k < 2; ++k)
      {
        scanf("%d", &test[i][j][k]);
      }
    }
  }


  // putting values in print with the correct index


  printf("\nDisplaying values:\n");
  for (int i = 0; i < 2; ++i)
  {
    for (int j = 0; j < 3; ++j)
    {


      {
        printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
      }
    }
  }


  return 0;
}

Output:

4-Dimensional Array in C/C++ 4-Dimensional Array in C/C++

Inserting arrays

#include<stdio.h>
int i,j,k;                         //nested loops variables
int main ()
{
	int arr[2][3][3];              //declaration of an arrays
	printf("enter the values in the array: \n");
	for(i=1;i<=2;i++)              //representations of blocks
	{
		for(j=1;j<=3;j++)          //representations of rows
		{
			for(k=1;k<=3;k++)      //representation of columns
			{
				printf("the value at arr[%d][%d][%d]: ",i,j,k);
				scanf("%d",&arr[i][j][k]);
			}
		}
	}
	printf("printing the values in array: \n");
	for(i=1;i<=2;i++)
	{
		for(j=1;j<=3;j++)
		{
			for(k=1;k<=3;k++)
			{
				printf("%d ",arr[i][j][k]);
				if(k==3)
				{
					printf("\n");
				}
			}
		}
		printf("\n");
	}
	return 0;
}

Output:

4-Dimensional Array in C/C++ 4-Dimensional Array in C/C++

Related Topics

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

C++ History

C++ is a middle-level programming language developed in 1980s by Bjarne Stroustrup at Bel Labs. C ++ development initially started in 1979, four years before its launch. It is started with...

1 minute read.

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the...

3 minutes read.

C++ Mutable keyword

C++ mutable keyword Mutable class storage specifier in C++ Storage class specifiers in C are auto, register, static, and external. Typedef is also considered as a specifier of the storage...

4 minutes read.

Method overriding in C++

What is method overriding? Using the same function in derived class as their base class is referred to as function/method overriding in c++. Method overriding is an example of polymorphism. With...

2 minutes read.

Reverse function in C++

The function std::reverse() is included in the standard template library of C++. It takes in a beginning and ending iterator, reversing the order. To use the reverse statement, we need...

2 minutes read.

C++ For loop

C++ loop Statement C++ Loop statement allows us to repeat the execution of a statement or group of statements multiple times. The statement(s) repeat execution within loop until the condition of loop...

2 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

Learn C++ Tutorial

C++ Introduction C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories. It is superset (extension) of C programming language. Depending upon features supported by programming...

10 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

C++ Output Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

4 minutes read.

Naming Convention in C++

The first and most fundamental step a programmer takes to produce clean code is to name a file or a variable. This naming must be acceptable so that it serves...

5 minutes read.

Hexadecimal to Decimal in C++

In computers, hexadecimal numbers are represented with base 16 and decimal numbers are represented with base 10 and values 0-9, whereas hexadecimal numbers have digits ranging from 0 to 15,...

3 minutes read.

SET Data Structure in C++

Generally, in our home, we store food items in a fridge or kitchen efficiently to find them easily and use them. Similarly, in programming, we need to store the data...

6 minutes read.

Exception Handling in C++ vs Java

Nowadays, exception handling is a feature found in virtually all object-oriented languages. We can also find this type of feature in Java and C++. The try-catch and block is required...

3 minutes read.

Ways to Copy a Vector in C++

Vectors in C++ are the same as arrays, along with additional outstanding features than them, like array lists in Java programming language. In Vectors, the size constraint is eliminated, which...

5 minutes read.

C++ Program to Implement Merge Sort

C++ Program to Implement Merge Sort The technique of merge sort is based on the strategy of divide and conquer. We divide the set of while data into smaller bits, arranged...

3 minutes read.

Single Handling in C++

Introduction: Single handling in C++ refers to a technique for processing multiple events or requests with a single function or handler rather than creating separate functions for each task. This allows...

5 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.