Arrays One-Dimensional initialize & display Arrays as Arguments Part I.

Slides:



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

Programming and Data Structure
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
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.
Kernighan/Ritchie: Kelley/Pohl:
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
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.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Chapter 9: Arrays and Strings
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Arrays.
Arrays One-Dimensional initialize & display Arrays as Arguments Two-dimensional initialize & display Part I.
C Programming Lecture 14 Arrays. What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Arrays Structured Programming 256 Chapter 10 © 2000 Scott S Albert Arrays One-Dimensional initialize & display Arrays as Arguments Two-dimensional initialize.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Chapter 8 Arrays and Strings
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Data Structures (Terminology) Static Set at compile time Dynamic Set at run time.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
Copyright Curt Hill Arrays in C/C++ What? Why? How?
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 7 Arrays.
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Arrays.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CSIS 113A Lecture 10 Arrays Glenn Stevenson CSIS 113A MSJC.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays Kingdom of Saudi Arabia
Lecture 18 Arrays and Pointer Arithmetic
Arrays Lecture 11.
Arrays Arrays A few types Structures of related data items
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Arrays One-Dimensional initialize & display Arrays as Arguments Part I

Data Types a simple or atomic a structured * char, int, float, double array, struct, union, class

Structured Data Type - Array An array is a collection of data storage locations, each of which holds the same type of data. Each storage location is called an element of the array. An homogeneous aggregate of elements Each element is referred to as an indexed or subscripted variable.

Array Declaration [] Syntax dataType arrayName [ ConstIntExpression ] The number of elements in an array is [ ] stated within square brackets, [ ]. Examples double angle [4];const int POLY = 8; int testScore [12];double angle [POLY]; char password [8];

Array Storage Each array has sufficient memory reserved to hold the number of data items of the given type. Initializing an array sets up the address of the first element. Addresses of all other elements are offsets from the starting address. Array elements occupy contiguous memory locations.

Array Element Access Syntax arrayName [ indexExpression ] Program access to each of the array elements is by referring to an offset from the array name. Array elements are counted beginning with zero. A[0] => add zero to the address of A (1 st element) A[3] => add (3 * width of element) to the address of A (4 th element)

Array Element Access double angle[4];// declaration Example angle [0] = 6.21; angle [1] = 15.89; angle [2] = 7.5; angle [3] = -45.7; angle sub zero = 6.21 angle sub one = angle sub two = 7.5 angle sub three = * * * angle 3 -- Mathematical notation

Using Array Elements  write the contents of an array element: cout << angle[2]; // the third element  assign values to an array element: cin >> angle[3]; // the fourth element angle[2] = pow(axis,4); // the third element  use it as a parameter: y = sqrt(angle[0]); // the first element  use it in an expression: x = 2.5 * angle[1] + 64; // the second element * *

Declare an Array float myArray[100]; Want to reset myArray to zeros myArray = 0.0; // works in some languages In C++, the array name is a pointer to an address in memory myArray is an address in memory An attempt to assign a float to a pointer to a float

Array Component Access indexindex index index for(index=0; index < arraySize; index++) myArray[index] = 0.0; 100 * Zero out an entire array. (Initialize every element to 0.0.) For loops were “made for” arrays You always know how many times the loop will execute – once for each element

Off Set memory addresses starting address off set by one unit off set by two units off set by three units [ 0 ] [ 1 ] [ 2 ] [ angle int angle[4];

Out-of-Bounds Array Index memory addresses angle[4] = 135; cout << angle[5]; [ 0 ] [ 1 ] [ 2 ] [ angle grade score Overwrites value in memory without warning!

Declare an Array Syntax type arrayName[index]; Example double dailyHigh[31] ; int quizGrade[29] ; char YSUID[9] ; *

Initialize an Array Element Syntax arrayName[index] = value; Example dailyHigh[18] = 16.7; quizGrade[2] = 15; YSUID[3] = ‘7’; *

Initialize an Array at Declaration Time double angle[4] = {16.21, 15.89, 7.5, -45.7}; double convert[5] = {.64,.89,.76,.83,.65}; int scores[12] = {210, 198, 203, 188, 200, 224, 220, 217, 211, 194, 197, 189}; double iona[8] = {0.0}; // fill with zeros *

Initialize an Array - Variations int scores[ ] = {210, 198, 203, 188, 200, 224, 220, 217, 211, 194, 197, 189}; char name1[4] = {‘Z’, ‘o’, ‘l’, ‘a’}; char name2[4] = “Zola”;// no braces or, char phrase [ ] = “Hello World”; * Space for 12 integers Compiler will allocate 12 bytes Will not compile: “init string too long” 5

Character Arrays - \0 char name1[4] = {‘Z’, ‘o’, ‘l’, ‘a’}; char name2[5] = “Zola”;// no braces or, needs 5 slots to have space for the end of string character, \0 char phrase [ ] = “Hello World”; * Compiler will allocate 12 bytes

Sequencing Through an Array Use the for statement to sequence through an array. Assume an array with 7 grades in it… Total the contents of an array: sum = 0; for (index=0; index < 7; index++) sum += grades[index]; char name1[4] = {‘Z’, ‘o’, ‘l’, ‘a’}; Print out an array for (i = 0, i < 4, i++) cout << name1[i];// prints Zola

Loading an Array – with cin double grade[10]; int index; for(index=0; index < 10; index++) { cout<<“Enter a grade “; cin >> grade[index]; } grade 0 9

Finding the Max/Min Value Set the max or min to element zero. Use a for loop to cycle through the array. Compare each element to the max/min. If necessary, assign a new value to max/min. How would you do this? *

Finding the Maximum Value double find_max(int temp[30]) { int max = temp[0]; //only max seen thus far for(int index=1; index max) max = temp[index]; return (max); } * If the current temp[ ] is bigger than the max seen thus far. Remember this one!

Finding the Minimum Value double find_min(int temp[30]) { int min = temp[0]; // minimum thus far < for(int index = 1; index < 30; index++) if (temp[index] < min) min = temp[index]; return ( min ); } *

Aggregate Assignment - NOT! There are no aggregate assignments with arrays. That is, you may not assign one array to another. int x[5] = {11, 22, 33, 44, 55}; int y[5]; y = x;y[ ] = x[ ]; for (i=0;i<5;i++) y[i] = x[i];

Arrays as Arguments temp[ ] double find_max(int temp[ ]) { max = temp[0]; for(index = 1; index max) max = temp[index]; return max; } temp[ ] double find_max(int temp[ ]) * Leave blank or use constant

Passing an Array or Elements temp find_max(temp); // whole array, no [ ] pricequantityamount inventory(price, quantity, amount); // 3 arrays cuplet words(cuplet); temp[3] find_max(temp[3]); pricequantity[4]amount[12] inventory(price, quantity[4], amount[12]); cuplet[0] words(cuplet[0]); * Elements of an array

Comparing Arrays as Arguments to Simple Data Types formal parameter formal parameter declaration fordeclaration for parameterPass-by-ValuePass-by-Reference simple var.int costint& price arrayimpossibleint myArray[ ] arrayconst int source[ ] Keeps function from changing values in the array

Passing an Array Element Follows the rules for the type of an element in the array int cntA[1111]; cntA[33] is an int and is treated as any other int when used as an actual argument. cntA refers to the entire array. It is the address of the array or a pointer to the array.

Slide 39 of 55

Slide 40 of 55

1-D Array Review An array is a structured data type 1st element is “ arrayName subzero ” Array Declaration int myArray [9]; Array Initialization int myArray [9 ] = { 9, 9, 9, 8, 7, 6, 5, 4, 3}; Array Element Reference myArray [3];// value = 8 * 9 const int MAXELS = 9; Use everywhere instead of the literal

1-D Array Review Arrays are always passed by reference, unless const added to the formal parameter. Array in a Function Prototype void myFunction(int [9]); Array in a Function Call myFunction(myArray); Array in a Function Header void myFunction(int anyArrayName[9]) optional