





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An introduction to arrays in C++ programming language. It explains how to declare an array, access its elements, and implement selection sort algorithm. The document also covers the concept of indices and their relationship with array positions.
What you will learn
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!
An array is a sequence of consecutive memory locations, of the same type, any one of which can be referenced via a common pointer to the first entry in the sequence using an index. This is an implementation description and not the description of an abstract data type.
Most common way:
which is a constant integer expression.
int list[5]; float x[100]; double y_coord[100]; char ch[1000]; These only declare the array; i.e., reserve the memory. None of the individual memory locations has been accessed, and no values have been assigned to array entries.
Consider the example int list[5];
list[0]=2; Put 2 in first location.
list[1]=8; Put 8 in second location.
list[2]=10; Put 10 in third location.
list[3]=100; Put 100 in fourth location.
list[4]=300; Put 300 in fifth and last location.
In the previous examples the quantity inside the [ ] is called the index. What do you notice about the index and the location of the array referenced?
When referencing an array, the index of the array position is always one less than the sequence number of a location in the array. In other words, 0 references the first location, 1 the second, and so forth. So, what can the largest index of an array position be?
This is a larger array, and we likely would not want to fill it up by typing 100 assignment statements. For each i in the correct range, suppose we want y_coord[i] to have the double value of i. for(i=0;i<100;i++) y_coord[i]=static_cast
Would we need the cast operator in this case?
int list1[5] = {1,2,3,4,5}; int list2[5] = {0}; int list3[5] = {1,2,3}; int list4[ ] = {6,2,7,9,10}; int list5[ ] = {1,2,3,4,5,6,7}; What happens with each of these declarations?
int list6[5] = {1,2,3,4,5,6,7};
This is a syntax error, because the right hand side of = has more elements to insert than the size of the array declared on the left hand side.
Consider
It can be any integer expression whose computed value falls within the legal range of the array indices.
If you declare an array, the name of that array is an array type, which can be thought of as a constant pointer whose value is the address of the first location in the array. int list[5]; Think oflist as a pointer. Its value is the address of the first of the five integers in the array sequence. It is constant in the sense that it cannot be changed; i.e., you can’t assign another address or value tolist.
When you then writelist[i] your are “de- referencing” the array and this means “the value of the(i+1)st location in the array”. The quantityi is the index and indicates how many locations to “skip over” to reach the specified location in the array. It merely adds i times the array type size to the address stored inlist to find the starting byte of the specified array element.
What is the dominating term in this last expression? Based on that, what happens to the total work when N is doubled?
When multiplied out, the previous expression becomes:
Suppose that instead of finding the MxInd within the body of the loop we desired to do that with a function. We would like to have: MxInd = IndexOfMax(A,i); Note we are passing the array name and the last index in the array we want to consider. The function will find the index of the maximum value in A[0....i].
int IndexOfMax(int X[], int last) { int i; int Mxi = 0; for(i=0; i<=last; i++) if(X[i]>X[Mxi]) Mxi = i; return Mxi; }
int IndexOfMax(int X[], int last) Note the return type; the parameter corresponding to the array type; and the parameterlast which provides the function with information regarding how far to go in addressing the array elements. Note also that in X[ ] the array size is not specified. No effect would take place by putting a number inside [ ], rather that information is passed through the variablelast.
int MxInd; //MxInd, index to //current max val int i; for(i=N-1; i>0; i--){ MxInd=IndexOfMax(A,i); swap(A[i],A[MxInd]); }
any code details, rather just the statement:
SelectSort(A,N-1); //sort //A[0..N-1]