CS-161 Computer Programming Lecture 14: Arrays I

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 6 Programming In C++, Lecture 6 By Umer Rana.
Advertisements

CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 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 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Arrays CE 102 Algorithms and Programming KTO Karatay University Arrays are data structures consisting of data items of the same type Arrays are not dynamic.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
C Arrays Systems Programming. Systems Programming: Arrays 22 ArraysArrays  Arrays  Defining and Initializing Arrays  Array Example  Subscript Out-of-Range.
Introduction to Programming Lecture 11. ARRAYS They are special kind of data type They are special kind of data type They are like data structures in.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
 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.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
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.
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Two Dimensional Array Mr. Jacobs.
Arrays Declarations CSCI N305
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Arrays Part-1 Armen Keshishian.
Arrays (Operations on Arrays)
C++ Arrays.
Based on lecture notes from Dr. Deepak Kumar of Brynmawr
DAYS OF THE WEEK.
C Arrays Systems Programming.
Based on lecture notes from Dr. Deepak Kumar of Brynmawr
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Array Data Structure Chapter 6
One-Dimensional Array Introduction Lesson xx
C Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Introduction to Programming
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Kingdom of Saudi Arabia
EKT150 : Computer Programming
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Lecture 12 Oct 16, 02.
INC 161 , CPE 100 Computer Programming
To refer to an element, specify
Chapter 9: Data Structures: Arrays
CHAPTER 2 Arrays and Vectors.
Array Data Structure Chapter 6
Arrays in Java.
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
CHAPTER 2 Arrays and Vectors.
Based on lecture notes from Dr. Deepak Kumar of Brynmawr
Lecture 14: Problems with Lots of Similar Data
Arrays.
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
Contact
2011年 5月 2011年 6月 2011年 7月 2011年 8月 Sunday Monday Tuesday Wednesday
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
4.1 Introduction Arrays A few types Structures of related data items
Week 7 - Monday CS 121.
Presentation transcript:

CS-161 Computer Programming Lecture 14: Arrays I Spring 2015 (Sections A & B) Saqib Rasheed

Arrays They are special kind of data type In C++ each array has name size They occupy contagious area of memory

Storage of an array in memory C[0] C[1] C[2] C[3] C[4] C[5] C[6] C[7] C[8] C[9] Name ... 35 59 24 memory Index

Declaration of Arrays arrayType arrayName[numberOfElements ]; For example , int age [ 10 ] ; More than one array can be declared on a line int age [10] , height [10] , names [20] ; Mix declaration of variables with declaration of arrays int i , j , age [10] ;

Defining Arrays When defining arrays, specify Examples: Name Type of array Number of elements arrayType arrayName[ numberOfElements ]; Examples: int week[ 10 ]; float myArray[ 3284 ]; Defining multiple arrays of same type Format similar to regular variables Example: int b[ 100 ], x[ 27 ];

Arrays Array To refer to an element, specify Format: Name of array (Note that all elements of this array have the same name, week) Array Group of consecutive memory locations Same name and same type To refer to an element, specify Array name Position number Format: arrayname[ position number ] First element at position 0 n element array named week: week[ 0 ], week[ 1 ]...week[ n–1] Saturday temp Sunday temp Monday temp Tuesday temp Wednesday temp Thursday temp Friday temp week[0] week[1] week[2] week[3] week[4] week[5] week[6] Position number of the element within array week

Arrays Array elements are like normal variables week[ 0 ] = 3; cout << week[ 0 ]; Perform operations in subscript. If x equals 3 week[ 5 - 2 ] = week[ 3 ] = week[ x ] Static entry (same size throughout program execution) Some arrays are dynamic (that can be shrink and increase at execution time) called Dynamic Array.

Initializers Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 }; If not enough initializers, rightmost elements become 0 int n[ 5 ] = { 0 } If too many a syntax error is produced If size omitted, initializers determine it int n[ ] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array

Referring to individual elements of a Array We can refer the individual elements of an array using subscripts (the number in brackets following the array name). The number specifies the element’s position in the array. All the array elements are numbered, starting at 0 to size. Example: week [2]. cin >> week[2]; Saturday temp Sunday temp Monday temp Tuesday temp Wednesday temp Thursday temp Friday temp week[0] week[1] week[2] week[3] week[4] week[5] week[6] cout << “The temperature is “ << week [2];

Entering Data into the Array Example: for (int day = 0; day < 7; day ++ ) { cout << “Enter temperature for day“ << day+1; cin >> week [day]; }

Displaying Data from Array Example: for (int day = 0; day < 7; day ++ ) { cout <<Week[day]; }

Reading Data from the Array To calculate the average of the week’s temperature using array (here is the code). int sum = 0; for (int day = 0; day < 7; day++ ){ sum += week[day]; } cout << “Average is “ << sum/7;

Example #include<iostream.h> void main() { int marks [5]; int total=0,i; cout<< "Please enter marks of five students:\n"; for (i=0; i<5; i++) { cin >> marks[i]; } total + = marks[i]; float classavg = total/5; for(int j=0; j<5; j++) { cout<< "Students # "<< j+1<< “ scored "<<marks[j]<<" marks"; cout<< "Class average is "<<classavg<<endl;

Class Task Write a program to store and display the mathematical table of given number using arrays. Write a program to reverse elements in an array of size 10. Write a program that reads 10 integers from user and then print the largest and smallest integer using arrays.