Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter 10.
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional Arrays.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Searching Arrays Linear search Binary search small arrays
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Chapter 8 Arrays and Strings
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
(continue) © by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 7 One Dimensional Arrays.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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.
Arrays as Function Parameters. CSCE 1062 Outline  Passing an array argument (section 9.3)  Reading part of an array (section 9.4)  Searching and sorting.
Searching and Sorting Arrays
Lecture 2A Data Types Richard Gesick
Engineering Problem Solving with C++, Etter
String in C++ A string is an array of characters which contains a non-printing null character ‘\0’ ( with ASCII value 0 ) marking its end. A string.
Array Data Structure Chapter 6
One Dimensional Arrays
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Searching and Sorting Arrays
Engineering Problem Solving with C++, Etter
Chapter 9: Data Structures: Arrays
Array Data Structure Chapter 6
Searching and Sorting Arrays
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.
Presentation transcript:

Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering

Arrays  An array is a collection of data which shares a common identifier and data type.  Individual elements of the array are specified using offsets referred to as subscripts.  In C++ the offsets or subscripts always start with 0.

Defining Arrays  General form data_type array_identifier[size]; The size of the array may be specified by a defined constant or constant literal.  Example: double m[8]; m[0] m[1] m[2] m[3] m[4] m[5] m[6] m[7]

Initializing Arrays  Initializing the array when declared char vowels[5] = {'a', 'e', 'i', 'o', 'u'}; bool ansKey[] ={true, true, false, true, false, false}; char word[] = "Hello"; vowels 'a''e''i''o''u' truefalse truefalsetrue ansKey word 'H''e''l' 'o''\0'

Accessing Array Elements  Subscripts are used to access individual elements of an array.  General format: array_identifier[offset]  Example for (int I=0; I<8; I++) m[I] = double(I) + 0.5;  Subscripts may be any integer expression.

Functions and arrays  An array identifier references the first element of the array. When arrays are passed as arguments to functions, the address of the array is passed, thus by default, arrays are always passed by reference.  Generally we specify additional parameters with the array to provide information regarding the number of elements in the array.

Example #include using namespace std; const int maxSize=20; //function prototypes void ReadArr(double a[], int &howMany); int FindMin(const double a[], int howMany); int main( ) {double darr[maxSize]; int cnt, where; ReadArr(darr, cnt); where = FindMin(darr, cnt); cout << "The smallest value in the array is " << darr[where] << endl; }

// This function inputs values into an array until –99 or // array limit reached void ReadArray(double a[], int & howMany) {double temp; howMany = 0; cin >> temp; while ((howMany < maxSize) && (temp != -99)) { a[howMany] = temp; howMany++; cin >> temp; }

/*This function returns the subscript of the minimum value in an array */ int FindMin(const double a[ ], int howMany) { minElem = 0; for (int I=1; I<howMany; I++) { if (a[I] < a[minElem]) minElem = I; } return minElem; }

Function overloading  In C++ we can have many functions with the same name provided the function signature is different.  The function signature includes the name of the function and the parameter list.

#include using namespace std; int Area (int Side); // Area of Square int Area (int Length, int Width); // Area of Rectangle double Area (double Radius);// Area of Circle const double PI= ; int main() { cout << "Area of Square:" << Area(10) << endl; cout << "Area of Rect:" << Area(8,12) << endl; cout << "Area of Circle: " << Area(2.5) << endl; return 0; } Example

int Area (int Side) // Area of Square { return (Side * Side); } int Area (int Length, int Width) // Area of Rectangle { return (Length * Width); } double Area (double Radius)// Area of Circle { return (PI* Radius * Radius); } Overloaded Function Implementations

Sorting Algorithms  Sorting algorithms arrange the data into either ascending or descending order, based on the values in the array.  Sorting algorithms to be discussed Selection sort Quick sort

Basic Premise of Selection Sort  Find minimum value, place it in the first position.  Find next minimum value, place it in the second position.  Continue doing this until you have placed the second to the largest value in the second to the last position.

Practice!  Fill in the following table to show how the array is sorted into ascending order using the selection sort. arr[0] arr[1] arr[2] arr[3] arr[4] swap min and arr[0]

Quick Sort  Select a pivot value - generally the first value in the list.  If list is greater than 2 values, divide list into two groups; values less than pivot, and values greater than the pivot.  Place pivot between these groups.  Recursively, repeat process with each group greater than 2. If group size is two, compare and swap if necessary.

Quick Sort Conceptual Example

Separate function

Searching Unordered Arrays  Simple Sequential Search Examine each element starting with the first one, until either a match is found or the end of the list is reached  Can be implemented with a function which returns true if in the list and false if not found  Can be implemented with a function which returns the location of the element if found, or –1 if not found

Searching Ordered Lists  Modified Sequential Search Stops either when item is found, or when the element examined is past where the item should be in the list.  Binary Search Examine middle element, if not found determine if item should be in top part or bottom part of list. Repeat with appropriate sublist, until sublist is empty.

Example of Binary Search for arr[0]arr[9] arr[mid]arr[9]arr[5] 4337 arr[5]arr[6] mid is 5 43 arr[6] arr[mid] mid is 6 mid is

Character Strings  C style strings array of characters terminated by \0 character remember when declaring the character array to add an extra space to the array for '\0' literal string constants are enclosed in double quote marks, "a string" file names when using file I/O must be C style strings.

Input and strings  >> uses whitespace (' ', '\t', '\n') to determine the beginning and end of data  whitespace characters can not be read using >>, to read strings with embedded whitespace use getline() char phrase[30]; getline(phrase, 30);  peek() returns the next character in a stream without removing it from the stream

C++ functions for C style strings #include uses namespace std; int main() {char str1[30]="John", str2[30]="Johnson"; char phrase[20]="'s shirt was green", sentence[30]; if (strcmp(str1,str2) < -1) strcpy (sentence, str1);// puts "John" into sentence else strcpy (sentence,str2); // puts "Johnson into sentence strcat(sentence, phrase); cout << "Sentence is: " << sentence << endl; return 0; }

The string class  include  declaring string objects string word="Engineering"; string word2;  string member functions size( ) empty( ) substr (int start, int len) c_str()

Overloaded operators for string class  relational operators == =  concatenation+ +=  assignment=

Example Using string Class #include uses namespace std; int main() {string str1="John", str2="Johnson"; string phrase = "'s shirt was green", sentence; if (str1 < str2) sentence = str1;// puts "John" into sentence else sentence = str2; // puts "Johnson into sentence sentence += phrase; cout << "Sentence is: " << sentence << endl; return 0; }