Simple Arrays COMP104 Lecture 11 / Slide 2 Arrays * An array is a collection of data elements that are of the same type (e.g., a collection of integers,characters,

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Passing Arrays to Functions Programming. COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 2 Passing Arrays as Parameters l Arrays are.
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
Data Structures Arrays and Structs Chapter The Array Data Type t Array elements have a common name –The array as a whole is referenced through.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT5: Array (1D and 2D) CS2311 Computer Programming.
C++ Basics Prof. Shermane Austin. Learning Programming Language Basics Data Types – simple Expressions Relational and Logical Operators Conditional Statements.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
Programming with Collections Collections in Java Using Arrays Week 9.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Arrays Slide 2 Arrays * An array is a collection of (homogeneous) data elements that are of the same type (e.g., a collection of integers,characters,
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
Arrays.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Chapter 7 Arrays. Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional Arrays.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Arrays and Containers Mechanism for representing lists.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 7 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 7 Arrays.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 6: Arrays 数据序列, 数组 1.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Arrays, Character Strings and Standard Type String ~ Collection.
CSIS 3401 Introduction to Data structures and algorithms.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Lecture 21: Arrays. 2 Lecture Contents: t Declaring and referencing arrays t Array subscripts t Using for/while loops for sequential access t Demo programs.
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
Arrays as Function Parameters. CSCE 1062 Outline  Passing an array argument (section 9.3)  Reading part of an array (section 9.4)  Searching and sorting.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Prof. Amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays.
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.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
L what is a void-function? l what is a boolean function? l is it possible for a function to have no parameters? l what is program stack? function frame?
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Basic Array Definition
C++ Arrays.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays.
Array Data Structure Chapter 6
Previous Lecture Review
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Arrays November 8, 2017.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Mechanism for representing lists
COMS 261 Computer Science I
Chapter 9: Data Structures: Arrays
Array Data Structure Chapter 6
COMS 261 Computer Science I
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

Simple Arrays

COMP104 Lecture 11 / Slide 2 Arrays * An array is a collection of data elements that are of the same type (e.g., a collection of integers,characters, doubles) * Arrays are like flats in a building, or post office boxes * Arrays provide a good way to name a collection, and to reference its individual elements.

COMP104 Lecture 11 / Slide 3 Array Applications * Given a list of test scores, determine the maximum and minimum scores. * Read in a list of student names and rearrange them in alphabetical order (sorting). * Given the height measurements of students in a class, output the names of those students who are taller than average.

COMP104 Lecture 11 / Slide 4 Array Declaration * Syntax: [ ]  The array elements are all values of the type  The size of the array is indicated by, the number of elements in the array  must be an int constant or a constant expression. Note that it is possible for an array to have multiple dimensions.

COMP104 Lecture 11 / Slide 5 Array Declaration Example // array of 10 uninitialized ints int A[10]; -- A

COMP104 Lecture 11 / Slide 6 Subscripting * Suppose int A[10]; // array of 10 ints  To access an individual element we must apply a subscript to list name A n A subscript is a bracketed expression  The expression in the brackets is known as the index n First element of list has index 0 A[0] n Second element of list has index 1, and so on A[1] n Last element has an index one less than the size of the list A[9] * Incorrect indexing is a common error

COMP104 Lecture 11 / Slide 7 Subscripting // array of 10 uninitialized ints int A[10]; A[3] = 1; int x = A[3]; -- 1 A A[4]A[5]A[6]A[3]A[0]A[2]A[8]A[9]A[7]A[1] 1 --

COMP104 Lecture 11 / Slide 8 Array Element Manipulation * Consider int A[10], i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; cin >> A[k]; // where the next input value is 3

COMP104 Lecture 11 / Slide 9 Array Initialization int A[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; A[3] = -1; A A Box A[3] 6

COMP104 Lecture 11 / Slide 10 Example Definitions  Suppose const int N = 20; const int M = 40; const int MaxStringSize = 80; const int MaxListSize = 1000; * Then the following are all legal array definitions. int A[10]; // array of 10 ints char B[MaxStringSize]; // array of 80 chars double C[M*N]; // array of 800 doubles int Values[MaxListSize];// array of 1000 ints

COMP104 Lecture 11 / Slide 11 2-D Array Example // 2-D array of 30 uninitialized ints int A[3][10]; -- A

COMP104 Lecture 11 / Slide 12 2-D Array References -- A // 2-D array of 30 uninitialized chars char A[3][10]; A[1][2] = ‘a’; char c; c = A[1][2]; --‘a’

COMP104 Lecture 11 / Slide 13 Inputting a List const int MaxSize = 10000; int A[MaxSize]; int n = 0; int CurrentInput; while(n >CurrentInput){ A[n] = CurrentInput; n++; }

COMP104 Lecture 11 / Slide 14 Displaying a List // List A of n elements has already been set int i; for (i=0; i<n; i++) cout << A[i] << " "; cout << endl;

COMP104 Lecture 11 / Slide 15 Smallest Value * Problem n Find the smallest value in a list of integers * Input n A list of integers and a value indicating the number of integers * Output n Smallest value in the list * Note n List remains unchanged after finding the smallest value!

COMP104 Lecture 11 / Slide 16 Smallest Value: Preliminary Design * Idea n When looking for smallest value, need a way of remembering best candidate found so far * Design n Search array looking for smallest value  Use a loop to consider each element in turn  If current element is smallest so far, then update smallest value so far n When done examining all of the elements, the smallest value seen so far is the smallest value

COMP104 Lecture 11 / Slide 17 const int N=10; int A[N]; int SmallestValueSoFar, i;... // A[] is input by user SmallestValueSoFar = A[0]; for (i=1; i<N; i++) if (A[i] < SmallestValueSoFar) SmallestValueSoFar = A[i]; Smallest Value