Download presentation
Presentation is loading. Please wait.
Published byDenis Mitchell Modified over 9 years ago
1
Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
2
206_C62 Outline Arrays Statistical Measurements Functions Revisited
3
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
4
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; }
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]; }
6
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
7
/* 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;
8
... // 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; }
9
/* 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; }
10
206_C610 Statistical Measurements Maximum Minimum Mean (average) Median (middle) Variance Average squared deviation from the mean Standard Deviation Square root of the variance
11
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; }
12
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); }
13
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);
14
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;...
15
206_C615 Summary Arrays Statistical Measurements Functions Revisited
16
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
17
206_C617 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
18
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
19
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
20
/*---------------------------------------------------*/ /* 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;
21
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; }
22
// 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; }
23
/*---------------------------------------------------*/ /* 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; }
24
/*---------------------------------------------------*/ /* 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; }
25
/*---------------------------------------------------*/ /* 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; } /*---------------------------------------------------*/
26
206_C626 Testing
27
206_C627 Testing
28
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
29
#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);...
30
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); }
31
206_C631 Testing
32
206_C632 Summary Arrays Statistical Measurements Functions Revisited Problem Solving Applied Selection Sort End of Chapter Summary C++ Statements Style Notes Debugging Notes
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.