Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.

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

One Dimensional Arrays
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 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
Sahar Mosleh California State University San MarcosPage 1 While Loop and For Loop.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 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.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
CSE202: Lecture 14The Ohio State University1 Arrays.
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
Arrays.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Arrays One-Dimensional initialize & display Arrays as Arguments Part I.
Input & Output: Console
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
Chapter 8 Arrays and Strings
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
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.
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.
Sahar Mosleh California State University San MarcosPage 1 Character String.
Arrays.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
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.
Module 1: Array ITEI222 - Advance Programming Language.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
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.
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?
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 6 Arrays in C++ 2nd Semester King Saud University
The Ohio State University
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Call by Value Call by Reference Review
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Arrays Kingdom of Saudi Arabia
CS150 Introduction to Computer Science 1
Array Data Structure Chapter 6
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types

Sahar Mosleh California State University San MarcosPage 2 One Dimensional Arrays Structured data types One Dimensional Arrays - Seen only simple or atomic types so far: - int, char, float - Plus ifstream and ofstream which are a bit more complex - structure is largely hidden from programmer - access only through operations: >, …

Sahar Mosleh California State University San MarcosPage 3 Structured Data - Often work with a bunch of data with some overall structure - Analogous to: - working with a page of text and not a few hundred individual characters - working with a book, and not a few hundred loose pages - C++ provides “Arrays” as a basic way of working with structured data

Sahar Mosleh California State University San MarcosPage 4 One Dimensional Arrays - Often want to manipulate a line of text with a computer - For example: - read, print and reverse a line of 80 chars - with techniques introduced so far: - read 80 chars into 80 variables of type char - print them in the order read - print them in reverse order (Looks Hard !)

Sahar Mosleh California State University San MarcosPage 5 One Dimensional Arrays int main() { char c1, c2,c3,c4,…, c80; cin >> c1 >> c2 >> c3 >> c4 >> ………>> c80; cout << c1 << c2 << c3 << … << c80 << endl; cout << c80 << c79 << ……. << c2 << c1 << endl; } - What if number of chars not known in advance? - What if a more complicated manipulation is required? - This approach is messy and inadequate.

Sahar Mosleh California State University San MarcosPage 6 One Dimensional Arrays - Declare a single variable line which holds an array of 80 chars with the declaration: char line[80]; - the individual chars found in line are accessed as: line[0] line[1] line[2] ……. line[79] - numbering from 0 to 79 (not 1 to 80)

Sahar Mosleh California State University San MarcosPage 7 One Dimensional Arrays int main() { char line[80]; cin >> line[0] >> line[1] >> … >> line[79]; cout << line[0] << line[1] << … << line[79] << endl; cout << line[79] << line[78] <<... << line[0]<< endl; } - This doesn't really help much yet - Only the variable declaration was simpler

Sahar Mosleh California State University San MarcosPage 8 One Dimensional Arrays - In the declaration char line[80]; - line is a one-dimensional arrays - 80 is the size of the array - the number of integers the array contains - In the use of the array cout << line[ 21] - 21 is called an index to the array line

Sahar Mosleh California State University San MarcosPage 9 One Dimensional Arrays - Key observation: - arrays can be indexed with variables int main() { char line[ 80]; int i; for (i= 0; i< 80; i= i+ 1) cin >> line[ i]; for( i= 0; i< 80; i= i+ 1) cout << line[ i]; for( i= 79; i>= 0; i= i- 1) cout << line[ i]; } Loop runs i= 0, i= 1, i= 2,..., i= 79

Sahar Mosleh California State University San MarcosPage 10 One Dimensional Arrays - Arrays can be declared of any data type - char, int, float,.… - General format of array declaration: DataType ArrayName [ConstIntExpression]; - DataType is the data type of the array elements - ArrayName is the name of the array - ConstIntExpression is a literal int or int constant

Sahar Mosleh California State University San MarcosPage 11 One Dimensional Arrays - Examples of Array Declaration - float cmplx[2]; // entries: cmplx[0], cmplx[1] - int ivec[2000]; // entries: ivec[0],..., ivec[1999] - const int arraylen= 1000; - char biglin[arraylen]; // array biglin has entries biglin[0],..., biglin[999];

Sahar Mosleh California State University San MarcosPage 12 One Dimensional Arrays - Accessing Array Components - General form of array access is: ArrayName[IndexExpression] - ArrayName is the name of the array - IndexExpression is any integer expression - When evaluated, an array access is just like any other accessed variable - treat it like you would any value of the array type

Sahar Mosleh California State University San MarcosPage 13 One Dimensional Arrays - Array Entry Assignment ivec[0]= 17; // ivec entry 0 assigned 17 ivec[0]= ivec[0]* 3+ 2; // ivec entry 0 assigned 53 ivec[ivec[0]-2]= 5; // ivec entry 51 assigned 5 cout << cmplx[0]; // print out cmplx entry 0 cin >> biglin[5]; // read biglin entry 5; foo( ivec[0]); // call function foo with value 53

Sahar Mosleh California State University San MarcosPage 14 One Dimensional Arrays - Array Storage - array entries are stored one after another in memory - the index is just the offset from the beginning of the array ivec[0] ivec[2] ……. ivec[1] ivec[1999] Each location holds a single integer Memory Layout of int ivec[2000]

Sahar Mosleh California State University San MarcosPage 15 One Dimensional Arrays // E. G: find the largest element in array of integers int main() { int ValueArray[500];...; // somehow move data into ValueArray int maxval, i; maxval= ValueArray[0]; for (i= 1; i< 500; i++) if (ValueArray[ i]> maxval) maxval= ValueArray[i]; cout << "Maximum value in array is " << maxval; } start out assuming ValueArray[ 0] is largest Update maxval if a larger value found

Sahar Mosleh California State University San MarcosPage 16 One Dimensional Arrays - Out of Bounds Array Indices - What does the following code do? char line[80]; line[80]= 'X'; - Code accesses element 80, but the last defined element in the array is line[79] - result is undetermined - it may even modify other variables - C++ does not check this - your program may do weird things - this is invalid code!

Sahar Mosleh California State University San MarcosPage 17 One Dimensional Arrays Initializing arrays - Like variables of type char, float and int, arrays can be initialized when declared: int littlearray[5]={2,4,6,8, 10}; float wintertemp[3] = {-40, -45, -50}; - This is especially important for const arrays const int IntsMod5[ 5]={ 0,1,2,3,4}; const float FunConstants[ 2]={3.14, 2.71}; // arrays of constants cannot be modified

Sahar Mosleh California State University San MarcosPage 18 Array Operations - C++ does not provide "Aggregate" array operations - Operations which act on entire array - Example: int x[100], y[100], z[100]; // two arrays of 100 ints x= y; // this does not assign contents of y to x x= y+z; // this does not assign contents of y+z to x

Sahar Mosleh California State University San MarcosPage 19 Array Operations - To assign the contents of array y to x, you need a loop: int i; int x[100], y[100]; for (i= 0; i< 100; i++) x[i]= y[i]; // assign entry y[i] to value x[i]

Sahar Mosleh California State University San MarcosPage 20 Array Operations - Also cannot - Output arrays: int arr[3]={ 1,2,3}; cout << arr; - Return arrays: return arr; - Operations can be defined (by user functions) - Not defined by automatically for most types

Sahar Mosleh California State University San MarcosPage 21 Another Array Example // Sorting an array of integers into increasing order #include using namespace std; const int size = 5; // constant for array size int main() { int arr[size]; int i, tmp; bool sorted = false; // get the user to read in the array for (i= 0; i< size; i++) { cout << "Enter entry " << i << ": "; cin >> arr[ i]; }

Sahar Mosleh California State University San MarcosPage 22 Another Array Example // sort by continually swapping out of order elements while (! sorted) { sorted= true; // stays true if nothing out of order for (i= 1; i< size; i++) { if (arr[ i]< arr[ i- 1]) { sorted = false; // swap arr[ i] and arr[ i- 1]; tmp = arr[ i]; arr[ i]= arr[ i- 1]; arr[ i- 1]= tmp; } Note use of tmp variable Why is this necessary?

Sahar Mosleh California State University San MarcosPage 23 Another Array Example // print out sorted array for (i= 0; i< size; i++) { cout << arr[ i] << " "; } } // end of main program

Sahar Mosleh California State University San MarcosPage 24 Another Array Example - What is the output on the following input: i= 1 // 1st time while loop i= i= i= i= 1 // 2nd time while loop // …. // …. - Therefore, the output is: