Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to 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.,
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional Arrays.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Chapter 4 Summation.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Multiple-Subscripted Array
Review of C++ Basics.
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 C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
計算機程式語言 Lecture 07-1 國立臺灣大學生物機電系 7 7 Arrays.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Copyright © 2012 Pearson Education, Inc. Chapter 7 One Dimensional Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
Arrays in C++: Numeric Character (Part 2). Passing Arrays as Arguments in C++, arrays are always passed by reference (Pointer) whenever an array is passed.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
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.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Starting Out with C++, 3 rd Edition 1 Sorting Arrays.
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
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.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Objectives You should be able to describe: One-Dimensional Arrays
Engineering Problem Solving with C++, Etter
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Pass by Reference.
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++, Etter
Functions Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_C62 Outline  Arrays  Statistical Measurements  Functions Revisited

206_C63 Arrays  One-dimensional array List of values Arranged in either a row or a column  Offsets (Subscripts) distinguish between elements in the array Identifier holds address of first element Offsets always start at 0

206_C64 Definition and Initialization  Declaration double x[4];  Initialization double x[4] = {1.2, -2.4, 0.8, 6.1}; int t[100] = {0}; char v[] = {'a', 'e', 'i', 'o', 'u'};  Loops double g[21]; for (int k=0; k<=20; k++) { g[k] = k*0.5; }

206_C65 Data Files double time[10], motion[10]; ifstream sensor3("sensor3.dat");... if(!sensor3.fail()) { for (int k=0; k<=9; k++) { sensor3 >> time[k] >> motion[k]; }

206_C66 Function Arguments  Passing array information to a function Two parameters usually used Specific array Number of elements in the array Always call by reference Address of the array

/* This program reads values from a data file and */ /* calls a function to determine the maximum value */ /* with a function. */ #include using namespace std; // Define constants and declare function prototypes. const int N=100; double maxval(double x[], int n); int main() { // Declare objects. int npts=0; double y[N]; string filename; ifstream lab;

... // Read a data value from the file. while (npts > y[npts] ) { npts++; // Increment npts. } // Find and print the maximum value. cout << "Maximum value: " << maxval(y,npts) << endl; // Close file and exit program. lab.close(); } return 0; }

/* This function returns the maximum */ /* value in the array x with n elements. */ double maxval(double x[], int n) { // Declare local objects. double max_x; // Determine maximum value in the array. max_x = x[0]; for (int k=1; k<=n-1; k++) { if (x[k] > max_x) max_x = x[k]; } // Return maximum value. / return max_x; }

206_C610 Statistical Measurements  Maximum  Minimum  Mean (average)  Median (middle)  Variance Average squared deviation from the mean  Standard Deviation Square root of the variance

206_C611 Mean double mean(double x[], int n) { double sum(0); for (int k=0; k<=n-1; k++) { sum += x[k]; } return sum/n; }

206_C612 Variance double variance(double x[], int n) { double sum(0), mu; mu = mean(x,n); for (int k=0; k<=n-1; k++) { sum += (x[k] - mu)*(x[k] - mu); } return sum/(n-1); }

206_C613 Function Overloading  Function name can have more than one definition Each function definition has a unique function signature Each function call looks for a function prototype with a matching function signature double maxval(double x[], int n); int maxval(int x[], int n); char maxval(char x[], int n);

206_C614 Function Templates  Generic algorithm for a function that is not tied to a specific data type template Data_type minval(Data_type x[], int n) { Data_type minx;...

206_C615 Summary  Arrays  Statistical Measurements  Functions Revisited

206_C616 Problem Solving Applied  Speech Signal Analysis Problem Statement Compute the following stastical measurements for a speech utterance: average power, average magnitude, and number of zero crossings. Input/Output Description Zero.dat Average power Average magnitude Zero crossings

206_C617 Problem Solving Applied Hand Example test.dat Average power = Average magnitude = 2.7 Number of zero crossings = 2

206_C618 Problem Solving Applied Algorithm Development main  read speech signal from data file and determine number of points, n  compute statistical measurements using functions ave_power(x, n)  set sum to zero  for k: add x[k] 2 to sum  return sum/n

206_C619 Problem Solving Applied Algorithm Development ave_mag(x, n)  set sum to zero  for k: add |x[k]| to sum  return sum/n crossings(x, n)  set count to zero  for k: if x[k] * x[k+1] < 0, increment count  return count

/* */ /* This program computes a set of statistical */ /* measurements from a speech signal. */ #include using namespace std; // Declare function prototypes and define constants. double ave_power(double x[], int n); double ave_magn(double x[], int n); int crossings(double x[], int n); const int MAXIMUM = 2500;

int main() { // Declare objects. int npts=0; double speech[MAXIMUM]; string filename; ifstream file_1; // Prompt user for file name and open file. cout << "Enter filename "; cin >> filename; file_1.open(filename.c_str()); if( file_1.fail() ) { cout << "error opening file " << filename << endl; return 1; }

// Read information from a data file. while (npts > speech[npts]) { npts++; } // Compute and print statistics. cout << "Statistics \n"; cout << "\taverage power: " << ave_power(speech,npts) << endl; cout << "\taverage magnitude: " << ave_magn(speech,npts) << endl; cout << "\tzero crossings: " << crossings(speech,npts) << endl; // Close file and exit program. file_1.close(); system("PAUSE"); return 0; }

/* */ /* This function returns the average power */ /* of an array x with n elements. */ double ave_power(double x[], int n) { // Declare and initialize objects. double sum=0; // Determine average power. for (int k=0; k<=n-1; k++) { sum += x[k]*x[k]; } // Return average power. return sum/n; }

/* */ /* This function returns the average magnitude */ /* of an array x with n values. */ double ave_magn(double x[], int n) { // Declare and initialize objects. double sum=0; // Determine average magnitude. for (int k=0; k<=n-1; k++) { sum += abs(x[k]); } // Return average magnitude. return sum/n; }

/* */ /* This function returns a count of the number */ /* of zero crossings in an array x with n values. */ int crossings(double x[], int n) { // Declare and initialize objects. int count=0; // Determine number of zero crossings. for (int k=0; k<=n-2; k++) { if (x[k]*x[k+1] < 0) count++; } // Return number of zero crossings. return count; } /* */

206_C626 Testing

206_C627 Testing

206_C628 Sorting Algorithms  Sorting Arranging in ascending (descending) order Not one "best" algorithm Depends on characteristics of data Selection Sort Find smallest value from current position to end Swap smallest with current position Move to next position

#include using namespace std; void sort(double x[], int n); void display(double x[], int n); const int N = 6; int main() { double data[N] = {5.0, 3.0, 12.0, 8.0, 1.0, 9.0}; cout << "Initial data" << endl; display(data, N); sort(data, N); cout << "Sorted data" << endl; display(data, N);...

void sort(double x[], int n) // Selection sort { int m; // marker double hold; for (int k=0; k<=n-2; k++) { m = k; // Find smallest value for (int j=k+1; j<=n-1; j++) { if (x[j] < x[m]) m = j; } hold = x[m]; // Swap smallest with current x[m] = x[k]; x[k] = hold; cout << "After k=" << k << endl; display(x, n); }

206_C631 Testing

206_C632 Summary  Arrays  Statistical Measurements  Functions Revisited  Problem Solving Applied  Selection Sort  End of Chapter Summary C++ Statements Style Notes Debugging Notes