SlideShare a Scribd company logo
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

More Related Content

PPT
Basics of Data structure using C describing basics concepts
PPTX
Chapter 13.pptx
PPT
Arrays
PDF
Array&amp;string
PPTX
Unit4pptx__2024_11_ 11_10_16_09.pptx
PDF
Cunit3.pdf
PPTX
C (PPS)Programming for problem solving.pptx
PPT
Array THE DATA STRUCTURE. ITS THE STRUCT
Basics of Data structure using C describing basics concepts
Chapter 13.pptx
Arrays
Array&amp;string
Unit4pptx__2024_11_ 11_10_16_09.pptx
Cunit3.pdf
C (PPS)Programming for problem solving.pptx
Array THE DATA STRUCTURE. ITS THE STRUCT

Similar to Homework Assignment – Array Technical DocumentWrite a technical .pdf (20)

PPTX
Module_3_Arrays - Updated.pptx............
DOC
Arrays and Strings
PDF
Array in C full basic explanation
PPTX
3.ArraysandPointers.pptx
PPTX
ppt on arrays in c programming language.pptx
PPTX
Arrays & Strings
PPTX
Array 2 hina
PPT
C programming , array 2020
PDF
Arrays-Computer programming
PPTX
Arrays 1D and 2D , and multi dimensional
PPTX
Arrays
PDF
Unit ii data structure-converted
PPTX
PPTX
unit 2.pptx
PPTX
Arrays basics
PPT
Arrays Basics
PDF
PDF
Arrays
PPT
Module_3_Arrays - Updated.pptx............
Arrays and Strings
Array in C full basic explanation
3.ArraysandPointers.pptx
ppt on arrays in c programming language.pptx
Arrays & Strings
Array 2 hina
C programming , array 2020
Arrays-Computer programming
Arrays 1D and 2D , and multi dimensional
Arrays
Unit ii data structure-converted
unit 2.pptx
Arrays basics
Arrays Basics
Arrays
Ad

More from aroraopticals15 (20)

PDF
For a given H0 and level of significance, if you reject the H0 for a.pdf
PDF
Find all elements of our ring R that have norm 1. Show that no elemen.pdf
PDF
Evaluate the decision to have a computer usage policy and the potent.pdf
PDF
(java) eclipse PleaseDevelop an application that implements a pro.pdf
PDF
At a sudden contraction in a pipe the diameter changes from D_1 to D_.pdf
PDF
Above is a trace depicting mechanical activity of frog heart. A stud.pdf
PDF
A gymnosperm, such as Juniperus virginiana, that produces female con.pdf
PDF
Write short descriptive answer to the following questions Discuss wh.pdf
PDF
Why should anyone else care about what I do with my sewage on my own .pdf
PDF
Why do viral particles, zymosan on fungi, endotoxin (LPS) from gram .pdf
PDF
Which of the following used historicalcomparative methods in their .pdf
PDF
Which of following is not a class of the phylum platyhelminthes Tre.pdf
PDF
What is wrong with this code Please fix.code#include stdio.h.pdf
PDF
What are two ways that meiosis could produce gametes that contai.pdf
PDF
what is the process in society that made this change to advertising .pdf
PDF
What is the best way to go about designing an Android AppSoluti.pdf
PDF
Use the following word bank to complete the numbers 51-100. Acoeloma.pdf
PDF
Two cards are randomly selected from a 52-card deck. What is the pro.pdf
PDF
Two labs are being compared to determine if they are providing the sa.pdf
PDF
Todopackage hwk6; This class contains the configuration of a t.pdf
For a given H0 and level of significance, if you reject the H0 for a.pdf
Find all elements of our ring R that have norm 1. Show that no elemen.pdf
Evaluate the decision to have a computer usage policy and the potent.pdf
(java) eclipse PleaseDevelop an application that implements a pro.pdf
At a sudden contraction in a pipe the diameter changes from D_1 to D_.pdf
Above is a trace depicting mechanical activity of frog heart. A stud.pdf
A gymnosperm, such as Juniperus virginiana, that produces female con.pdf
Write short descriptive answer to the following questions Discuss wh.pdf
Why should anyone else care about what I do with my sewage on my own .pdf
Why do viral particles, zymosan on fungi, endotoxin (LPS) from gram .pdf
Which of the following used historicalcomparative methods in their .pdf
Which of following is not a class of the phylum platyhelminthes Tre.pdf
What is wrong with this code Please fix.code#include stdio.h.pdf
What are two ways that meiosis could produce gametes that contai.pdf
what is the process in society that made this change to advertising .pdf
What is the best way to go about designing an Android AppSoluti.pdf
Use the following word bank to complete the numbers 51-100. Acoeloma.pdf
Two cards are randomly selected from a 52-card deck. What is the pro.pdf
Two labs are being compared to determine if they are providing the sa.pdf
Todopackage hwk6; This class contains the configuration of a t.pdf
Ad

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PDF
From loneliness to social connection charting
PPTX
Onica Farming 24rsclub profitable farm business
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Open folder Downloads.pdf yes yes ges yes
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Pre independence Education in Inndia.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Revamp in MTO Odoo 18 Inventory - Odoo Slides
From loneliness to social connection charting
Onica Farming 24rsclub profitable farm business
STATICS OF THE RIGID BODIES Hibbelers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Open Quiz Monsoon Mind Game Prelims.pptx
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Open folder Downloads.pdf yes yes ges yes
Pharmacology of Heart Failure /Pharmacotherapy of CHF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Cardiovascular Pharmacology for pharmacy students.pptx
Week 4 Term 3 Study Techniques revisited.pptx
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
O7-L3 Supply Chain Operations - ICLT Program
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Pre independence Education in Inndia.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester

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 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 :-
  • 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 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