Homework Assignment – Array Technical Document Write a technical document that describes the structure and use of arrays. The document should be 3 to 5 pages and include an Introduction section, giving a brief synopsis of the document and arrays, a Body section, describing arrays and giving an annotated example of their use as a programming construct, and a conclusion to revisit important information about arrays described in the Body of the document. Some suggested material to include: Declaring arrays of various types Array pointers Printing and processing arrays Sorting and searching arrays Multidimensional arrays Indexing arrays of various dimension Array representation in memory by data type Passing arrays as arguments If you find any useful images on the Internet, you can use them as long as you cite the source in end notes. Solution Array is a collection of variables of the same type that are referenced by a common name. Specific elements or variables in the array are accessed by means of index into the array. If taking about C, In C all arrays consist of contiguous memory locations. The lowest address corresponds to the first element in the array while the largest address corresponds to the last element in the array. C supports both single and multi-dimensional arrays. 1) Single Dimension Arrays:- Syntax:- type var_name[size]; where type is the type of each element in the array, var_name is any valid identifier, and size is the number of elements in the array which has to be a constant value. *Array always use zero as index to first element. The valid indices for array above are 0 .. 4, i.e. 0 .. number of elements - 1 For Example :- To load an array with values 0 .. 99 int x[100] ; int i ;
for ( i = 0; i < 100; i++ ) x[i] = i ; To determine to size of an array at run time the sizeof operator is used. This returns the size in bytes of its argument. The name of the array is given as the operand size_of_array = sizeof ( array_name ) ; 2) Initialisg array:- Arrays can be initialised at time of declaration in the following manner. type array[ size ] = { value list }; For Example :- int i[5] = {1, 2, 3, 4, 5 } ; i[0] = 1, i[1] = 2, etc. The size specification in the declaration may be omitted which causes the compiler to count the number of elements in the value list and allocate appropriate storage. For Example :- int i[ ] = { 1, 2, 3, 4, 5 } ; 3) Multidimensional array:- Multidimensional arrays of any dimension are possible in C but in practice only two or three dimensional arrays are workable. The most common multidimensional array is a two dimensional array for example the computer display, board games, a mathematical matrix etc. Syntax :type name [ rows ] [ columns ] ; For Example :- 2D array of dimension 2 X 3. int d[ 2 ] [ 3 ] ; A two dimensional array is actually an array of arrays, in the above case an array of two integer arrays (the rows) each with three elements, and is stored row-wise in memory. For Example :- Program to fill in a 2D array with numbers 1 to 6 and to print it out row-wise. #include void main( ) { int i, j, num[2][3] ; for ( i = 0; i < 2; i++ ) for ( j = 0; j < 3; j ++ ) num[i][j] = i * 3 + j + 1 ; for ( i = 0; i < 2; i++ ) { for ( j = 0; j < 3; j ++ ) printf("%d ",num[i][j] ) ; printf(" " );
} 4) Array of Strings:- Array of strings is in fact a two dimensional array of characters but it is more useful to view this as an array of individual single dimension character arrays or strings. For Example :- char str_array[ 10 ] [ 30 ] ; where the row index is used to access the individual row strings and where the column index is the size of each string, thus str_array is an array of 10 strings each with a maximum size of 29 characters leaving one extra for the terminating null character. For Example :- Program to read strings into str_array and print them out character by character. #include char str_array[10][30] ; void main() { int i, j ; puts("Enter ten strings ") ; for ( i = 0 ; i < 10; i++ ) // read in as strings so a single for loop suffices { printf( " %d : ", i + 1) ; gets( str_array[i] ) ; } for ( i = 0; i < 10; i++ )//printed out as individual chars so a { // nested for loop structure is required for ( j=0; str_array[i][j] != '0' ; j++ ) putchar ( str_array[i][j] ) ; putchar( ' ' ) ; } } } 5) Arrays as arguments to functions :- In C it is impossible to pass an entire array as an argument to a function -- instead the address of the array is passed as a parameter to the function. The name of an array without any index is the address of the first element of the array and hence of the whole array as it is stored contiguously. However we need to know the size of the array in the function - either by passing an extra parameter or by using the sizeof operator.. For Example :-
void main() { int array[20] ; func1( array ) ;/* passes pointer to array to func1 */ } In the function receiving the array the formal parameters can be declared in one of three almost equivalent ways as follows :- func1 ( int x[10] ) { } func1 ( int x[ ] ) { } func1 ( int *x ) { } 6) Passing Multidimensional Arrays :- Function calls with multi-dimensional arrays will be the same as with single dimension arrays as we will still only pass the address of the first element of the array. However to declare the formal parameters to the function we need to specify all but one of the dimensions of the array so that it may be indexed properly in the function. For Example :- 2D array of doubles :- double x[10][20] ; Call func1 with x a parameter :- func1( x ) ; Declaration in func1 :- func1( double y[ ][20] ) { } 7) Pointers and Arrays:- There is a very close relationship between pointer and array notation in C. As we have seen already the name of an array ( or string ) is actually the address in memory of the array and so it is essentially a constant pointer. For Example :- char str[80], *ptr ; ptr = str ;/* causes ptr to point to start of string str */ ptr = &str[0] ; /* this performs the same as above */ It is illegal however to do the following str = ptr ; /* illegal */ as str is a constant pointer and so its value i.e. the address it holds cannot be changed. Instead of using the normal method of accessing array elements using an index we can use pointers in much the same way to access them as follows.
char str[80], *ptr , ch; ptr = str ; // position the pointer appropriately *ptr = 'a' ; // access first element i.e. str[0] ch = *( ptr + 1 ) ; // access second element i.e. str[1] Thus *( array + index ) is equivalent to array[index]. 8) Arrays of Pointer:- It is possible to declare arrays of pointers in C the same as any other 'type'. For example int *x[10] ; declares an array of ten integer pointers. To make one of the pointers point to a variable one might do the following. x[ 2 ] = &var ; To access the value pointed to by x[ 2 ] we would do the following *x[ 2 ] which simply de-references the pointer x[ 2 ] using the * operator. Passing this array to a function can be done by treating it the same as a normal array which happens to be an array of elements of type int *. 9) Searching and sorting in array:- a) Sequential search :- To search the array sequentially, we may use the algorithm Algorithm:- int function SequentialSearch (Array A, int Lb, int Ub, int Key); begin for i = Lb to Ub do if A(i) = Key then return i; return b) Binary Search:- If the data is sorted, a binary search is usefull Algorithm:- int function BinarySearch (Array A, int Lb, int Ub, int Key); begin do forever M = (Lb + Ub)/2; if (Key < A[M]) then Ub = M - 1; else if (Key > A[M]) then Lb = M + 1; else return M; if (Lb > Ub) then return -1;
end; 9) Sorting:- Sorting Summary Selection Sort The idea behind selection sort is: The approach is as follows: Note that after i iterations, A[0] through A[i-1] contain their final values (so after N iterations, A[0] through A[N-1] contain their final values and we're done!) Here's the code for selection sort: Insertion Sort The idea behind insertion sort is: Merge Sort As mentioned above, merge sort takes time O(N log N), which is quite a bit better than the two O(N2) sorts described above (for example, when N=1,000,000, N2=1,000,000,000,000, and N log2 N = 20,000,000; i.e., N2 is 50,000 times larger than N log N!). The key insight behind merge sort is that it is possible to merge two sorted arrays, each containing N/2 items to form one sorted array containing N items in time O(N). To do this merge, you just step through the two arrays, always choosing the smaller of the two values to put into the final array Quick Sort Quick sort (like merge sort) is a divide and conquer algorithm: it works by creating two problems of half size, solving them recursively, then combining the solutions to the small problems to get a solution to the original problem. However, quick sort does more work than merge sort in the "divide" part, and is thus able to avoid doing any work at all in the "combine" part! The idea is to start by partitioning the array: putting all small values in the left half and putting all large values in the right half. Hope I tryed to covered most of your points you can pickup this points and just write neet and clean document with propere headings.array[0]12loc 1000array[1]-345loc 1004array[2]342loc 1008array[3]-3000loc 1012array[4]23455loc 1016

Homework Assignment – Array Technical DocumentWrite a technical .pdf

  • 1.
    Homework Assignment –Array Technical Document Write a technical document that describes the structure and use of arrays. The document should be 3 to 5 pages and include an Introduction section, giving a brief synopsis of the document and arrays, a Body section, describing arrays and giving an annotated example of their use as a programming construct, and a conclusion to revisit important information about arrays described in the Body of the document. Some suggested material to include: Declaring arrays of various types Array pointers Printing and processing arrays Sorting and searching arrays Multidimensional arrays Indexing arrays of various dimension Array representation in memory by data type Passing arrays as arguments If you find any useful images on the Internet, you can use them as long as you cite the source in end notes. Solution Array is a collection of variables of the same type that are referenced by a common name. Specific elements or variables in the array are accessed by means of index into the array. If taking about C, In C all arrays consist of contiguous memory locations. The lowest address corresponds to the first element in the array while the largest address corresponds to the last element in the array. C supports both single and multi-dimensional arrays. 1) Single Dimension Arrays:- Syntax:- type var_name[size]; where type is the type of each element in the array, var_name is any valid identifier, and size is the number of elements in the array which has to be a constant value. *Array always use zero as index to first element. The valid indices for array above are 0 .. 4, i.e. 0 .. number of elements - 1 For Example :- To load an array with values 0 .. 99 int x[100] ; int i ;
  • 2.
    for ( i= 0; i < 100; i++ ) x[i] = i ; To determine to size of an array at run time the sizeof operator is used. This returns the size in bytes of its argument. The name of the array is given as the operand size_of_array = sizeof ( array_name ) ; 2) Initialisg array:- Arrays can be initialised at time of declaration in the following manner. type array[ size ] = { value list }; For Example :- int i[5] = {1, 2, 3, 4, 5 } ; i[0] = 1, i[1] = 2, etc. The size specification in the declaration may be omitted which causes the compiler to count the number of elements in the value list and allocate appropriate storage. For Example :- int i[ ] = { 1, 2, 3, 4, 5 } ; 3) Multidimensional array:- Multidimensional arrays of any dimension are possible in C but in practice only two or three dimensional arrays are workable. The most common multidimensional array is a two dimensional array for example the computer display, board games, a mathematical matrix etc. Syntax :type name [ rows ] [ columns ] ; For Example :- 2D array of dimension 2 X 3. int d[ 2 ] [ 3 ] ; A two dimensional array is actually an array of arrays, in the above case an array of two integer arrays (the rows) each with three elements, and is stored row-wise in memory. For Example :- Program to fill in a 2D array with numbers 1 to 6 and to print it out row-wise. #include void main( ) { int i, j, num[2][3] ; for ( i = 0; i < 2; i++ ) for ( j = 0; j < 3; j ++ ) num[i][j] = i * 3 + j + 1 ; for ( i = 0; i < 2; i++ ) { for ( j = 0; j < 3; j ++ ) printf("%d ",num[i][j] ) ; printf(" " );
  • 3.
    } 4) Array ofStrings:- Array of strings is in fact a two dimensional array of characters but it is more useful to view this as an array of individual single dimension character arrays or strings. For Example :- char str_array[ 10 ] [ 30 ] ; where the row index is used to access the individual row strings and where the column index is the size of each string, thus str_array is an array of 10 strings each with a maximum size of 29 characters leaving one extra for the terminating null character. For Example :- Program to read strings into str_array and print them out character by character. #include char str_array[10][30] ; void main() { int i, j ; puts("Enter ten strings ") ; for ( i = 0 ; i < 10; i++ ) // read in as strings so a single for loop suffices { printf( " %d : ", i + 1) ; gets( str_array[i] ) ; } for ( i = 0; i < 10; i++ )//printed out as individual chars so a { // nested for loop structure is required for ( j=0; str_array[i][j] != '0' ; j++ ) putchar ( str_array[i][j] ) ; putchar( ' ' ) ; } } } 5) Arrays as arguments to functions :- In C it is impossible to pass an entire array as an argument to a function -- instead the address of the array is passed as a parameter to the function. The name of an array without any index is the address of the first element of the array and hence of the whole array as it is stored contiguously. However we need to know the size of the array in the function - either by passing an extra parameter or by using the sizeof operator.. For Example :-
  • 4.
    void main() { int array[20]; func1( array ) ;/* passes pointer to array to func1 */ } In the function receiving the array the formal parameters can be declared in one of three almost equivalent ways as follows :- func1 ( int x[10] ) { } func1 ( int x[ ] ) { } func1 ( int *x ) { } 6) Passing Multidimensional Arrays :- Function calls with multi-dimensional arrays will be the same as with single dimension arrays as we will still only pass the address of the first element of the array. However to declare the formal parameters to the function we need to specify all but one of the dimensions of the array so that it may be indexed properly in the function. For Example :- 2D array of doubles :- double x[10][20] ; Call func1 with x a parameter :- func1( x ) ; Declaration in func1 :- func1( double y[ ][20] ) { } 7) Pointers and Arrays:- There is a very close relationship between pointer and array notation in C. As we have seen already the name of an array ( or string ) is actually the address in memory of the array and so it is essentially a constant pointer. For Example :- char str[80], *ptr ; ptr = str ;/* causes ptr to point to start of string str */ ptr = &str[0] ; /* this performs the same as above */ It is illegal however to do the following str = ptr ; /* illegal */ as str is a constant pointer and so its value i.e. the address it holds cannot be changed. Instead of using the normal method of accessing array elements using an index we can use pointers in much the same way to access them as follows.
  • 5.
    char str[80], *ptr, ch; ptr = str ; // position the pointer appropriately *ptr = 'a' ; // access first element i.e. str[0] ch = *( ptr + 1 ) ; // access second element i.e. str[1] Thus *( array + index ) is equivalent to array[index]. 8) Arrays of Pointer:- It is possible to declare arrays of pointers in C the same as any other 'type'. For example int *x[10] ; declares an array of ten integer pointers. To make one of the pointers point to a variable one might do the following. x[ 2 ] = &var ; To access the value pointed to by x[ 2 ] we would do the following *x[ 2 ] which simply de-references the pointer x[ 2 ] using the * operator. Passing this array to a function can be done by treating it the same as a normal array which happens to be an array of elements of type int *. 9) Searching and sorting in array:- a) Sequential search :- To search the array sequentially, we may use the algorithm Algorithm:- int function SequentialSearch (Array A, int Lb, int Ub, int Key); begin for i = Lb to Ub do if A(i) = Key then return i; return b) Binary Search:- If the data is sorted, a binary search is usefull Algorithm:- int function BinarySearch (Array A, int Lb, int Ub, int Key); begin do forever M = (Lb + Ub)/2; if (Key < A[M]) then Ub = M - 1; else if (Key > A[M]) then Lb = M + 1; else return M; if (Lb > Ub) then return -1;
  • 6.
    end; 9) Sorting:- Sorting Summary SelectionSort The idea behind selection sort is: The approach is as follows: Note that after i iterations, A[0] through A[i-1] contain their final values (so after N iterations, A[0] through A[N-1] contain their final values and we're done!) Here's the code for selection sort: Insertion Sort The idea behind insertion sort is: Merge Sort As mentioned above, merge sort takes time O(N log N), which is quite a bit better than the two O(N2) sorts described above (for example, when N=1,000,000, N2=1,000,000,000,000, and N log2 N = 20,000,000; i.e., N2 is 50,000 times larger than N log N!). The key insight behind merge sort is that it is possible to merge two sorted arrays, each containing N/2 items to form one sorted array containing N items in time O(N). To do this merge, you just step through the two arrays, always choosing the smaller of the two values to put into the final array Quick Sort Quick sort (like merge sort) is a divide and conquer algorithm: it works by creating two problems of half size, solving them recursively, then combining the solutions to the small problems to get a solution to the original problem. However, quick sort does more work than merge sort in the "divide" part, and is thus able to avoid doing any work at all in the "combine" part! The idea is to start by partitioning the array: putting all small values in the left half and putting all large values in the right half. Hope I tryed to covered most of your points you can pickup this points and just write neet and clean document with propere headings.array[0]12loc 1000array[1]-345loc 1004array[2]342loc 1008array[3]-3000loc 1012array[4]23455loc 1016