answersLogoWhite

0


Best Answer

All arrays are one dimensional; multi-dimensional arrays are implemented as one-dimensional arrays of one-dimensional arrays (for as many dimensions as required).

Arrays have many applications. The most common array type is the character array which we typically use to represent null-terminated strings. That is, we allocate sufficient storage to accommodate a string of characters plus a null-terminator. Standard ASCII strings use the char data type which is always 1 byte in length. The null-terminator is always the all-zeroes bit pattern which is the literal character '\0' (or ASCII character code 0). Wide strings typically use the wchar_t data type where the null-terminator is denoted by the literal character L'\0'.

Aside from strings, the most common reason to use an array is to be able to refer to a collection of values by address rather than by name. That is, each value in the array has an address and we can easily calculate that address knowing the start address of the array and the zero-based index of the element we wish to access:

int A[10] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};

int x = A[3]; // same as: int x = *(A + 3);

assert (x==7);

The conventional array suffix notation A[3] is nothing more than sugar-coating for pointer arithmetic. What the compiler actually "sees" is *(A+3). However, the index, 3, is variable so we can easily traverse an array of values knowing only the start address and the number of elements to traverse:

void f (int* A, unsigned elements) {

for (int i=0; i

A[i] = i*i; // equivalent to: *(A+i) = i*i;

}

Note that *(A+i) has the same runtime cost regardless of the value of i, thus we have constant-time, random-access to any element in the array.

All array elements other than the first element are anonymous; they have no names. While naming variables is useful in itself, a name can only refer to one value at most, but we don't always know how many variables we might need at runtime. Arrays allow us to store as many variables as required whenever we require them. Imagine naming 1000 variables individually! Then imagine how you would access them sequentially let alone randomly.

Arrays needn't exit solely in working memory, they can also exit on mass storage mediums such as a hard-disk drive. This greatly increases the storage capacity allowing us to store literally billions of variables in consecutive storage locations that can be retrieved (reasonably) quickly.

When we sort an array we can also take advantage of binary search. That is, we look at the middle element of the array. If that's not the value we're looking for, we compare the two values and this will tell us in which half of the array to look, effectively eliminating one half of the array. We then search the remaining half in the same way, starting at the middle element. We repeat the process until we locate the value in the middle of the remaining half or the remaining half has no elements in which case the value doesn't exist. In this way, searching an array of n elements takes O(log n) time instead of O(n) time.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the applications for single dimensional arrays in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How many types of sorting array in C programming?

You can sort an array with any method you want, but there is a built-in qsort function, declared in stdlib.h (see the attached link).bubble sort, quick sort, insertion sort, merge sort, radix sort and lot more..merge sort is the most efficient one..


What is Array in Programming C?

Arrays allow similar types of data to be stored within a contiguous block of memory such that every data element is accessible in constant time, regardless of its physical location within the array. This is achieved through simple pointer arithmetic treating each element as a memory offset from the start of the array. Since every element is the same length (in bytes), locating any element is simply a matter of calculating its offset from its index. Indices are zero-based thus the third element can be found at index 2. The memory offset for that element is therefore the product of the element size and 2. However, C permits indices to be specified directly, while the pointer arithmetic is done in the background. Thus array_name[2] automatically returns a reference to the third element. Arrays with large and complex variable length data elements need to store those elements separately from the array, usually non-contiguously. This is achieved by using a pointer array. Pointer arrays are particularly useful when sorting extremely large data lists as it is much easier and more efficient to implement a sorting algorithm with an array than it is with a linked list, particularly when constant-time random-access is essential to the algorithm. The time and effort in building the array is generally more than compensated for by the efficiency of the algorithm. Arrays can also be divided and subdivided to better model the data they represent. For instance, a chessboard might be implemented as a one-dimensional array of 64 elements, however it makes more sense to model the chessboard in a two-dimensional array of 8x8 elements. Although the array is still allocated contiguously and can be thought of as being 8 rows and 8 columns, it's actually better to think of this two-dimensional array as being a one-dimensional array of 8 elements, where each element is another one-dimensional array of 8 elements. By thinking this way it makes it possible to allocate extremely large arrays in non-contiguous memory (as completely separate one-dimensional arrays) and also makes comprehension of a four-dimensional array in a three-dimensional world that much easier (unless you actually want to model time and space of course). A four-dimensional array can be thought of in a variety of ways: as being a one dimensional array of three-dimensional arrays, or as a two-dimensional array of two-dimensional arrays, or as a three-dimensional array of one-dimensional arrays, or even as a one-dimensional array of one-dimensional arrays of one-dimensional arrays of one-dimensional arrays. Whichever method you use to imagine your array is immaterial, so long as it makes sense to you that's all that really matters.


How do you initialise a two dimentional array?

C provides rectangular multidimensional arrays. In C, a two-dimensional array is really a one-dimensional array, each of whose elements is an array. An array is initialized by a list of initializations in braces; each row of a two-dimensional array is initialized by a corresponding sub-list. Example of two dimensional array initialization: char array_example[2][4] = { {11, 12, 13, 14}, {21, 22, 23, 24} };


What is the purpose of using arrays in C language?

The purpose of using arrays in C is to store multiple values in one variable. Then you can make programs that use arrays like lists, printing values from multiple arrays into one line. It take memory in continues block then we can know memory location easily. We can retrieve data quickly.


How do you pass an array as a parameter?

AnswerUnfortunately your question is to broad. All progamming languages vary in the way things are done. I will just give a general way of doing it.You have to pass the multidimensional array into the function by including it with calling the function. On the receiving end you have to declare another multidimensional array so the information can be passed into it. Depending on the language, you may not be passing in a multidimensional array, instead that array may be stored in an object which you can pass instead.Hope this helps some.-Ashat-in C, when passing two dimensional arrays the compiler needs to know the width so it can calculate memory offsets.passing a 2d array of width 4:voidFunc(type array[][4]);

Related questions

How many types of arrays in c?

An array is simply a contiguous block of memory containing two or more elements. There are two types of array: a static array which is allocated on the stack at compile time; and a dynamic array which is allocated on the heap at runtime. Both can be one-dimensional or multi-dimensional. A one-dimensional array can be likened to a row (or column) of chessboard squares, with as many squares as required to store all the elements. A multi-dimensional array is any array with two or more dimensions. A two-dimensional array can be likened to the whole chessboard, where any square can be identified by its row and column index. However the dimensions needn't be equal. A two-dimensional array can also be imagined as a one-dimensional array where every element is simply another one-dimensional array. Three-dimensional arrays can be likened to a cube, or as a one-dimensional array of two-dimensional arrays. A four-dimensional array can be linked to a one-dimensional array of three-dimensional arrays, and so on. Although every one-dimensional array must be allocated in contiguous memory, multi-dimensional arrays can be dynamically allocated so that each dimension is itself a separately allocated one-dimensional array of pointers to the next dimension, making it possible to allocate extremely large arrays over a series of smaller allocations rather than as a single contiguous block.


What is the required syntax for creating C arrays?

The required syntax for creating C arrays include the brackets, array size, variety length arrays, codes like std:vector, classPTR, and many more to create C arrays.


How many types of sorting array in C programming?

You can sort an array with any method you want, but there is a built-in qsort function, declared in stdlib.h (see the attached link).bubble sort, quick sort, insertion sort, merge sort, radix sort and lot more..merge sort is the most efficient one..


What is Array in Programming C?

Arrays allow similar types of data to be stored within a contiguous block of memory such that every data element is accessible in constant time, regardless of its physical location within the array. This is achieved through simple pointer arithmetic treating each element as a memory offset from the start of the array. Since every element is the same length (in bytes), locating any element is simply a matter of calculating its offset from its index. Indices are zero-based thus the third element can be found at index 2. The memory offset for that element is therefore the product of the element size and 2. However, C permits indices to be specified directly, while the pointer arithmetic is done in the background. Thus array_name[2] automatically returns a reference to the third element. Arrays with large and complex variable length data elements need to store those elements separately from the array, usually non-contiguously. This is achieved by using a pointer array. Pointer arrays are particularly useful when sorting extremely large data lists as it is much easier and more efficient to implement a sorting algorithm with an array than it is with a linked list, particularly when constant-time random-access is essential to the algorithm. The time and effort in building the array is generally more than compensated for by the efficiency of the algorithm. Arrays can also be divided and subdivided to better model the data they represent. For instance, a chessboard might be implemented as a one-dimensional array of 64 elements, however it makes more sense to model the chessboard in a two-dimensional array of 8x8 elements. Although the array is still allocated contiguously and can be thought of as being 8 rows and 8 columns, it's actually better to think of this two-dimensional array as being a one-dimensional array of 8 elements, where each element is another one-dimensional array of 8 elements. By thinking this way it makes it possible to allocate extremely large arrays in non-contiguous memory (as completely separate one-dimensional arrays) and also makes comprehension of a four-dimensional array in a three-dimensional world that much easier (unless you actually want to model time and space of course). A four-dimensional array can be thought of in a variety of ways: as being a one dimensional array of three-dimensional arrays, or as a two-dimensional array of two-dimensional arrays, or as a three-dimensional array of one-dimensional arrays, or even as a one-dimensional array of one-dimensional arrays of one-dimensional arrays of one-dimensional arrays. Whichever method you use to imagine your array is immaterial, so long as it makes sense to you that's all that really matters.


How do you initialise a two dimentional array?

C provides rectangular multidimensional arrays. In C, a two-dimensional array is really a one-dimensional array, each of whose elements is an array. An array is initialized by a list of initializations in braces; each row of a two-dimensional array is initialized by a corresponding sub-list. Example of two dimensional array initialization: char array_example[2][4] = { {11, 12, 13, 14}, {21, 22, 23, 24} };


What is an arrays in c?

array is collection of many data


Give the syntax to declare two dimensional array using array of pointers?

It is not possible to declare a two-dimensional array using an array of pointers in any programming language, but many programming languages support declarations of N-dimensional arrays of pointers.The exact syntax varies with the programming language, and requires support for N-dimensional arrays and pointers. In C, the following declares an array of pointer variables, each implemented as pointer to the generic type "void":void* array_1D[10];The type of the expression array_1D is "void * const."The following example expands on the previous one by declaring a two-dimensional array of "void" pointers:void* array_2D[10][20];The type of the expression array_2D is "void ** const."The last example declares a 3-dimensional array of "void" pointers, which can be seen as a 2-dimensional array of arrays of pointers:void* array_3D[10][20][30];


How do you get the mean median and mode in c programming?

By writing in C code the mathematical methods for finding the mean, median and mode of your data taking into account how your data is stored (eg an array; two separate arrays one with data and the other with frequencies; a two dimensional array containing both data and frequencies; an array of structures containing the data instead of arrays; a linked list of structures; etc).


How do you access the elements of double dimension arrays?

Working with two-dimensional arrays is really no different to working with a one-dimensional array. A two-dimensional array is really just a one-dimensional array where every element is a one-dimensional array. int x[4]; Here, x is a one-dimensional array of 4 elements of type int. int y[3][4]; Here, y is two-dimensional, however it's actually a one-dimensional array of 3 elements where each element is exactly the same type as x; an array of 4 elements of type int. The biggest problem with multi-dimensional arrays is when we pass arrays into functions. An array implicitly decays to a pointer so we lose all information regarding the dimensions when we pass them to functions. Thus we have to pass the dimensions as separate arguments. However, a two-dimensional array does just decay to a point it decays to a pointer-to-pointer. That is, each dimension adds another level of indirection. The following example demonstrates how to pass a multi-dimensional array to a function: #include<stdio.h> void print2d (int* a, unsigned rows, unsigned cols) { for (unsigned r=0; r<rows; ++r) { for (unsigned c=0; c<cols; ++c) printf ("%d\t", a[r * cols + c]); printf ("\n"); } } int main() { int x[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; print2d (&x[0][0], 3, 4); return 0; } Note that we don't have this problem in C++ because we can use std::array and std::vector objects, which are much easier to work with and just as efficient as any built-in array.


What is the purpose of using arrays in C language?

The purpose of using arrays in C is to store multiple values in one variable. Then you can make programs that use arrays like lists, printing values from multiple arrays into one line. It take memory in continues block then we can know memory location easily. We can retrieve data quickly.


What are the differences between structures and arrays?

Arrays are collections of repeated data items. Structures are complex data items made up of other data items, including, potentially, other structures and arrays. You can, of course, also have arrays of structures. Array items can be accessed by using its subscript whereas structure items can be accessed using its dot or "arrow" operator in C, C++, C#, Java, and JavaScript.


What are the data type in c language?

Some of them are: 1. char, short, int, long, float, double 2. pointers to these 3. arrays of these 4. arrays of pointers 5. pointers to arrays ...