Department of Computer Science Western Michigan University

Slides:



Advertisements
Similar presentations
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Advertisements

©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
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.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
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.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays and Strings
Arrays- Part 2 Spring 2013Programming and Data Structure1.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays.
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
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.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Windows Programming Lecture 03. Pointers and 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 Chapter 7.
ARRAYS.
Array in C# Array in C# RIHS Arshad Khan
EGR 2261 Unit 10 Two-dimensional Arrays
Computer Science 210 Computer Organization
BASIC ELEMENTS OF A COMPUTER PROGRAM
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2 - Introduction to C Programming
EGR 115 Introduction to Computing for Engineers
Chapter 2 - Introduction to C Programming
Arrays in C.
Computer Science 210 Computer Organization
C Passing arrays to a Function
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Array Data Structure Chapter 6
Chapter 2 - Introduction to C Programming
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Chapter 2 - Introduction to C Programming
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
EKT150 : Computer Programming
Introduction To Programming Information Technology , 1’st Semester
Multidimensional Arrays
Chapter 2 - Introduction to C Programming
MSIS 655 Advanced Business Applications Programming
Standard Version of Starting Out with C++, 4th Edition
7 Arrays.
Dr Tripty Singh Arrays.
Array Data Structure Chapter 6
EECE.2160 ECE Application Programming
Chapter 2 - Introduction to C Programming
Arrays.
C++ Array 1.
EECE.2160 ECE Application Programming
Introduction to C Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Dr. Khizar Hayat Associate Prof. of Computer Science
Files Chapter 8.
Presentation transcript:

Department of Computer Science Western Michigan University CS 1023 Intro to Engineering Computing 3: Computer Programming LM5 – One-Dimensional Arrays Mark Kerstetter Department of Computer Science Western Michigan University © Spring 2013

Arrays – Lists or Vectors Tables or Matrices Engineering uses subscripted names like xk , yj+1 , z10 , mij to represent lists, vectors, tables and matrices. arrays store groups of homogeneous data use when it is necessary to keep several related values in memory at the same time. Don’t use if not necessary! array names same naming rules as everything else E.g., temperature, elevation, direction, x_coord, y_coord array elements & array element names – indicated by [ ] temperature[0], elevation[100], direction[k], x_coord[p1], y_coord[p1], month[day[n]] element index (aka element subscript) must evaluate to an integer array element is an expression just like a simple variable Can be used in calculations Can be assigned a value Can be passed to a function as an actual paramter Can receive a value via scanf or fscanf Can print a value via printf or fprintf every element of an array is the same data type (i.e., homogeneous) E.g., all int, all double, all char, all long, etc. array processing arrays and loops are often used together xk becomes x[k] yj+1 becomes y[j+1] z10 becomes z[10] mij becomes m[i,j]

Arrays 1-D 1-D Array - Lists   M T W R F S N Reserve Space Only! Sample Declarations int quiz[5] ; double gpa[150] ; int monthL[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} ; char day[7] = {‘M’, ‘T’, ‘W’, ‘R’, ‘F’, ‘S’, ‘N’} ; Reserve + Initialize gpa[0] gpa[1] gpa[2] gpa[3] gpa[147] gpa[148] gpa[149]  quiz[0] quiz[1] quiz[2] quiz[3] quiz[4] 31 28 30 monthL [0] monthL [1] monthL [2] monthL[3] monthL[4] monthL[5] monthL[6]  monthL[10] monthL[11] M T W R F day[0] day[1] day[2] day[3] S N day[4] day[5] day[6]

Arrays 2-D 2-D Array - Matrices or Tables  Terminology: dimensions, rows, and columns double year[52, 7] ; /* 52 rows representing weeks of a year */ int quiz[4,15] ; /* 4 quizzes up to 15 questions per quiz *  7 columns 52 rows 364 array elements of double precision numbers 15 columns 4 rows

Parallel Arrays     Use several arrays of the same dimension, i.e., size and shape, to hold related information of the same or differing data types E.g., char *name[150], char *ltrGrade[150], double gpa[150] ; E.g., int x[20], y[20], z[20] ;  x y z [0] [1] [2] [3] [4] [5] [19] [18] name[0] name[1] name[2] name[3] name[147] name[148] name[149]  ltrGrade[0] ltrGrade[1] ltrGrade[2] ltrGrade[3] ltrGrade[147] ltrGrade[148] ltrGrade[149]  gpa[0] gpa[1] gpa[2] gpa[3] gpa[147] gpa[148] gpa[149] 

Arrays 1-D 1-D Array E.g., int quiz[5] ; /* 1-D integer array declaration */ What’s the name of the 1st element in the array called quiz? the last element? Answers: quiz[0] and quiz[4] How can we initialize the elements of quiz to contain all zeros? Answer: int quiz[5] = {0} ; How can we initialize the elements of quiz to contain all 32’s? Answer: int quiz[5] = {32, 32, 32, 32, 32 } ; E.g., double gpa[150] ; /* 1-D double precision array declaration */ What’s the name of the 1st element in the array called gpa? The last element? Answer: gpa[0] and gpa[149] How can we initialize the elements of gpa to contain all zeros? Answer: double gpa[150] = {0} ; How can we initialize the elements of quiz to contain all 4.00’s? Answer: for (k=0; k<=149; k++) { gpa[k] = 4.00 } ;

Passing Arrays as Function Parameters Declare the array(s) you want to use in the main (or calling) program double arr1[100], arr2[100] ; Create a programmer-defined function that uses an array of the same data type. void copy_array(double a[], double b[], int len) { } Copy the heading line of the programmer-defined function to the appropriate declaration area and add a trailing semicolon. void copy_array(double a[], double b[], int len) ; Use the array name, without square brackets, as the actual parameter corresponding to the array. copy_array(arr1, arr2, 47) ;

Passing Arrays as Function Parameters Syntax of Array Formal Parameters Create a programmer-defined function that uses an array of the same data type. void copy_array(double a[], double b[], int len) { } Array parameters refer to entire arrays, not single array elements Array parameters are always pass-by-reference because . . . multiple elements are passed through one parameter. Array parameters have special pass-by-reference syntax double a[] not double[] a not double *a[] syntax is … <type> <name> []

Passing Arrays as Function Parameters Syntax of Array Actual Parameters Using an array as an actual parameter. Array parameters are always pass-by-reference because . . . multiple elements are passed through one parameter. Just use the array name (without square brackets) copy_array(arr1, arr2, 47) ;

Area of a Polygon (x0,y0) (x1,y1) (x2,y2) (x3,y3) (x4,y4) (x5,y5) (x0,y0) (x1y1) (x2y2) (x3y3) (x4y4) (x5y5) Initial Draft Solution: input vertex points of polygon print the vertex points compute the polygon’s area using the vertex points print the polygon’s area

Characters character data type: char Each memory location contains exactly one character Character constants – single characters enclosed in single quotes ‘M’ is the upper case character M and ‘m’ is the lower case character m ‘3’ is the character 3, while 3 (without single quotes) is the number 3, an integer. Integers and characters are stored differently. You can do arithmetic with integers, but not with characters! ‘ ‘ is the space character and ‘’ is the empty or null character. ‘\’ ’ is the single quote character (See Escape Character below.) Escape Character is \ (back slash) used to represent non-printing (invisible) characters, like newline (\n), tab (\t), backspace (\b), alarm (\a), newpage (\f), etc. used to “quote” the single character that follows the \ so the character temporarily does not have its programmed meaning E.g., to print a double-quote character within an I/O control string use \” to represent the single-quote character as a character constant use \’

Arrays Characters char day[7] = {‘M’, ‘T’, ‘W’, ‘R’, ‘F’, ‘S’, ‘N’} ; What would result from … printf (“Day %i of the week is %c\n”, 2, day[1] ) ; Day 2 of the week is T char compass_rose[4] = {‘N’, ‘S’, ‘E’, ‘W’} ; What would result from … printf (“\”West\” on a compass is \”%c\”\n”, compass_rose[3] ) ; “West” on a compass is “W”. How to display a table using tabs ( \t ) for (d = 0; d <= 360; d+=15) { r = d * pi/180 ; printf(“%4i\t%f\t%f\%f\n”, d, sin(d), cos(d), tan(d) ) ; }