EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.

Slides:



Advertisements
Similar presentations
2000 Prentice Hall, Inc. All rights reserved. 1 Capitolo 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
Programming In C++ Spring Semester 2013 Lecture 6 Programming In C++, Lecture 6 By Umer Rana.
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #8 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-211 DATA STRUCTURES LECTURE 2. EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100]
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
1 ICS102: Introduction To Computing King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
 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.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 9: Pass-by-Value.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Introduction to Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lecture 1 Wanda M. Kunkle.
Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
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.
EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Introduction to programming in java Lecture 21 Arrays – Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Arrays Declarations CSCI N305
CSC 113: Computer Programming (Theory = 03, Lab = 01)
L7 – Assembler Directives
Engineering Problem Solving with C++, Etter/Ingber
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Kingdom of Saudi Arabia
CS-161 Computer Programming Lecture 14: Arrays I
Introduction To Programming Information Technology , 1’st Semester
Arrays Arrays A few types Structures of related data items
C Programming Pointers
CS149D Elements of Computer Science
Lecture 14: Problems with Lots of Similar Data
Programming Fundamental
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST

Introduction  Arrays –Structures of related data items –Static entity (same size throughout program)

Arrays  Array –Consecutive group of memory locations –Same name and type ( int, char, etc.)  To refer to an element –Specify array name and position number (index) –Format: arrayname[ position number ] –First element at position 0  N-element array c c[ 0 ], c[ 1 ] … c[ n - 1 ] –Nth element as position N-1

Arrays  Array elements like other variables –Assignment, printing for an integer array c c[ 0 ] = 3; cout << c[ 0 ];  Can perform operations inside subscript c[ 5 – 2 ] same as c[3]

Arrays c[6] Name of array (Note that all elements of this array have the same name, c) c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Position number of the element within array c

Declaring Arrays  When declaring arrays, specify –Name –Type of array Any data type –Number of elements –type arrayName [ arraySize ]; int c[ 10 ]; // array of 10 integers float d[ 3284 ]; // array of 3284 floats  Declaring multiple arrays of same type –Use comma separated list, like regular variables int b[ 100 ], x[ 27 ];

Examples Using Arrays  Initializing arrays –For loop Set each element –Initializer list Specify each element when array declared int n[ 5 ] = { 1, 2, 3, 4, 5 }; If not enough initializers, rightmost elements 0 –To set every element to same value int n[ 5 ] = { 0 }; –If array size omitted, initializers determine size int n[] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array

1 2 // Initializing an array. 3 #include #include int main() 13 { 14 int n[ 10 ]; // n is an array of 10 integers // initialize elements of array n to 0 17 for ( int i = 0; i < 10; i++ ) 18 n[ i ] = 0; // set element at location i to cout << "Element" << setw( 13 ) << "Value" << endl; // output contents of array n in tabular format 23 for ( int j = 0; j < 10; j++ ) 24 cout << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl; 25

26 return 0; // indicates successful termination } // end main Element Value

1 2 // Initializing an array with a declaration. 3 #include #include int main() 13 { 14 // use initializer list to initialize array n 15 int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; cout << "Element" << setw( 13 ) << "Value" << endl; // output contents of array n in tabular format 20 for ( int i = 0; i < 10; i++ ) 21 cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl; return 0; // indicates successful termination } // end main

Element Value

Examples Using Arrays  Array size –Can be specified with constant variable ( const ) const int size = 20; –Constants cannot be changed –Constants must be initialized when declared –Also called named constants or read-only variables

1 2 // Initialize array s to the even integers from 2 to #include #include int main() 13 { 14 // constant variable can be used to specify array size 15 const int arraySize = 10; int s[ arraySize ]; // array s has 10 elements for ( int i = 0; i < arraySize; i++ ) // set the values 20 s[ i ] = * i; cout << "Element" << setw( 13 ) << "Value" << endl; 23

24 // output contents of array s in tabular format 25 for ( int j = 0; j < arraySize; j++ ) 26 cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl; return 0; // indicates successful termination } // end main Element Value

1 2 // Using a properly initialized constant variable. 3 #include int main() 9 { 10 const int x = 7; // initialized constant variable cout << "The value of constant variable x is: " 13 << x << endl; return 0; // indicates successful termination } // end main The value of constant variable x is: 7

1 2 // A const object must be initialized. 3 4 int main() 5 { 6 const int x; // Error: x must be initialized 7 8 x = 7; // Error: cannot modify a const variable 9 10 return 0; // indicates successful termination } // end main d:\cpphtp4_examples\ch04\Fig04_07.cpp(6) : error C2734: 'x' : const object must be initialized

1 2 // Compute the sum of the elements of the array. 3 #include int main() 9 { 10 const int arraySize = 10; int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int total = 0; // sum contents of array a 17 for ( int i = 0; i < arraySize; i++ ) 18 total += a[ i ]; cout << "Total of array element values is " << total << endl; return 0; // indicates successful termination } // end main Total of array element values is 55

1 2 // Histogram printing program. 3 #include #include int main() 13 { 14 const int arraySize = 10; 15 int n[ arraySize ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; cout << "Element" << setw( 13 ) << "Value" 18 << setw( 17 ) << "Histogram" << endl; // for each element of array n, output a bar in histogram 21 for ( int i = 0; i < arraySize; i++ ) { 22 cout << setw( 7 ) << i << setw( 13 ) 23 << n[ i ] << setw( 9 ); for ( int j = 0; j < n[ i ]; j++ ) // print one bar 26 cout << '*';

27 28 cout << endl; // start next line of output } // end outer for structure return 0; // indicates successful termination } // end main Element Value Histogram 0 19 ******************* 1 3 *** 2 15 *************** 3 7 ******* 4 11 *********** 5 9 ********* 6 13 ************* 7 5 ***** 8 17 ***************** 9 1 *