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

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 10.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
Input & Output: Console
C Programming Tutorial – Part I CS Introduction to Operating Systems.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
C/C++ Operators Binary Operators: Operators Between Two Operands: Operator + MeaningExample Definition. Additionx = 6 + 2;Add the values on either side.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
1 CSE 2341 Object Oriented Programming with C++ Note Set #2.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays.
POINTERS.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Pointers *, &, array similarities, functions, sizeof.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
Arrays and Strings Lecture 30. Summary of Previous Lecture In the previous lecture we have covered  Functions Prototypes Variable Scope  Pointers Introduction.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Strings Chapter 5.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
Variables and memory addresses
1 Object-Oriented Programming Using C++ A tutorial for pointers.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Pointers. The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte).
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Pointers and Arrays Dynamic Variables and Arrays.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Quiz 11/15/16 – C functions, arrays and strings
Numeric Arrays Numeric Arrays Chapter 4.
Your questions from last session
Arrays Arrays A few types Structures of related data items
CISC181 Introduction to Computer Science Dr
Presentation transcript:

“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers” 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??

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 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, , , ,024 Notice also that the first offset is always 0 (which is why our formula works) How do we know this??

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

Arrays Your output should look like

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

Arrays and Pointers Your output should appear as follows:

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

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:

Arrays The addresses given are in Hexadecimal Decimal Hex ABCDEF Dec. 2FF8D FF8DC FF8E FF8E FF8E FF8EC FF8F FF8F FF8F FF8FC The corresponding decimal values are: Offsets The array elements would be stored at:

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)

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

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.

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

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

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)

Strings Because we can perform a lot of different operations on strings there are a number of functions available to us (See for a more complete list) 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"; }

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

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:

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

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:

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[] ILoveC++\0 string2[]

Strings

Strings

Strings

Strings

Strings

Strings

Strings

Strings

Strings

Strings