Found 7404 Articles for C++

4 Dimensional Array in C/C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

636 Views

A 4 dimensional array is an array of 3Darrays.AlgorithmBegin. Declare the variables. Declare the array elements. Take the no of elements as input. Take the elements as input. Print the elements stored in array. End.Here is an example of 4D array.#include using namespace std; int main() {    int a[2][2][3][2];    cout > a[i][j][k][l];             }          }       }    }    cout

Why C/C++ array index starts from zero?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

539 Views

As Array index starts with 0, so a[i] can be implemented as *(a + i).If Array index starts with 1 then a[i] will be implemented as *(a+i-1) which will be time consuming during compilation and the performance of the program will also be effected.So, it is better to start index of the array from 0.A simple program of array is given -Example Codeint main() {    int array[5] = {7, 7, 7, 6, 6};    for (int i = 0; i < 5; i++)       cout

An Uncommon representation of array elements in C/C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

69 Views

This is a simple C++ program of an uncommon representation of array elements.#include using namespace std; int main() { int array[5] = {7,7,7, 6, 6}; for (int i = 0; i < 5; i++) cout

Short hand array notation in C/C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

284 Views

If there are repeated values are present in C then we use shorthand array notation to define that array.Here is an example:Example Code#include int main() { int array[10] = {[0 ... 3]7, [4 ... 5]6,[6 ... 9]2}; for (int i = 0; i < 10; i++) printf("%d ", array[i]); return 0; }Output7 7 7 7 6 6 2 2 2 2In this program,int array[10] = {[0 ... 3]7, [4 ... 5]6,[6 ... 9]2}is similar as,int array[10] = {7, 7, 7, 7, 6, 6, 2, 2, 2, 2}.If there is any gap in the middle of the array it will be filled up by 0.In C++ above program will give the same output but it will give warnings with the output.

How to create a dynamic array of integers in C++ using the new keyword

Nitya Raut
Updated on 30-Jul-2019 22:30:25

9K+ Views

In C++, a dynamic array can be created using new keyword and can be deleted it by using delete keyword.Let us consider a simple example of it.Example Code Live Demo#include using namespace std; int main() {    int i,n;    cout

Passing Arrays to Function in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

205 Views

C++ does not allow to pass an entire array as an argument to a function. However, you can pass a pointer to an array by specifying the array's name without an index.If you want to pass a single-dimension array as an argument in a function, you would have to declare function formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received.Method-1Formal parameters as a pointer as follows −void myFunction(int *param) {    .    .    . }Method-2Formal parameters as ... Read More

How to initialize a dynamic array in C++?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

2K+ Views

Here is a C++ program to initialize a dynamic array. Here in this program we can use dynamically allocated array to return a local array from the function Array().Example Code Live Demo#include using namespace std; int* Array() {    int* a = new int[100];    a[0] = 7;    a[1] = 6;    a[2] = 4;    a[3] = 5;    return a; } int main() {    int* p = Array();    cout

How to use std::sort to sort an array in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

8K+ Views

In programming language, sorting is a basic function which is applied to data to arrange these data is ascending or descending data. In C++ program, there is a function std::sort() for sorting the array.sort(start address, end address)Here,Start address => The first address of the element. Last address => The address of the next contiguous location of the last element of the array.Example Code Live Demo#include #include using namespace std; void display(int a[]) {    for(int i = 0; i < 5; ++i)    cout

How to pass an array by reference in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

6K+ Views

If we pass the address of an array while calling a function, then this is called function call by reference. The function declaration should have a pointer as a parameter to receive the passed address, when we pass an address as an argument.Example Code Live Demo#include using namespace std; void show( int *num) {    cout

Find size of array in C/C++ without using sizeof

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

4K+ Views

In this program, we find out Find size of array in C/C++ without using sizeof.AlgorithmBegin Initialize the elements of the array. &a => This is the pointer to array which points at the same memory address as a. &a + 1 => It points at the address after the end of the array. *(a+1) => Dereferencing to *(&a + 1) gives the address after the end of the last element. *(a+1)-a => Subtract the pointer to the first element to get the length of the ... Read More

Advertisements