Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7: Arrays In this chapter, you will learn about
C Language.
Arrays and Strings.
One Dimensional Arrays
Structure.
Kernighan/Ritchie: Kelley/Pohl:
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
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.
Multidimensional Arrays in Java Vidhu S. Kapadia.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
C++ for Engineers and Scientists Third Edition
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
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
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 05 ARRAY 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
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.
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.
Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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:
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.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Arrays.
Arrays.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
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.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays. Arrays are objects that help us organize large amounts of information.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
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)
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.
Windows Programming Lecture 03. Pointers and Arrays.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
ARRAYS.
Array in C# Array in C# RIHS Arshad Khan
Computer Programming BCT 1113
Arrays, Part 1 of 2 Topics Definition of a Data Structure
7 Arrays.
EKT150 : Computer Programming
Multidimensional Arrays
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Dr Tripty Singh Arrays.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

Computer Programming Lecture 8 Arrays

2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press down arrow

3 #include void main (void) { char keyPress; printf (“Press any key from the keyboard: \n”); keyPress = getch (); int row = 12; col = 40; gotoxy (col, row); printf (“_”); switch (keyPress) { switch-statement Example (3)

4 case 75: // left arrow while (col > 0) { gotoxy (col, row); printf(“ “); col--; gotoxy (col,row); printf(“_”); delay (100); } break; case 77: // right arrow while (col < 80) { gotoxy (col, row); printf(“ “); col++; gotoxy (col,row); printf(“_”); delay (100); } break; switch-statement Example (3)

5 case 72: // up arrow while (row > 0) { gotoxy (col, row); printf(“ “); row--; gotoxy (col,row); printf(“_”); delay (100); } break; case 80: // down arrow while (row < 24) { gotoxy (col, row); printf(“ “); row++; gotoxy (col,row); printf(“_”); delay (100); } break; } // end of switch statement } // end of main switch-statement Example (3)

6 Duplication Input Problem Consider the problem: 5 input numbers, find the average and the input numbers which are above the average. EXAMPLE #include #include main() {int i, number, sum, average; float average; sum = 0; sum = 0; for (i=0; i< 5; i++) { printf(“input number: “); printf(“input number: “); scanf(“%d”,&number); scanf(“%d”,&number); sum += number; sum += number; }}

7 Arrays Array can help with that kind of problems. What is ARRAY? A collection of memory locations (same data type, single variable name) Data structure which can access more than one item using a single variable. (one-to-many correspondence between name and current value.)

8 Arrays An array is a collection of two or more consecutive memory cells, call array element, that are associated with a particular symbolic name. To set up an array in memory, we must declare both the name of the array and the number of cells associated with it.

9 Arrays Declaration 1 SYNTAX 1 element-type name[size]; EXAMPLE double a[5]; int x[10]; float y[20];

10 Arraysmain(){ int x[10]; int x[10]; …} X[0]X[1]X[2]X[3]X[4]X[5]X[6]X[7]X[8]X[9] 10-element array called x 10 boxes for keeping 10 integer numbers

11 Arrays x[3] refers to the element in x whose index is 3.x[3] refers to the element in x whose index is 3. Array declaration in C:Array declaration in C: –int x[10] (an array with storage space for 10 integers) (fixed (nonvariable) size: declared before execution) –first element: x[0], last element: x[n-1] (I.e., x[9]) (homogeneous data structure / uniform data type)

12 Array subscript is used to differentiate between the individual array elements and to allow us to specify which array element is to be manipulated. Array subscript is used to differentiate between the individual array elements and to allow us to specify which array element is to be manipulated. Arrays Subscription Example: int x[5]; int x[5]; x[0] = 10 x[0] = 10 x[1] = ; x[1] = ; x[2] = x[1]; x[2] = x[1]; x[3] = 2*x[1]; x[3] = 2*x[1]; x[4] = x[1] + 3*x[3]; x[4] = x[1] + 3*x[3];

13 The subscript value is in range 0 to N-1 for an array with size N We call it zero-based indexing because it starts from 0. An array subscript value outside the range often causes a run- time error. Arrays Subscription Example: int a[5]; a[10] = 10; a[-1] =0; !!! Run-time error !!! !!! Run-time error !!!

14 Entering Data into the Array Example: for (int day = 0; day < 7; day ++ ) { printf( “ Enter temperature for day “ ); scanf ( “ %d ”, &week [day] ); }

15 Reading Data from the Array To calculate the average of the week’s temperature using array (here is the code). float sum = 0; for (int day = 0; day < 7; day++ ) sum += week[day]; printf ( “ Average is %f ”,sum/7);

16 A Simple Example of Using Arrays Create an array (Ex. : a 12 floating point array)Create an array (Ex. : a 12 floating point array) Set a value to an element in an arraySet a value to an element in an array (Ex.: set the third element of rainfall to 2.5) (Ex.: set the third element of rainfall to 2.5) Compare two elements of an arrayCompare two elements of an array (Ex.: compare the 4rd and the 12th elements) float rainfall [12]; if (rainfall[3] > rainfall[11]) printf (“more rain in April than in December”); printf (“more rain in April than in December”); rainfall [2] = 2.5;

17 Arrays Declaration 2 EXAMPLE int arr[5] = {0, 0, 3, 2, 1}; char v [ ] = {‘A’, ’E’, ’I’, ’O’, ’U’}; int arr[5] = {0, 0, 3, 2, 1}; float x[2] = {0.112, 3.2} SYNTAX 2 element-type aname[size] = {initialization list}

18 Multidimensional arrays are defined in much the same manner as one dimensional arrays, Multidimensional arrays are defined in much the same manner as one dimensional arrays, Except that a separate pair of square brackets is required for each dimension. Except that a separate pair of square brackets is required for each dimension. Two-dimensional array ---->[ ][ ] Two-dimensional array ---->[ ][ ] Three-dimensional array ----->[ ][ ][ ] Three-dimensional array ----->[ ][ ][ ] In general term, the multidimensional array definition In general term, the multidimensional array definition can be written as: Multi-Dimensional Arrays

19 MultiDimensional Arrays Declaration SYNTAX 1 element-type aname[size 1 ][size 2 ]…[size n ]; element-type aname[size 1 ][size 2 ]…[size n ]; /*decalration*/ element-type aname[][size2]…[sizen] element-type aname[][size2]…[sizen] /* prototype */ element-type aname[][]…[] element-type aname[][]…[] /* prototype */ EXAMPLE double table[NROWS][NCOLS]; /* declaration */ int out[ ][4], /* output parameter */ int nrows[ ][ ][ ] /* number of rows */ Ex. in a[5]; Ex. int mat[3][4]; Ex. double vert[x][y][z];

20 Example char tictac[3][3];// declaration Initialization of 2D Arrays char tictac[3][3] = {‘x’, ’o’, ’x’, ’o’, ’x’, ’o’, ’o’, ’x’, ’x’}; char tictac[3][3] = {{‘x’,’o’,’x’},{‘o’,’x’,’o’},{‘o’,’x’,’x’}}; Multi-Dimensional Arrays

21 The array The array enroll declared enroll declared here here int enroll [100][5][4]; Multi-Dimensional Arrays