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

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

Dec 31, 2015

Download

Documents

Erick Atkins
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.

Chapter 6One-Dimensional

ArraysELEC 206

Computer Tools for Electrical Engineering

Page 2: 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.

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

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]

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

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'

true falsefalsetruefalsetrueansKey

word'H' 'e' 'l' 'l' 'o' '\0'

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

Accessing Array Elements

Subscripts are used to access individual elements of an array.

General format:array_identifier[offset]

Examplefor (int I=0; I<8; I++)m[I] = double(I) + 0.5;

Subscripts may be any integer expression.

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

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.

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

Example#include <iostream>using namespace std;const int maxSize=20;//function prototypesvoid 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;}

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

// This function inputs values into an array until –99 or// array limit reachedvoid ReadArray(double a[], int & howMany){ double temp;

howMany = 0;cin >> temp;while ((howMany < maxSize) && (temp != -99)){ a[howMany] = temp;

howMany++;cin >> temp;

}}

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

/*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;}

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

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.

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

#include <iostream>using namespace std;int Area (int Side); // Area of Squareint Area (int Length, int Width); // Area of Rectangledouble Area (double Radius); // Area of Circleconst double PI= 3.14159;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

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

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

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

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

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

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.

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

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]

29 45 18 51 36

18 45 29 51 36swap min and arr[0]

18 29 45 51 3618 29 36 51 45

18 29 36 45 51

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

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.

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

Quick Sort Conceptual Example12 6 15 24 7 31 20 3 19 8

6 7 3 8 12 15 24 31 21 19

3 6 7 8 12 15 24 31 21 19

3 6 7 8 12 15 21 19 24 31

3 6 7 8 12 15 19 21 24 31

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

Separate function12 6 15 24 7 31 20 3 19 8

12 6 7 24 15 31 20 3 19 8

12 6 7 3 15 31 20 24 19 8

12 6 7 3 8 31 20 24 19 15

6 12 7 3 8 31 20 24 19 15

6 7 12 3 8 31 20 24 19 15

6 7 3 12 8 31 20 24 19 15

6 7 3 8 12 31 20 24 19 15

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

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

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

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.

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

Example of Binary Search for 48

7059564337282214115arr[0] arr[9]

arr[mid] arr[9]arr[5]

4337

arr[5] arr[6]mid is 5

43arr[6]

arr[mid]

mid is 6

mid is 7 7059564337

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

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.

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

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

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

C++ functions for C style strings

#include <iostream>#include <cstring>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 sentenceelse strcpy (sentence,str2); // puts "Johnson into sentencestrcat(sentence, phrase);cout << "Sentence is: " << sentence << endl;return 0;

}

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

The string class include <string> declaring string objects

string word="Engineering";string word2;

string member functions size( ) empty( ) substr (int start, int len) c_str()

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

Overloaded operators for string class relational operators < > == <=

>=

concatenation + +=

assignment =

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

Example Using string Class#include <iostream>#include <string>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 sentenceelse sentence = str2; // puts "Johnson into sentencesentence += phrase;cout << "Sentence is: " << sentence << endl;return 0;

}