“in mathematics and computer programming, index notation is used to specify the elements of an...

35
“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers” http://en.wikipedia.org/wiki/Index_notation Arrays All arrays contain elements of the same basic data type (regardless of basic data type) and must be stored at contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. We have already used a numeric array (our decimal to binary conversion) Notice that the first element in C/C++ always has the subscript (index value) zero Why must all of the elements in an Array be of the same basic data type?? Why must they be stored in contiguous memory?? Why must the first element in the array have the subscript zero??

Upload: nathan-jonathan-carter

Post on 02-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers” http://en.wikipedia.org/wiki/Index_notation

Arrays

All arrays contain elements of the same basic data type (regardless of basic data type) and must be stored at contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

We have already used a numeric array (our decimal to binary conversion)

Notice that the first element in C/C++ always has the subscript (index value) zero

Why must all of the elements in an Array be of the same basic data type??

Why must they be stored in contiguous memory??

Why must the first element in the array have the subscript zero??

Page 2: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Arrays

short primenos {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};

Consider the following array declaration

The array has 16 elements and will be stored as

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Offset/Index

• If the base address of array primenos is 1,000 the address of each element in the array is 1,000 + (2 * offset/index)

We know that each element requires 16-bits (2 bytes) of storage. Therefore:

Offset Address0 1,0001 1,0022 1,0043 1,0064 1,0085 1,0106 1,0127 1,0148 1,0169 1,018

10 1,02011 1,02212 1,024

Notice also that the first offset is always 0 (which is why our formula works)

How do we know this??

Page 3: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Arrays and pointers

#include "stdafx.h"#include <iostream>using namespace std;void main(){ int primenos[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53}; int i; int * mypointer;

mypointer = &primenos[0]; printf("The base address is%ld\n\n", mypointer);

for (i=0; i <15; i++) { cout << "the value at primenos[" << i << "] is " << primenos[i]; mypointer = &primenos[i]; printf(" It is stored at %ld\n", mypointer); }}

Enter, compile and run the following code

Page 4: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

ArraysYour output should look like

Page 5: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Arrays and PointersWe also previously noted that we could pass an array to another function, but we could pass a pointer

#include "stdafx.h"#include <iostream>using namespace std;// function prototype for printarrayvoid printarray(int nvals, int *parray [ ]);

void main(){

// declare and initialize the array variable int i,*arrayPtr[7], primenos[7]={2,3,5,7,11,13,17}; //Store the array addresses for(i=0; i<7; i++) arrayPtr[i] = &primenos[i]; // call to function printarray, pass along the pointer to the 1st array element printarray(7, arrayPtr);}

void printarray(int nvals, int *parray [ ]){

short count; for (count=0; count < nvals; count++) {

cout << "the value at offset [" << count << "] is " << *parray [count]; printf(" It is stored at %ld\n", &parray[count]); }}

• Enter, compile and execute the following code

Page 6: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Arrays and PointersYour output should appear as follows:

Page 7: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Multidimensional ArraysArrays of course can be multidimensional (think spreadsheet)

• Consider a 2-dimensional array, where the first column holds a prime number, and the second column contains the square of the first column:

Offset 0 10 2 41 3 92 5 253 7 494 11 1215 13 1696 17 2897 19 361

Page 8: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

ArraysEnter, compile and run the following code

#include <iostream>using namespace std;void main(){ int i, primenos[5][2] = {{2,1}, {3,1}, {5,1}, {7,1}, {11,1}};

for (i=0; i <5; i++) { primenos[i][1] = primenos[i][0] * primenos[i][0]; cout << "primenos[" << i << "][0] = " << primenos[i][0] << " at address "<< &primenos[i][0] << endl; cout << "primenos[" << i << "][1] = " << primenos[i][1] << " at address "<< &primenos[i][1] << endl; }}

The output should appear as:

Page 9: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

ArraysThe addresses given are in Hexadecimal

Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F

Hex Dec.2FF8D8 31438962FF8DC 31439002FF8E0 31439042FF8E4 31439082FF8E8 31439122FF8EC 31439162FF8F0 31439202FF8F4 31439242FF8F8 31439282FF8FC 3143932

The corresponding decimal values are:

Offsets 0 10 3143896 31439001 3143904 31439082 3143912 31439163 3143920 31439244 3143928 3143932

The array elements would be stored at:

Page 10: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Strings• Strings are arrays of data type char, but are treated slightly differently.

Consider the following sentence:

The quality of mercy is not strained; It droppeth as the gentle rain from heaven upon the place beneath.

Quickly – How many characters are in the sentence (including spaces and the semi-colon and the period)??

(There are 107 – I think)

• The point is that how many characters a string contains is not something we are interested in.

(We are concerned only about where it begins and where it ends)

• The beginning is easy: as with any array, it is the base address of the array

(We will talk about where it ends in a little while)

Page 11: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Strings• Let’s first do it the hard way. Enter the following code:

#include "stdafx.h"#include <iostream>using namespace std;

void main(){

short i;char string1[11];string1[0] ='H';string1[1] ='e';string1[2] ='l';string1[3] ='l';string1[4] ='o';string1[5] =' ';string1[6] ='W';string1[7] ='o';string1[8] ='r';string1[9] ='l';string1[10] ='d';for (i=0; i<11; i++)

cout << string1[i];cout << endl;}

Page 12: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Strings• Not surprisingly, the output is:

• This program works because we knew exactly how many characters were in this string (11)

• However, we initially stated that we didn’t want to constantly count all of the characters in a string.

Page 13: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

StringsThe null character '\0' is used to terminate C strings

Let’s modify our previous code

void main(){

char string1[12];string1[0] ='H';string1[1] ='e';string1[2] ='l';string1[3] ='l';string1[4] ='o';string1[5] =' ';string1[6] ='W';string1[7] ='o';string1[8] ='r';string1[9] ='l';string1[10] ='d';string1[11] ='\0';

printf("%s\n",string1);}

Changes??

1 additional character needed (‘\0’)

(The output will look the same)

Add the null character (‘\0’)

Use the %s printf token

Page 14: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

StringsWhat happens if we forget to add the null character???

Try it. Delete the string1[11] ='\0'; command line (or comment it out)

Your output should appear something like:

Why???

When we first started talking about strings, we said we know where the string starts (at the base address of the string) AND where it ends

• Without the null character, we don’t know where it ends

Page 15: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

StringsFortunately for us, there are easier ways to enter and print out strings.

#include "stdafx.h"#include <iostream>using namespace std;

void main(){

char string1[] = “Hello World!!";puts (string1);// You could use the command: cout << string1 << endl;}

(The output will look the same – Try it for yourself)

Page 16: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

StringsBecause we can perform a lot of different operations on strings there are a number of functions available to us

(See http://www.cplusplus.com/reference/cstring/ for a more complete list)

Let’s look at some of the more common ones

(You must use the precompiler header: #include <string>)

Enter the following code:

#include "stdafx.h"#include <string>#include <iostream>using namespace std;

void main(){char string[] = "The quality of mercy is not strained; It droppeth as the gentle rain from heaven upon the place beneath.";int schars = strlen(string);puts (string);cout << "There are " << schars <<" Characters in the string\n";}

Page 17: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

StringsThe output would appear as:

Another commonly used function is strcmp (which compares 2 strings to find out if they are equal (or not)).

Consider the two strings: char string1[] = “Apple“, string2[] = “Apple“;

To compare the strings, we need to compare them letter by letter in a loop

Consider the programming logic on the following slide

Page 18: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

StringsPass Offset Char1 Char2 Equal?

1 0 A A Y2 1 p p Y3 2 p p Y4 3 l l Y5 4 e e Y6 5 \0 s N

#include "stdafx.h"#include <string>#include <iostream>using namespace std;

void main(){

char string1[] = "Apple", string2[] = "Apples";cout << "The strings " << string1 << " and " << string2 << " are " << strcmp(string1, string2) << endl;}

Using function strcmp makes our life much easier. Consider (enter, compile and run) the following code:

Page 19: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

StringsThe output would appear as:

Typically, most T/F functions return the value 0 (zero) if the result is true.

In this function (and others) if the first string (address) is less than (alphabetically) the second, the function returns the value -1 (i.e., they were the same until the -1 comparisons to go)

We could clean up the program a little with the following code

Page 20: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Stringsvoid main(){

char string1[] = "Apple", string2[] = "Apples";cout << "The strings " << string1 << " and " << string2 << " are ";if (strcmp (string1,string2) == 0) cout << "Equal"; else cout << "Not equal";cout << endl;}

The output would appear as:

Page 21: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

StringsA similar problem occurs when we try to copy one string to another.

char string1[] = "I Love C++", string2[];

Consider the variable declarations:

Where we want to copy the contents of string1 into string2

We know these have to be copied character by character:

string1[]

73 32 76 111 118 101 32 67 43 43 0

I L o v e C + + \0

string2[]

73 32 76 111 118 101 32 67 34 34 34

Page 22: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Strings

Page 23: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Strings

Page 24: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Strings

Page 25: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Strings

Page 26: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Strings

Page 27: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Strings

Page 28: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Strings

Page 29: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Strings

Page 30: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Strings

Page 31: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”

Strings

Page 32: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
Page 33: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
Page 34: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
Page 35: “In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”