×

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

Design Patterns in C++

A design pattern offers an all-encompassing, repeatable answer to the typical issues that arise in software design. Usually, the pattern demonstrates the connections and interactions between several classes or objects....

10 minutes read.

What does Buffer Flush mean in C++

A buffer flush, to explain simple layman's terms, is nothing but the transfer of computer data which is being stored in a rentable temporary memory of your computer running either...

3 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.

Factory Method for Designing Pattern in C++

In C++, the factory method is a type of conditional design pattern. The factory method is related to creating a new object in C++. With the help of a factory...

3 minutes read.

Single level Inheritance

Inheritance is a fundamental element of C++’s Object-Oriented Programming (OOP). It allows a class (called the derived class) to inherit characteristics and attributes from another class (called the base class)....

5 minutes read.

Computing index using Pointers Returned by STL Functions in C++

In this tutorial we will learn how to compute index using pointers which returned by STL functions in C++. Many built-in C++ functions return pointers to memory places that provide...

2 minutes read.

C++ Pointer

Pointer is a derived data type that stores the address of a variable. A pointer is used for memory management and dynamic memory allocation. Pointer works on the address of data rather than...

2 minutes read.

Include Guards in C++

In C++ programming, we frequently utilize a class more than once, so it is necessary to create a header file and include it in the main program. Now, occasionally a...

3 minutes read.

C++ Structs

We frequently encounter scenarios in which we must store a bunch of data, whether of comparable or dissimilar data kinds. Arrays are used to hold a group of data of...

6 minutes read.

C++ find missing in the second array

Given two arrays A and B of sizes n and m. Find the elements from array A that are not present in array B. Example Input : a[] = {1, 2, 3, 5,...

3 minutes read.

How to calculate size of string in C++

What is string in C++? In C++, a string is a sequence of characters. The string data type is part of the Standard Template Library (STL) and is defined in the...

4 minutes read.

C++ add two numbers using the function

Here we will learn how to add two numbers by creating function in C++. Let’s learn this with help of example. Code: - #include <iostream> using namespace std; int add_two_no(int a, int b); int main(){   int...

1 minute read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

Palindrome Number Program in C++

A palindrome number is one that is the same when it is reversed. Palindrome numbers include 22, 33, 44, 55, 66, 77, 88, and 99. Algorithm for Palindrome Numbers Get the user's...

4 minutes read.

Constructor Vs Destructor in C++

C++ Constructor A function Object () { [native code] } is a member function that shares the same name as the class. It is called automatically whenever a class object is...

3 minutes read.

C++ Overloading

C++ Overloading is a condition when two or more members have the same name with different parameter type or a different number of parameter. C++ overloading is two types: Function...

1 minute read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

Program to arrange an array in alternate positive and negative numbers

Let’s say, there is given an array arr, arrange the array in such a way that every positive number is followed by a negative number. If there are extra positive...

4 minutes read.

Desired Capabilities in Selenium Web Driver in C++

A class called Desired Capabilities is used to specify a set of fundamental requirements, such as browser, operating system, and version combinations.to carry out automated testing of a web application...

7 minutes read.

Private Inheritance in C++

Private inheritance is an inheritance in object-oriented programming (OOP) languages where a subclass derives from a superclass. Still, the derived class does not inherit the public and protected members of...

7 minutes read.