answersLogoWhite

0


Best Answer

When there is a need of storing a list of same type of large no. of data in linear manner,it is ridiculous to use large no. of different variables.It is more complex also.So in c and c++ array is used.
If the ans helps you,plz increase the trust point.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Arrays permit constant time random access to any data of the same type. An array is, specifically, one or more blocks of contiguous memory, such that every block holds the same number of elements of the same data type.

Constant time random access is achieved via simple pointer arithmetic Behind the Scenes. That is, each element is indexed as a zero-based offset from the memory address of the first element (at offset 0). Since all elements are intrinsically the same length (in bytes), each offset has a regular period or interval (in bytes). Thus element 5 will always be found at offset 4, the memory address immediately following the 4th element in the array or, to put it another way, <array_name>+(4*sizeof(<data_type>)).

However, one-dimensional arrays must be allocated as a single block of contiguous memory. If a large enough block is not available, the array cannot be instantiated. The only way around this is to break the array up into smaller, regular-sized blocks (each of which is itself a one-dimensional array), and use a separate array of pointers to point to each of these blocks. The array is then essentially a dynamic multi-dimensional array, but the same pointer arithmetic applies, albeit with an extra level of indirection.

While the purpose of an array is to permit fast random access to data of the same type, arrays are not ideal if the amount of data (the number of elements) is dynamic. That is, when every element in an array is consumed, the entire array must be reallocated to accommodate new elements. If it is possible to expand the array in situ, the cost is minimal. However, more often than not, the entire array must be copied to a new, larger block of memory. This requires more than double the memory of the original array until the copy is complete. While this is costly in terms of memory consumption and performance, the aforementioned multi-dimensional allocation technique would allow the array to expand and contract in smaller blocks. Thus only the pointer array itself need be reallocated. Note that shrinking an array to remove redundant elements also incurs a performance penalty, however no additional memory is required (the unused elements are simply released).

Where random access is not a priority, but the amount of data is dynamic, then linked lists are the best alternative to arrays. Linked lists allow constant time access to the first and/or last nodes in the list, but random access is linear time.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

An array is an extension of a variable. Whereas a variable only allows us to store one value at a time, an array allows us to store many values simultaneously.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the importance of using an array in C plus plus programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is an ordered list of data structure using c plus plus?

An ordered list of data in any programming language is simply a sorted array or list. In C++ this can either mean a sorted array, vector, list or forward list.


Why study c plus plus programming?

we are using c plus plus programming for developing object oriented programing software.


Is it possibly to return an array of strings in a function without using pointers in C plus plus?

No.


How do you draw a pixel of varying intensity on the screen using C Plus Plus?

C++ has nothing to do with pixels. Your question may be related with 'Windows programming' or 'DOS programming' or 'X Window System programming'.


How to write a code for finding sum of left diagonals elements in an array using c plus plus?

truzi i Ghal


In C plus plus is it possible to instantiate an array without specifying a length?

No. You can declare a dynamic array without specifying a length, but in order to physically instantiate (either by using malloc or by using object-oriented construction) you must provide a length.


To develop a taj mahal using c-language programming in turbo c plus plus?

turbo c


Would you Write c plus plus program using array for Fibonacci number?

You can write a C++ fib pro using arrays but the problem is the prog becomes very complicated since u need to pass the next adding value in an array.....


Appending array elements using function in c plus plus programming?

#include&lt;iostream&gt; void append(std::vector&lt;int&gt;&amp; v, int i){ v.push_back(i); } int main() { std::vector&lt;int&gt; v; append( v, 100 ); // same as calling v.push_back(100); return(0); }


What is programming languages in c plus plus?

Programming in C++ means designing and writing a computer program using the C++ programming language. C++ is a high-level, machine-independent language which must be converted into machine-dependent code by a compiler.


What is the lowest subscript of an array in c plus plus?

The lowest subscript of an array in C, or C++ is 0.


What are the two major types of programming languages in c plus plus?

Object oriented programming and structured programming.