330 likes | 436 Views
Arrays. ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne. Outline. Arrays Statistical Measurements Functions Revisited. Arrays. One-dimensional array List of values Arranged in either a row or a column Offsets (Subscripts) distinguish between elements in the array
E N D
Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Outline • Arrays • Statistical Measurements • Functions Revisited 206_C6
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_C6
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_C6
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_C6
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 206_C6
/* 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;
... // 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; }
/* 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; }
Statistical Measurements • Maximum • Minimum • Mean (average) • Median (middle) • Variance • Average squared deviation from the mean • Standard Deviation • Square root of the variance 206_C6
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_C6
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_C6
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_C6
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; ... 206_C6
Summary • Arrays • Statistical Measurements • Functions Revisited 206_C6
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 Average power Average magnitude Zero crossings Zero.dat 206_C6
Problem Solving Applied • Hand Example • test.dat 2.5 8.2 -1.1 -0.2 1.5 • Average power = 15.398 • Average magnitude = 2.7 • Number of zero crossings = 2 206_C6
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_C6
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 206_C6
/*---------------------------------------------------*/ /* 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;
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 <= 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; }
/*---------------------------------------------------*/ /* 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; } /*---------------------------------------------------*/
Testing 206_C6
Testing 206_C6
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 206_C6
#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); ...
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); } }
Testing 206_C6
Summary • Arrays • Statistical Measurements • Functions Revisited • Problem Solving Applied • Selection Sort • End of Chapter Summary • C++ Statements • Style Notes • Debugging Notes 206_C6