Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 12: Arrays.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Introduction to C Programming
Arrays.
The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
One Dimensional Arrays
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 16: Classes and Objects.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 13: Pass-by-Value?!?
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Arrays Chapter 6.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 9: Pass-by-Value.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
Introduction to Programming with C++ Fourth Edition
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Introduction to Computers and Programming Class 21 Arrays Professor Avi Rosenfeld.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 Pearson Education, Inc. All rights reserved Arrays.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino.
 2006 Pearson Education, Inc. All rights reserved 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.
Chapter 8 Arrays and Strings
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
7 Arrays.
Introduction To Programming Information Technology , 1’st Semester
CS2011 Introduction to Programming I Arrays (I)
7 Arrays.
JavaScript: Arrays.
C Programming Pointers
Presentation transcript:

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 12: Arrays

In This Lesson We Will: Introduce arrays Introduce arrays Demonstrate how to declare and create arrays Demonstrate how to declare and create arrays

Topic Arrays

Arrays An array is a list of items An array is a list of items Usually associated with each other Usually associated with each other Of the same data type (though there are ways around this) Of the same data type (though there are ways around this) Declare arrays by adding square brackets to the declaration statement: Declare arrays by adding square brackets to the declaration statement: // Variables int grades[NUM_STUDENTS];

Worth Noting For now we will declare the number of elements in the array at the same time we declare the variable For now we will declare the number of elements in the array at the same time we declare the variable Number of elements is often a constant Number of elements is often a constant Declaration can take other forms Declaration can take other forms In advanced C++ classes you will learn about pointers; you may come to prefer that approach In advanced C++ classes you will learn about pointers; you may come to prefer that approach

Referencing Array Elements Access elements using index operator: [] Access elements using index operator: [] Example: Example: grades[5] = 80; Indices run from 0 to one less than the number of elements Indices run from 0 to one less than the number of elements

Array Initialization Often initialize with a for loop: Often initialize with a for loop: // Variables int grades[NUM_OF_STUDENTS]; for (int i = 0; i < NUM_OF_STUDENTS; i++) grades[i] = 0;

Array Initialization Also possible to initialize arrays explicitly Also possible to initialize arrays explicitly Enclose “array literals” in curly braces Enclose “array literals” in curly braces int grades[] = {20, 18, 25, 22, 15, 17, 21, 19}; Curly braces declare array, allocate space, and initialize values Curly braces declare array, allocate space, and initialize values NOTE: If size of the array is not specified the compiler sets its size to the number of elements NOTE: If size of the array is not specified the compiler sets its size to the number of elements

Topic Example

Problem Specification Write a program which will read a sequence of five numbers, sort the numbers into their proper order, then print out the results Write a program which will read a sequence of five numbers, sort the numbers into their proper order, then print out the results Use a selection sort algorithm to place the elements in their proper order Use a selection sort algorithm to place the elements in their proper order

Selection Sort Start at the first array element Start at the first array element Search through the remaining elements for the largest value Search through the remaining elements for the largest value Switch the position of the largest element with the first element Switch the position of the largest element with the first element Now repeat that process, but ignore the first element, since that one has already been set Now repeat that process, but ignore the first element, since that one has already been set Find the largest remaining element and move it to the earliest remaining position Find the largest remaining element and move it to the earliest remaining position

In This Lesson We Have: Introduced the concept of arrays Introduced the concept of arrays Discussed the declaration and initialization of arrays Discussed the declaration and initialization of arrays