1 / 33

310201 Fundamental Programming

310201 Fundamental Programming. Introduction to Arrays. Arrays. in this class we introduce the last substantial topic in the course the idea is quite simple – to begin, let’s go back to the beginning…. Variables. write “Number of marks in exam ==> “ read NbrMarks

bmaxwell
Download Presentation

310201 Fundamental Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 310201Fundamental Programming Introduction to Arrays

  2. Arrays • in this class we introduce the last substantial topic in the course • the idea is quite simple – to begin, let’s go back to the beginning…

  3. Variables write “Number of marks in exam ==> “ read NbrMarks write “Student’s mark ==> “ read StudentMark set Percentage to 100 * StudentMark / NbrMarks write “ Student’s percentage: “ write Percentage this design uses 3 variables: NbrMarks, StudentMark, Percentage

  4. Variables write “Number of marks in exam ==> “ read NbrMarks write “Student’s mark ==> “ read StudentMark set Percentage to 100 * StudentMark / NbrMarks write “ Student’s percentage: “ write Percentage this design uses 3 variables: NbrMarks, StudentMark, Percentage

  5. Variables write “Number of marks in exam ==> “ read NbrMarks write “Student’s mark ==> “ read StudentMark set Percentage to 100 * StudentMark / NbrMarks write “ Student’s percentage: “ write Percentage this design uses 3 variables: NbrMarks, StudentMark, Percentage

  6. Variables • each variable can hold a single value • each variable is associated with a single memory location • after running this program with the following inputs...

  7. Programs Number of marks in exam ==> 50 Student's mark ==> 30 Student's percentage: 60

  8. Variables • after running this program with the previous inputs... • the memory locations used by this program hold the following values... NbrMarks: 50 StudentMark: 30 Percentage: 60 • each variable is associated with one memory location and can hold one value

  9. Arrays • an array is a variable that can hold more than one value • An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. • an array called Marks might hold the collection of marks from an exam • we refer to values in an array using square brackets and a position number of the value of interest: Marks[1] Marks[17] Marks[123]

  10. Arrays • in some languages, the first value in an array is at position 1 • however, in C++, the first value in an array is at position 0 • in pseudocode examples, we will adopt this 0-based convention Marks[0]  first value held in Marks Marks[1]  second value held in Marks Marks[2]  third value held in Marks

  11. Arrays • to display the first three values in Marks, we can write (in pseudocode): writeMarks[0] writeMarks[1] writeMarks[2] • we’ll look at arrays in C++ in detail later – to output array values we write: cout << Marks[0]; cout << Marks[1]; cout << Marks[2];

  12. Arrays • we assign values to an array using the same notation: readMarks[0] (read value and assign as first in Marks) setMarks[5] =0(assign sixth value in Marks to 0) • no surprises here in C++: cin >> Marks[0]; Marks[5] = 0;

  13. Arrays • the power of arrays comes from the ability to refer to values stored in an array using a variable ( display first mark ) setMarkNbr=0 write Marks[MarkNbr] ( display second mark ) setMarkNbr=1 write Marks[MarkNbr]

  14. Activity • if Marks holds the values shown on the right, what output will the following design produce? setNbrMarks=3 setMarkNbr=0 whileMarkNbr < NbrMarks write”Mark number “, MarkNbr, ” = “ write Marks[MarkNbr] writeNewLine setMarkNbr=MarkNbr +1 ( end of loop over marks ) Marks [0] [1] [2] 17 29 8

  15. Activity Break

  16. Activity Feedback • if Marks holds the values shown on the right, this design will produce the following output? Mark number 0 = 17 Mark number 1 = 29 Mark number 2 = 8 Marks [0] [1] [2] 17 29 8 Marks 17 29 8 [1] [2] [0]

  17. Array References • below, a variable is used to refer to the values of interest in an array read Mark[StudentNbr] set Percentages[StudentNbr] to Percentage • a useful convenience is that we can use an expression to refer to the values of interest in an array set Mark[StudentNbr] to Mark[StudentNbr + 1] • why is this useful?...

  18. Array References • Because we can now do something like for (i=0; i<5; i++) { cout << “Mark for next student? “; cin >> mark[i] }

  19. Implementing Arrays in C++ • let’s have a look at the basics of using arrays in C++ - we’ll look at: • declaring arrays • initialising arrays • we’ve already seen examples of how arrays are used in C++, more follow… • in the next class we’ll learn some more details about using arrays in C++

  20. Declaring Arrays in C++ • no surprises in declaring arrays – below we declare an array to hold 10 integers: int Results[10]; • as mentioned, our ten integers are held in positions 0 to 9: Results[0] Results[1] Results[2] : Results[9]

  21. Declaring Arrays in C++ • a constant is sometimes used to hold the number of values in an array const int NBR_RESULTS = 10; int Results[NBR_RESULTS]; • a constant is useful when looping, and also makes it easy to change the array size: for (ResultNbr = 0; ResultNbr < NBR_RESULTS; ResultNbr++) cout << “Result[“ << ResultNbr << “]: “ Results[ResultNbr]

  22. Declaring Arrays in C++ • take care with loop conditions – one common form is: for (ResultNbr = 0; ResultNbr < NBR_RESULTS; ResultNbr++) : • an alternative form is: for (ResultNbr = 0; ResultNbr <= NBR_RESULTS - 1; ResultNbr++) :

  23. Initialising Arrays in C++ • to initialise values in an array, enclose the initial values in braces: const NBR_RESULTS = 3; int Results[NBR_RESULTS] = {0,1,2}; • you can initialise all values in an array of integers to zero using: int Results[NBR_RESULTS] = {0}; the default initial value for an array of integers is zero – if you do not provide enough initial values, others are set to zero!

  24. Debugging • to debug looping logic effectively, you need to know how to set watches and to step through a loop

  25. More on Initialising Arrays • if the array size is omitted when it is declared, the compiler canautomatically determine the size from initial values int Primes[] = {1, 3, 5, 7, 11}; • is equivalent to int Primes[5] = {1, 3, 5, 7, 11};

  26. More on Referencing Arrays • we normally use an integer literal, or an integer variable, to refer to a value of interest in an array Results[0] Results[ResultNbr] • also, an integer expression can be used Results[CurrentPos - 1]

  27. A Common Error Using Arrays • a common error when using arrays is to refer to an element that does not exist • that is: to use an index value that is outside the acceptable range...

  28. Activity • can you see how this code refers to an array element that does not exist? const NBR_RESULTS = 3; int Results[NBR_RESULTS] = {0}; for (int ResultNbr = 0; ResultNbr <= NBR_RESULTS; ResultNbr++) cout << Results[ResultNbr];

  29. Activity Break

  30. Activity Feedback • in the last trip through the loop below, the code refers to Results[3], which does not exist const NBR_RESULTS = 3; int Results[NBR_RESULTS] = {0}; for (int ResultNbr = 0; ResultNbr <= NBR_RESULTS; ResultNbr++) cout << Results[ResultNbr];

  31. A Common Error Using Arrays • C++ does notgive an error when using an index value that is out-of-range • it is the programmer’s responsibility to ensure that array references are always within the acceptable range • if, in an expression, you refer to an array element that does not exist: • results are unpredictable – the program may give erroneous output, or go into an infinite loop

  32. A Common Error Using Arrays • if you assign a value to an array elementthat does not exists: • with luck, you’ll get a memory violation error – so you know there’s a problem • without luck, results are unpredictable: • erroneous output produced • infinite loops lock-up the program • memory used by the operating system is overwritten causing a system crash

  33. Summary • simple variables hold a single value • arrays hold a collection of related values • first value in a C++ array is at position 0 • arrays can be defined for any of the basic data types – char, int, double, float

More Related