CS149D Elements of Computer Science

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
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.
Lecture 13: 10/8/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
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 C++ Syntax and Semantics The Development Process.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Arrays Chapter 8 page /24/07CS150 Introduction to Computer Science 1 Arrays (8.1)  One variable that can store a group of values of the same.
Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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.
Lecture 13: 10/10/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Arrays.
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.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Introduction to programming in java Lecture 21 Arrays – Part 1.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Test 2 Review Outline.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Two-Dimensional Arrays
Two Dimensional Array Mr. Jacobs.
Arrays Declarations CSCI N305
Chapter 8 Arrays Objectives
C++ Arrays.
CS149D Elements of Computer Science
DATA HANDLING.
CS149D Elements of Computer Science
Arrays ICS 111: Introduction to Computer Science I
CS150 Introduction to Computer Science 1
Chapter 8 Arrays Objectives
CS-161 Computer Programming Lecture 14: Arrays I
EKT150 : Computer Programming
Lecture 12 Oct 16, 02.
Introduction To Programming Information Technology , 1’st Semester
CS148 Introduction to Programming II
Arrays .
Topics discussed in this section:
Multidimensional Arrays
CISC181 Introduction to Computer Science Dr
Arrays Lecture 11.
CS148 Introduction to Programming II
CS150 Introduction to Computer Science 1
Chapter 8 Arrays Objectives
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
CS149D Elements of Computer Science
CS149D Elements of Computer Science
CS148 Introduction to Programming II
CS148 Introduction to Programming II
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Lecture 14: Problems with Lots of Similar Data
Arrays Imran Rashid CTO at ManiWeber Technologies.
CS148 Introduction to Programming II
CS148 Introduction to Programming II
CS148 Introduction to Programming II
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
CS148 Introduction to Programming II
COS 151 Bootcamp – Week 4 Department of Computer Science
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 22: 11/14/2002 Lecture 22: 11/14/2002 CS149D Fall 2002

Outline Arrays (section 5.1) Declaration and initialization Accessing array elements Arrays and data files Lecture 22: 11/14/2002 CS149D Fall 2002

Arrays 1 variable can store 1 data value. How about if we want to deal with 100 integers or 100 doubles (100 measurements) at the same time? Declare 100 variables (How about 1000?) One Solution We use an array to store a number of elements of the same data type A one-dimensional array is a number of successive memory locations, each of which can store an item of data of the same type and which are all referenced through the same variable name. Example a values array How many array elements (array size)? First element is values[0] Last element is values[5] 73 62 51 42 41 34 values[0] values[1] values[2] values[3] values[4] values[5] Lecture 22: 11/14/2002 CS149D Fall 2002

Arrays Declaration and Initialization Syntax to declare an array Data Type Array_Name [Array Size]; int values[6]; array index ranges from 0 to 5 (6-1) double elements[5]; array index ranges from 0 to ? Can initialize array when defined int values[6] = {0,1,2,3,4,5}; 0 1 2 3 4 5 values[0] values[1] values[2] values[3] values[4] values[5] Initialization sequence shorter than array size int values[6] = {0}; 0 0 0 0 0 0 values[0] values[1] values[2] values[3] values[4] values[5] 0 1 2 3 values[0] values[1] values[2] values[3] No size, but an initialization sequence int values[] = {0,1,2,3}; Lecture 22: 11/14/2002 CS149D Fall 2002

Accessing Array elements1/2 Fill an array with numbers 0,2,4,6,8,10,12 (0*2, 1*2,3*2, …) //define array const int SIZE = 7; // Why did we choose SIZE as 7? int values[SIZE]; //assign values to array elements for (int k = 0; k < SIZE ; k++) values[k] = k *2; //print array for (int k = 0 ; k < SIZE ; k++) cout << values[k] << “\t”; Lecture 22: 11/14/2002 CS149D Fall 2002

Accessing Array elements2/2 Printing an array in reverse order for (int k = SIZE - 1 ; k >= 0 ; k--) cout << values[k] << “\t”; No check is performed that array index lies within the bounds of the array! int values[5]; // index bounds from 0 to 4 cout << values[5]; //What happens in this case? Reading from a file and assigning to array elements A file with 2 fields per line (id grade) id is an int and grade is a float const int SIZE = 20; int id[20]; float grade[20]; fstream gradesf; gradesf.open(“GradesFile”,ios::in); for (k=0 ; k < SIZE; k++) gradesf >> id[k] >> grade[k]; Lecture 22: 11/14/2002 CS149D Fall 2002