arrays elec 206 computer applications for electrical engineers dr. ron hayne
TRANSCRIPT
![Page 1: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/1.jpg)
Arrays
ELEC 206
Computer Applications for Electrical Engineers
Dr. Ron Hayne
![Page 2: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/2.jpg)
206_C6 2
Outline
Arrays Statistical Measurements Functions Revisited
![Page 3: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/3.jpg)
206_C6 3
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
![Page 4: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/4.jpg)
206_C6 4
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;
}
![Page 5: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/5.jpg)
206_C6 5
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];
}
}
![Page 6: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/6.jpg)
206_C6 6
Function Arguments
Passing array information to a function Two parameters usually used
Specific arrayNumber of elements in the array
Always call by referenceAddress of the array
![Page 7: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/7.jpg)
/* This program reads values from a data file and *//* calls a function to determine the maximum value *//* with a function. */
#include <iostream>#include <fstream>#include <string>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;
![Page 8: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/8.jpg)
...
// Read a data value from the file. while (npts <= (N-1) && lab >> 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;}
![Page 9: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/9.jpg)
/* 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;}
![Page 10: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/10.jpg)
206_C6 10
Statistical Measurements
Maximum Minimum Mean (average) Median (middle) Variance
Average squared deviation from the mean
Standard Deviation Square root of the variance
![Page 11: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/11.jpg)
206_C6 11
Mean
double mean(double x[], int n)
{
double sum(0);
for (int k=0; k<=n-1; k++)
{
sum += x[k];
}
return sum/n;
}
![Page 12: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/12.jpg)
206_C6 12
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);
}
![Page 13: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/13.jpg)
206_C6 13
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);
![Page 14: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/14.jpg)
206_C6 14
Function Templates
Generic algorithm for a function that is not tied to a specific data type
template <class Data_type>
Data_type minval(Data_type x[], int n)
{
Data_type minx;
...
![Page 15: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/15.jpg)
206_C6 15
Summary
Arrays Statistical Measurements Functions Revisited
![Page 16: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/16.jpg)
206_C6 16
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
![Page 17: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/17.jpg)
206_C6 17
Problem Solving Applied
Hand Exampletest.dat
2.5 8.2 -1.1 -0.2 1.5Average power = 15.398Average magnitude = 2.7Number of zero crossings = 2
![Page 18: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/18.jpg)
206_C6 18
Problem Solving Applied
Algorithm Developmentmain
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
![Page 19: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/19.jpg)
206_C6 19
Problem Solving Applied
Algorithm Developmentave_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
![Page 20: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/20.jpg)
/*---------------------------------------------------*//* This program computes a set of statistical */ /* measurements from a speech signal. */
#include <cstdlib>#include <iostream>#include <fstream>#include <string> #include <cmath> 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;
![Page 21: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/21.jpg)
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; }
![Page 22: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/22.jpg)
// Read information from a data file. while (npts <= MAXIMUM-1 && file_1 >> 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;}
![Page 23: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/23.jpg)
/*---------------------------------------------------*//* 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;}
![Page 24: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/24.jpg)
/*---------------------------------------------------*//* 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;}
![Page 25: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/25.jpg)
/*---------------------------------------------------*//* 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;}/*---------------------------------------------------*/
![Page 26: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/26.jpg)
206_C6 26
Testing
![Page 27: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/27.jpg)
206_C6 27
Testing
![Page 28: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/28.jpg)
206_C6 28
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 endSwap smallest with current positionMove to next position
![Page 29: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/29.jpg)
#include <iostream>#include <cstdlib>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); ...
![Page 30: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/30.jpg)
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); }}
![Page 31: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/31.jpg)
206_C6 31
Testing
![Page 32: Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56649e8f5503460f94b92bc4/html5/thumbnails/32.jpg)
206_C6 32
Summary
Arrays Statistical Measurements Functions Revisited Problem Solving Applied Selection Sort End of Chapter Summary
C++ Statements Style Notes Debugging Notes