Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Arrays in C++: Declaration, Accessing Elements, and Selection Sort, Study notes of Computer Programming

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

  • How can you access the elements of an array in C++?
  • What is the difference between an index and a position in an array?
  • What is the common way to declare an array in C++?

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

ehaab
ehaab 🇺🇸

4.2

(32)

275 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Arrays
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.
Declaring an Array in C++
Most common way:
type arrayname[constant integer
expression];
type
is the common data type of all entries.
arrayname
is a user-defined indentifier.
[ ]
contains the size or length of the array
which is a constant integer expression.
Examples
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.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Arrays in C++: Declaration, Accessing Elements, and Selection Sort and more Study notes Computer Programming in PDF only on Docsity!

Arrays

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.

Declaring an Array in C++

Most common way:

type arrayname[constant integer

expression];

type is the common data type of all entries.

arrayname is a user-defined indentifier.

[ ] contains the size or length of the array

which is a constant integer expression.

Examples

„ 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.

Accessing Elements of the

Array

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.

Range of Indices

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?

Position vs. Index

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?

double y_coord[100];

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(i);

Would we need the cast operator in this case?

Alternate Declaration Methods

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?

A Syntax Error

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.

Valid Indices

Consider

list[ expression ] = computed value;

What can the indexexpression be?

It can be any integer expression whose computed value falls within the legal range of the array indices.

What is the Array Name?

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.

Accessing the Array Elements

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:

( N^2 − N )/ 2 = N^2 / 2 − N / 2

Passing an Array to a Function

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].

The Function Definition

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; }

Consider the Header

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.

Revised Code with Function

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]); }

Do it all with functions:

Suppose that inmain( ), we don’t want

any code details, rather just the statement:

SelectSort(A,N-1); //sort //A[0..N-1]