Download presentation
Presentation is loading. Please wait.
Published byNathan Jonathan Carter Modified over 9 years ago
1
“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??
2
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 2357111317192329313741434753 0123456789101112131415 Offset/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 Address 01,000 11,002 21,004 31,006 41,008 51,010 61,012 71,014 81,016 91,018 101,020 111,022 121,024 Notice also that the first offset is always 0 (which is why our formula works) How do we know this??
3
Arrays and pointers #include "stdafx.h" #include 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
4
Arrays Your output should look like
5
Arrays and Pointers We also previously noted that we could pass an array to another function, but we could pass a pointer #include "stdafx.h" #include using namespace std; // function prototype for printarray void 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
6
Arrays and Pointers Your output should appear as follows:
7
Multidimensional Arrays Arrays 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: Offset01 024 139 2525 3749 411121 513169 617289 719361
8
Arrays Enter, compile and run the following code #include 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:
9
Arrays The addresses given are in Hexadecimal Decimal0123456789101112131415 Hex0123456789ABCDEF Dec. 2FF8D83143896 2FF8DC3143900 2FF8E03143904 2FF8E43143908 2FF8E83143912 2FF8EC3143916 2FF8F03143920 2FF8F43143924 2FF8F83143928 2FF8FC3143932 The corresponding decimal values are: Offsets01 031438963143900 131439043143908 231439123143916 331439203143924 431439283143932 The array elements would be stored at:
10
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)
11
Strings Let’s first do it the hard way. Enter the following code: #include "stdafx.h" #include 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; }
12
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.
13
Strings The 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
14
Strings What 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
15
Strings Fortunately for us, there are easier ways to enter and print out strings. #include "stdafx.h" #include 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)
16
Strings Because 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)http://www.cplusplus.com/reference/cstring/ Let’s look at some of the more common ones (You must use the precompiler header: #include ) Enter the following code: #include "stdafx.h" #include 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"; }
17
Strings The 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
18
Strings PassOffsetChar1Char2Equal? 10AAY 21ppY 32ppY 43llY 54eeY 65\0sN #include "stdafx.h" #include 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:
19
Strings The 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
20
Strings void 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:
21
Strings A 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[] 733276111118101326743 0ILoveC++\0 string2[] 733276111118101326734
22
Strings
23
Strings
24
Strings
25
Strings
26
Strings
27
Strings
28
Strings
29
Strings
30
Strings
31
Strings
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.