Engr 0012 (04-1) LecNotes 22-01. Engr 0012 (04-1) LecNotes 22-02 arrays collection of values - all of the same type, e.g., int individual values referred.

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Advertisements

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Introduction to C Programming
Chapter 7: Arrays In this chapter, you will learn about
C Language.
Pointers. 2 A pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address.
One Dimensional Arrays
Programming and Data Structure
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.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Programming with Collections Collections in Java Using Arrays Week 9.
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
© The McGraw-Hill Companies, 2006 Chapter 5 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.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
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
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
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.
 2007 Pearson Education, Inc. All rights reserved C 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,
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Arrays One-Dimensional initialize & display Arrays as Arguments Part I.
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.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes C++ errors/debugging build/compile compiler does not recognize a statement build/compile.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Engr 0012 (04-1) LecNotes Variable (data object) declaration in C two methods of variable declaration 154 a int 0065FDF4 Value-oriented int a; address.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
 2007 Pearson Education, Inc. All rights reserved C 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 :
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Computer Programming for Engineers
Two Dimensional Arrays Found in chapter 8, Section 8.9.
ICS103: Programming in C 7: Arrays Muhamed F. Mudawar.
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];
© 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.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Computer Programming BCT 1113
Engr 0012 (04-1) LecNotes
Engr 0012 (04-1) LecNotes
Engr 0012 (04-1) LecNotes
EKT150 : Computer Programming
Engr 0012 (04-1) LecNotes
Arrays.
Presentation transcript:

Engr 0012 (04-1) LecNotes 22-01

Engr 0012 (04-1) LecNotes arrays collection of values - all of the same type, e.g., int individual values referred to as an element of the array example: an array of type int an individual element is 14 example: This is an array. an array of type char an individual element is h

Engr 0012 (04-1) LecNotes arrays - declaring // defined constants #define MAXARRAYSIZE 100 … main() { // begin main // variable declaration double length[MAXARRAYSIZE], // array of lengths width[MAXARRAYSIZE]; // array of widths char filename[81]; // string array int lengthused, // actual amt in use i; // loop index … use defined constant to specify maximum number of elements in the array type of values meaningful name maximum number of elements in [ ] arrays do not need to be “full” - need to keep track of how much is actually used

Engr 0012 (04-1) LecNotes arrays - using each “element” of an array must be accessed individually there are no commands that process an entire array!!! contrast with MATLAB area = length.*width single statement can find “area” for set of lengths and widths C language for (i=0; i<lengthused; i=i+1) { // begin area calc area[i] = length[i]*width[i]; } // end area calc must process element by element

Engr 0012 (04-1) LecNotes arrays - using for (i=0; i<lengthused; i=i+1) { // begin area calc area[i] = length[i]*width[i]; } // end area calc area[3] refers to the 4th element of area area[7] refers to the 8th element of area area[i] refers to the (i+1)th element of area array indices always start at 0 (zero) contrast with MATLAB where indices start at 1 (one) each element of an array is accessed through its index

Engr 0012 (04-1) LecNotes arrays - using // defined constants #define MAXLENGTH 10 main() { // begin main // variable declaration double length[MAXLENGTH], // length array width[MAXLENGTH], // width array area[MAXLENGTH], // area array perim[MAXLENGTH]; // perimeter array int lengthused, // actual num of lengths i; // loop index maximum size of array is different than size actually in use ==> You, the programmer, must keep track of amount in use and pass the amount in use as a parameter to functions.

Engr 0012 (04-1) LecNotes arrays - initialization // defined constants #define MAXSIZE 5 main() { // begin main // variable declaration double a[3] = {8, 9}, // double array b[] = {4, 5, 6}, // double array c[5], // double array d[MAXSIZE]; // double array int i; // loop index // algorithm printf( "\na[0] = %f, a[1] = %f, a[2] = %f" "\n\nb[0] = %f, b[1] = %f, b[2] = %f", a[0], a[1], a[2], b[0], b[1], b[2] ); hardwire - can specify size and some elements can specify all elements - let C determine size a[0] = , a[1] = , a[2] = b[0] = , b[1] = , b[2] =

Engr 0012 (04-1) LecNotes arrays - initialization // defined constants #define MAXSIZE 5 main() { // begin main // variable declaration double a[3] = {8, 9}, // double array b[] = {4, 5, 6}, // double array c[5], // double array d[MAXSIZE]; // double array int i; // loop index // algorithm for (i=0; i<3; i=i+1) { // begin for c[i] = 2*i; } // end for printf( "\n\nc[0] = %f, c[1] = %f, c[2] = %f" "\nc[3] = %f \nc[4] = %f", c[0], c[1], c[2], c[3], c[4] ); can initialize using assignment statements (with or without loop) c[0] = , c[1] = , c[2] = c[3] = c[4] =

Engr 0012 (04-1) LecNotes arrays - initialization // defined constants #define MAXSIZE 5 // prototypes void get_d_from_file( double d[], int *pdused ); main() { // begin main // variable declaration double a[3] = {8, 9}, // double array b[] = {4, 5, 6}, // double array c[5], // double array d[MAXSIZE]; // double array int i, dused; // loop index // algorithm get_d_from_file( d, &dused ); can initialize by reading a file with values warning: need to keep track of how many actual values were read

Engr 0012 (04-1) LecNotes arrays - use simple assignment statements newlength = length[12]; assigns 13th element of length to newlength length[7] = 3*width[7]; assigns 3 times the 8th element of width to the 8th element of length

Engr 0012 (04-1) LecNotes arrays - use avelength = 0.0; for( i = 0; i < actsize; i = i+1 ) { // begin for loop avelength = avelength + length[i]; } // end for loop avelength = avelength/actsize; computes the average of an array process only elements in use - not whole (MAXSIZE) array uses variable(index) i to access each element of array

Engr 0012 (04-1) LecNotes arrays - use for( j = 0; j < actsize; j = j+1 ) { // begin for loop length[j] = 3*j+12; } // end for loop assigns a value to each element of array length can use variable (index) names other than i

Engr 0012 (04-1) LecNotes arrays - use nonsense = length[3*intvalue%2]; index needs to be an integer value how many ways can we produce an integer value?

Engr 0012 (04-1) LecNotes arrays - function prototypes/parameters // preprocessor commands #define MAXARRAYSIZE 100 … main() { // variable declaration double length[MAXARRAYSIZE], // array of lengths width[MAXARRAYSIZE]; // array of widths int actualsize, // actual amt in use i; // loop index … // function prototypes void getarray( double length[ ], int *pactsize ); double avearray( double length[ ], int actsize ); used to return number of values actually in use [ ] tell that the variable is an array need to send amount actually in use

Engr 0012 (04-1) LecNotes arrays - function prototypes/parameters // preprocessor commands #define MAXARRAYSIZE 100 // function prototypes void getarray( double length[ ], int *pactsize ); double avearray( double length[ ], int actsize ); … main() { // variable declaration double length[MAXARRAYSIZE], // array of lengths width[MAXARRAYSIZE]; // array of widths int actualsize, // actual amt in use i; // loop index // calling functions getarray( length, &actualsize ); ave = avearray( length, actualsize ) name w/o [ ] meaning, not name, is important for value transferred

Engr 0012 (04-1) LecNotes arrays - addresses &length[k] & used to denote address of single element printf( "\nEnter next length ==> " ); scanf( "%lf", &length[j] ); \\ prototypes double prod( double *plen, double *pwid ); … \\ algorithm area = prod( &length[i], &width[i] );

Engr 0012 (04-1) LecNotes arrays - addresses name of array alone (without subscript) is the same as the address of its first element length &length[0]